0% found this document useful (0 votes)
32 views4 pages

For End For End For For End End For For

The document describes solving a partial differential equation using an explicit finite difference scheme. It initializes matrices to store the calculated and exact solutions over a grid with stepsizes h and k in space and time. Boundary values are applied at t=0. The scheme then marches forward in time, computing values within the domain based on neighboring points. The calculated solution is plotted against the exact solution to compare the results. The problem is solved on a grid with stepsizes h and k. Matrices are initialized to store the calculated and exact solutions. Boundary values are applied at t=0. Values within the domain are then computed using an explicit finite difference scheme, marching forward in time based on neighboring points. The calculated solution

Uploaded by

BaluKrish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

For End For End For For End End For For

The document describes solving a partial differential equation using an explicit finite difference scheme. It initializes matrices to store the calculated and exact solutions over a grid with stepsizes h and k in space and time. Boundary values are applied at t=0. The scheme then marches forward in time, computing values within the domain based on neighboring points. The calculated solution is plotted against the exact solution to compare the results. The problem is solved on a grid with stepsizes h and k. Matrices are initialized to store the calculated and exact solutions. Boundary values are applied at t=0. Values within the domain are then computed using an explicit finite difference scheme, marching forward in time based on neighboring points. The calculated solution

Uploaded by

BaluKrish
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

h=1/8;%delta x

k=1/8;%delta t
las=(k/h)^2;%lamda square
rows=1/h+1;%no.of rows
columns=20;%no.of columns(can be set exact acc to time=2 i.e,columns=17 for
given h,k)
u=zeros(rows,columns);% initializing u for calculated values
u1=u;% initializing u1 for exact values
%boundary condition at t=0
for i=1:rows
u(i,1)=sin(pi*(i-1)*h)+sin(3*pi*(i-1)*h)/2+3*sin(7*pi*(i-1)*h);
end
%step 1 computation using neumann boundary condition
for i=2:rows-1
u(i,2)=las*(u(i+1,1)+u(i-1,1))/2+(1-las)*u(i,1)+k*sin(2*pi*(i-1)*h);
end
%computing remainig steps using explicit scheme
for j=2:columns-1
for i=2:rows-1
u(i,j+1)=las*(u(i+1,j)+u(i-1,j))+2*(1-las)*u(i,j)-u(i,j-1);
end
end
%computing exact solution
for i=1:rows
for j=1:columns
u1(i,j)=sin(pi*(i-1)*h)*cos(pi*(j-1)*k)+sin(2*pi*(i1)*h)*cos(2*pi*(j-1)*k)/(2*pi)+sin(3*pi*(i-1)*h)*cos(3*pi*(j1)*k)/2+3*sin(7*pi*(i-1)*h)*cos(7*pi*(j-1)*k);
end
end
%u--calculated values
%u1-exact valued
%plot at t=?
t=2;
x=1:rows;
j1=t/k+1;
%exact values-- blue line with + at each point
%calculated values -- with red line
plot(x,u1(x,j1),'b+:',x,u(x,j1),'r')
disp('%exact values-- blue line with + at each point...')
disp('%calculated values -- with red line')

Red line calculated values


Blue line with + -- exact values

PROBlEM

2;

theta = pi/4;
r = 3;
h
k
m
n

=
=
=
=

1/2;
pi/16;
theta/k;
r/h;

A = zeros(m+1,n+1);
P = zeros((m-1)*(n-1),1);
sols = zeros((m-1)*(n-1),1);
d = zeros((m-1)*(n-1),1);
for i = 1:n+1
A(1,i) = 1;
A(m+1,i) = -1;
end

for i = 1:m
A(i,n+1) = 2;
A(i,1) = (A(1,1) + A(m+1,1))/2;
end
A(1,n+1) = (2+1)/2;
A(m+1,n+1) = (2-1)/2;
for i = 1:(m-1)*(n-1)
[indx,indy] = getIndex(i,n);
if i <= n-1
d(i) = -1*(1/((k^2)*(indx-1)^2))*(A(indy-1,indx));
else if i > (m-2)*(n-1)
d(i) = -1*(1/((k^2)*(indx-1)^2))*(A(indy+1,indx));
end
end
P(i,i) = -2*(1+1/((k^2))*(indx-1)^2);
if i > n-1
P(i,i-(n-1)) = (1/((k^2)*(indx-1)^2));
else
d(i) = -1*(1/((k^2)*(indx-1)^2))*A(indy-1,indx);
end
if i <= (m-2)*(n-1)
P(i,i+(n-1)) = (1/((k^2)*(indx-1)^2));
else
d(i) = -1*(1/((k^2)*(indx-1)^2))*A(indy+1,indx);
end
if indx == 2
P(i,i+1) = 1+1/(2*indx);
d(i) = d(i) + -1*A(indy,indx-1)*(1-1/(2*indx));
else if indx == n
P(i,i-1) = 1-1/(2*indx);
d(i) = d(i) + -1*A(indy,indx+1)*(1+1/(2*indx));
end
end
end
sols = inv(P)*d;
sols
for k =1:(m-1)*(n-1)
[indx,indy] = getIndex(k,n);
A(indy,indx) = sols(k);
end
plot(x,u1(x,j1),x,u(x,j1))

#Problem 3
h=1/4;%delta x
k=1/2;%delta t
la=k/(h)^2;%lamda square
rows=1/h+1;%no.of rows
columns=3;%no.of columns
u=zeros(rows,columns);% initializing u for calculated values
u1=u;% initializing u1 for exact values

%boundary condition at t=0


for i=1:rows
u(i,1)=cos(pi*(i-1)*h);
end
for j=1:columns-1
for i=2:rows-1
if i==2
b(i)=la*(u(i-1,j)+u(i+1,j))/2+(1-la)*u(i,j)+la^2*u(i,j+1)/2;
end
if i==3
b(i)=la^2*(u(i-1,j)+u(i+1,j))/2+(1-la^2)*u(i,j)
end
if i==4
b(i)=la^2*(u(i-1,j)+u(i+1,j))/2+(1-la^2)*u(i,j)+la^2*u(i,j+1)/2;
end
end
A=[1+la^2,-1*la^2/2,0;-1*la^2/2,la^2+1,-la^2/2;0,-la^2/2,la^2+1];
end

You might also like