i need help with this question

1 次查看(过去 30 天)
dulanga
dulanga 2019-5-7
评论: dulanga 2019-5-7
I am supposed to find the number of timesteps i.e the number of steps taken but my answer is incorrect any inputs on my code will be a major help thanks.Also has the advection equation with the center spaced method used correctly ?
N=200;
x=linspace(-10,10,N);
dx=x(2)-x(1);
c=-0.9;
t=0;
dt=0.2;
timestep=0;
f=@(x)exp(-5*x.^2);
y=f(x)';
figure(1),plot(x,y)
title(['Current time=',num2str(t)])
drawnow
%using centered space
A=eye(N)-(0.5*c*(dt/dx))*((diag(ones(N-1,1),1)-diag(ones(N,1),0)));
while abs(y(end))<0.001 && abs(y(1))<0.001
y=A*y;
t=t+dt;
timestep=timestep+1;
figure(1),cla
plot(x,y)
hold on
plot(x,f(x-c*t),'r')
title(['Current time=',num2str(t)])
drawnow
end
  6 个评论
Torsten
Torsten 2019-5-7
编辑:Torsten 2019-5-7
You forgot the parenthesis around the difference of the diag-expressions.
And it must be dt/dx instead of dx/dt.
dulanga
dulanga 2019-5-7
thanks alot how would u implemnt this Capture.JPG
Capt11ure.JPG
N=1000;
x=linspace(-10,10,N);
dx=x(2)-x(1);
c=2.6;
t=0;
dt=0.2;
timestep=0;
f=@(x)exp(-5*x.^2);
y=f(x)';
figure(1),plot(x,y)
title(['Current time=',num2str(t)])
drawnow
%using lax
A=eye(N)-(0.5*c*(dt/dx))*diag(ones(N-1,1),1)-diag(ones(N-1,1),-1);
while abs(y(end))<0.001 && abs(y(1))<0.001
y=A*y;
t=t+dt;
timestep=timestep+1;
figure(1),cla
plot(x,y)
hold on
plot(x,f(x-c*t),'r')
title(['Current time=',num2str(t)])
drawnow
end

请先登录,再进行评论。

回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Introduction to Installation and Licensing 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by