Why is the graph in this code becoming distorted?

6 ビュー (過去 30 日間)
영택 조
영택 조 2023 年 9 月 19 日
編集済み: Dyuman Joshi 2023 年 9 月 19 日
%% This is the code.
n=101;
nstep=100;
long=10.0;
h=long/(n-1);
dt=0.05;
D=0.05;
f=zeros(n,1);
y=zeros(n,1);
time=0.0;
for i=1:n
f(i)=0.5*sin(2*pi*h*(i-1));
end
for m=1:nstep
plot(f,'linewidth',2);
axis([1 n -2 2])
y=f;
for ii=2:n-1
f(ii)=y(ii)-0.5*(dt/h)*(y(ii+1)-y(ii-1)+D*(dt/h^2)*(y(ii+1)-2*y(ii)+y(ii-1)));
end
f(n)=y(n)-0.5*(dt/h)*(y(2)-y(n-1))+D*(dt/h^2)*(y(2)-2*y(n)+y(n-1));
time=time+dt;
title(sprintf('time = %3.2f',time))
pause(0.2)
end
As a result of running this code, the graph becomes distorted from the right end and stretched as 'time' increases. Is there something wrong that is causing this?

採用された回答

Mathieu NOE
Mathieu NOE 2023 年 9 月 19 日
hello
maybe this ? I simply corrected one of your equation (looking at the one that follows , assumming the second was correct ) because there is a difference where you put your parenthesis
%% This is the code.
n=101;
nstep=100;
long=10.0;
h=long/(n-1);
dt=0.05;
D=0.05;
f=zeros(n,1);
y=zeros(n,1);
time=0.0;
i=1:n;
f=0.5*sin(2*pi*h*(i-1));
for m=1:nstep
plot(f,'linewidth',2);
axis([1 n -2 2])
y=f;
for ii=2:n-1
% f(ii)=y(ii)-0.5*(dt/h)*(y(ii+1)-y(ii-1)+D*(dt/h^2)*(y(ii+1)-2*y(ii)+y(ii-1)));
f(ii)=y(ii)-0.5*(dt/h)*(y(ii+1)-y(ii-1))+D*(dt/h^2)*(y(ii+1)-2*y(ii)+y(ii-1)); % correction based on the equation 2 lines below
end
f(n)=y(n)-0.5*(dt/h)*(y(2)-y(n-1))+D*(dt/h^2)*(y(2)-2*y(n)+y(n-1));
time=time+dt;
title(sprintf('time = %3.2f',time))
pause(0.2)
end

その他の回答 (0 件)

カテゴリ

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

タグ

製品


リリース

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by