i need help with this question

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

Jan
Jan 2019년 5월 7일
편집: Jan 2019년 5월 7일
All we know is the code. We cannot know, why you assume, that the answer is wrong. Without any further details it is impossible to miodify a code, such that it produces the output you expect. So please edit the question, exoplain, which problem you want to solve and why you assume, that there is a bug.
dulanga
dulanga 2019년 5월 7일
with c= -0.9 on -10<x<10 with the Gaussian initial condition f,The code runs from t = 0 until a time in which |y|=0.001 on one of the boundaries,solve this problem using the centred-in-space approximation
So my assignment has a method of checking if my answer is correct where input my final answer and it says if it is correct or not and in this case it is incorrect.
the below is the centered approach method i should implement in matlab
thanks
Cap22222ure.JPG
Torsten
Torsten 2019년 5월 7일
편집: Torsten 2019년 5월 7일
A is wrong. The factor 0.5 is missing, and the lower diagonal is replaced by a main diagonal.
Maybe this A was written for Euler forward instead of centered.
dulanga
dulanga 2019년 5월 7일
편집: dulanga 2019년 5월 7일
oh sorry i forgot the 0.5 where the c is at what do u mean by lower diagonal is replaced by main ?
%using centered space
A=eye(N)-(0.5*c*(dx/dt))*diag(ones(N-1,1),1)-diag(ones(N-1,1),-1);
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.
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개)

카테고리

도움말 센터File Exchange에서 Just for fun에 대해 자세히 알아보기

태그

질문:

2019년 5월 7일

댓글:

2019년 5월 7일

Community Treasure Hunt

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

Start Hunting!

Translated by