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

18 visualizaciones (últimos 30 días)
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

Respuesta aceptada

Star Strider
Star Strider el 14 de Feb. de 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 comentarios
Cliff Shaw
Cliff Shaw el 14 de Feb. de 2020
Works nicely and is more elegant than my solution
Thanks
Star Strider
Star Strider el 14 de Feb. de 2020
As always, my pleasure!
No worries. MATLAB has a lot of useful characteristics that take a while to learn about.

Iniciar sesión para comentar.

Más respuestas (0)

Productos

Community Treasure Hunt

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

Start Hunting!

Translated by