Finite difference method using upwind scheme

조회 수: 16 (최근 30일)
Md Khorshed Alam
Md Khorshed Alam 2021년 11월 27일
Problem:
My code:
% clear workspace
clear
clc
% define variables
xmin = 0;
xmax = 1;
t = 0;
tmax = 1;
h=input("Enter the temporal step size h = ");
dx=input("Enter the spatial step size dx = ");
N = (xmax-xmin)/dx; % No of nodes
%discretize the domain
x = xmin : dx : xmax;
%calculate intial condtions
u0 = x+1;
u = u0;
unp1 = u;
%march fds through time
nsteps = ceil(tmax/h);
for n = 1 : nsteps
%boundary condtions
u(1) = exp(-t);
% calculate upwind scheme for a>0
for i = 2: N+1
unp1(i)= u(i)-(x+1)*h/dx*(u(i)-u(i-1));
end
% calculate exact solution
exact = (x+1)*exp(-t);
% error calculation
err = abs(unp1-exact);
% update u
u = unp1;
x= x+dx;
t = t+h;
% plot solution
plot(x,exact,'r-')
hold on
plot(x,u,'o-')
hold off
axis ([0 1 0 1])
shg
pause(h)
end
I'm not sure whether I coded the intial and boundary condtions correctly or not? Please correct me if I am wrong. Also, I'm getting error while run becasue of the term (x+1) inside the for loop. I understand that I need to create a loop for x but have no idea how can I do that since I discretized the domain already. Please help me with that. Thank you!

답변 (0개)

카테고리

Help CenterFile Exchange에서 App Building에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by