Azzera filtri
Azzera filtri

How can I do the same calculation multiple times for specified values of a variable

9 visualizzazioni (ultimi 30 giorni)
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

Risposta accettata

Star Strider
Star Strider il 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 Commenti
Star Strider
Star Strider il 14 Feb 2020
As always, my pleasure!
No worries. MATLAB has a lot of useful characteristics that take a while to learn about.

Accedi per commentare.

Più risposte (0)

Prodotti

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by