plotting with for loop

4 ビュー (過去 30 日間)
Nima Vali
Nima Vali 2020 年 9 月 28 日
コメント済み: Nima Vali 2020 年 9 月 28 日
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 件のコメント
Star Strider
Star Strider 2020 年 9 月 28 日
As always, my pleasure!
Nima Vali
Nima Vali 2020 年 9 月 28 日
Thank you.

サインインしてコメントする。

採用された回答

Star Strider
Star Strider 2020 年 9 月 28 日
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')
.

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeCrystals についてさらに検索

製品


リリース

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by