Finite difference method using upwind scheme

16 Ansichten (letzte 30 Tage)
Md Khorshed Alam
Md Khorshed Alam am 27 Nov. 2021
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!

Antworten (0)

Kategorien

Mehr zu App Building finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by