plotting with for loop

1 vue (au cours des 30 derniers jours)
Nima Vali
Nima Vali le 28 Sep 2020
Commenté : Nima Vali le 28 Sep 2020
hello I am trying to solve FTCS discretization of the heat equation
and I have writtenn the below code:
I have 3 grid refinement, dx=[0.5,0.25,0.05];
I am going to plot 3 plot get for each grid (x,T1) in one plot. Currently, I can not plot 3 plot in one plot. I do not know how pull out T1 values after completion in for loop. Can someone tell me how to plot?
%setting up the parameters
clear;clc;;close all;
U0=2;
dt=0.01;
tf=1000;
ntotal=tf/dt;
dx=[0.5,0.25,0.05];
Kappa=0.001;
% boundary condition
for i=1:length(dx)
x = (-3:dx(i):3);
n=length (x);
alpha=Kappa*dt./dx(i).^2;
for j=1:n
if (1<x(j)) || (x(j)<-1)
T0(j)=0;
else T0(j)=U0;
end
end
%solving the problem with FTC
for k=1:ntotal
for i=2:n-1
T1(i) = T0(i) + alpha*(T0(i+1)-2*T0(i)+T0(i-1));
T1(1)=T0(1) + alpha*(T0(2)-2*T0(1)+0);
T1(n) = T0(n) + alpha*(0-2*T0(n)+T0(n-1));
end
T0=T1;
end
figure
plot (x,T1)
hold on
end
  3 commentaires
Star Strider
Star Strider le 28 Sep 2020
As always, my pleasure!
Nima Vali
Nima Vali le 28 Sep 2020
Thank you.

Connectez-vous pour commenter.

Réponse acceptée

Star Strider
Star Strider le 28 Sep 2020
To plot all of them on the same axes, put the figure call before the loop instead of inside it:
figure
hold on
for i=1:length(dx)
x = (-3:dx(i):3);
n=length (x);
alpha=Kappa*dt./dx(i).^2;
for j=1:n
if (1<x(j)) || (x(j)<-1)
T0(j)=0;
else T0(j)=U0;
end
end
%solving the problem with FTC
for k=1:ntotal
for i=2:n-1
T1(i) = T0(i) + alpha*(T0(i+1)-2*T0(i)+T0(i-1));
T1(1) = T0(1) + alpha*(T0(2)-2*T0(1)+0);
T1(n) = T0(n) + alpha*(0-2*T0(n)+T0(n-1));
end
T0=T1;
end
plot (x,T1)
end
hold off
legend(compose('dx = %.2f',dx), 'Location','S')
.

Plus de réponses (0)

Catégories

En savoir plus sur 2-D and 3-D Plots dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by