필터 지우기
필터 지우기

plotting with for loop

조회 수: 1 (최근 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 CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by