How can I do the same calculation multiple times for specified values of a variable
17 views (last 30 days)
Show older comments
I am calculating compositonal profiles with a very simple version of the diffusion equation.
I want to do the same calculation several times and store the values in the workspace (ideally as mutliple columns for a single variable).
I have figured out the followng way to do it - but I am sure that there is a more elegant solution to my problem:
x = 0:1:100;
Co = 22;
t1 = 30000;
t2 = 60000;
t3 = 120000;
D = 0.001;
C1 = Co*erf(x/(D*t1).^0.5);
C2 = Co*erf(x/(D*t2).^0.5)
C3 = Co*erf(x/(D*t3).^0.5)
plot(x,C1, x, C2, x,C3)
Thanks
Cliff
0 Comments
Accepted Answer
Star Strider
on 14 Feb 2020
Thios does the same as yoiur code, using a loop, and a simplified plot call:
x = 0:1:100;
Co = 22;
t1 = 30000;
t2 = 60000;
t3 = 120000;
t = [t1 t2 t3];
D = 0.001;
for k = 1:numel(t)
C(k,:) = Co*erf(x/(D*t(k)).^0.5);
end
figure
plot(x,C)
grid
It is may be a bit more efficient. I did not assess that specifically.
2 Comments
Star Strider
on 14 Feb 2020
As always, my pleasure!
No worries. MATLAB has a lot of useful characteristics that take a while to learn about.
More Answers (0)
See Also
Categories
Find more on Title in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!