This is a sample MATLAB code that calculates but with different conditions (So i
ID: 3168390 • Letter: T
Question
This is a sample MATLAB code that calculates but with different conditions (So it doesnt apply to this particular question).
clc;
clear all;
n=8;
m=40;
a=0; % lower limt for x
b=4; %upper limt for x
c=0;% lower limt for t
d=1; % upper limt for t
h=(b-a)/n; % step size for x
k=(d-c)/m; % step size for t
% initial condidtion x=0 to 2
for i=1:n/2+1;
u0(i)=1;
end
% initial condidtion x=2 to 4
% 1.0000
for i=n/2+1:n+1
u0(i)=0;
end
lamd=(k/h^2)
t=0;
while(t<1)
t=t+k
u(1)=1;
u(n+1)=0;
for i=2:n
u(i)=u0(i+1)*lamd+(1-2*lamd)*u0(i)+u0(i-1)*lamd;
end
for i=1:n+1
u0(i)=u(i);
end
end
Explanation / Answer
clc;
clear all
n=8; % number of points in x
m=40; % number of points in t
a=0; %lower limt of x
b=6; %upper limit of x
c=0; %lower limt of t
d=1 ;% upper limt of t
dx=(b-a)/n; %step length of x
dt=(d-c)/m; %step length of t
x=a:dx:b;
% initial conditon ( from i =1 t0 n/2+1 means 1 to 3)
for i=2:n/2+1
u0(i)=1;
end
% initial conditon ( from i =n/2+1 t0 n/2+1 means 4 to 6)
for i=n/2+2:n+1
u0(i)=0;
end
t=c;
r=dt/dx^2; % (lamda )
while (t<d)
t=t+dt; % increment of t
u(1)=0; % first boundary condition
u(n+1)=0; % second boundary condition
for i=2:n
u(i)=u0(i+1)*r+u0(i)*(1-2*r)+u(i-1)*r; % Given formula
end
for i=2:n
u0(i)=u(i);
end
end
for i=1:n-1
fprintf('u(%f,1)=%f ',i*dx,u(i+1))
end
u(0.750000,1)=0.369262
u(1.500000,1)=0.606204
u(2.250000,1)=0.659625
u(3.000000,1)=0.554866
u(3.750000,1)=0.371588
u(4.500000,1)=0.200051
u(5.250000,1)=0.080930
>>
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.