A. Listing Program: 1. Skema Implisit Pada Heat Equation

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 8

1.

Skema Implisit Pada Heat Equation


a. Listing Program
% Implicit scheme for Heat equation

close
clear

P = 10;
h = 0.2;
g = 0.2;
tau = g * h^2;
T = 1;
m = length(0:tau:T)

xl = 0;xr = 1;
x = xl:h:xr;
n = length(x);

% initial condition
u = x;
%u(1:round(n/2))=.5 ;u(round(n/2):end)=0;

U = u;

t = 0;
ctr = 0;
set(figure,'backingstore','off','doublebuffer','on');
u0 =u;

Temp = zeros(length(0:tau:T),n);
Temp(1,:) = u0;

A=zeros(n-2,n-2)
for i=1:n-2
if (i==1)
A (i,i)=1+g
A (i,i+1)=-g
elseif (i==n-2)
A (i,i)=1+g
A (i,i-1)=-g
else
A (i,i)=1+2*g
A (i,i-1)=-g
A (i,i+1)=-g
end
end

while t<T
ctr = ctr + 1;
t = ctr * tau;

% implicit scheme
U(2:end-1) = inv(A)*u(2:end-1)'
% boundary conditions
U(1) = u(1);
U(end) = u(end);

Temp(ctr+1,:)=U;

% drawing picture
plot(x,u0,'r-',x,U,'r.');

axis([0 1 -2.5 2.5]);
title(['t = ',num2str(t)]);

drawnow;

% change variable
u = U;
end

C = zeros(m,n)
for i = 1:m
t = (i-1)*m
for j = 1:n
x=(j-1)*(n)
C(i,j)=(P/2)-((4*P/pi^2)*(cos((pi*x)/P))*(exp((-pi^2*t)/P^2)))
end
end

error = C-Temp

surf(Temp);
shading interp
colorbar



b. Tampilan Program



Keterangan: hasil tampilan program hanya ditampilkan sebagian, tidak keseluruhan.

2. Skema Crank Nicolson Pada Heat Equation
a. Listing Program
% Crank Nilson scheme for Heat equation

close
clear

P = 10;
h = 0.2;
g = 0.2;
tau = g*(h^2);
T = 1;
m = length(0:tau:T)

xl = 0;xr = 1; % boundaries
x = xl:h:xr;
n = length(x); % number of spatial grids

% initial condition
u = x;
%u(1:round(n/2))=.5 ;u(round(n/2):end)=0;

U = u;

t = 0;
ctr = 0;
set(figure,'backingstore','off','doublebuffer','on'); % to avoid
blinking on the screen
u0 =u;

Temp = zeros(length(0:tau:T),n);
Temp(1,:) = u0;

A=zeros(n-2,n-2)
for i=1:n-2
if (i==1)
A (i,i)=2+g
A (i,i+1)=-g
elseif (i==n-2)
A (i,i)=2+g
A (i,i-1)=-g
else
A (i,i)=2*(1+g)
A (i,i-1)=-g
A (i,i+1)=-g
end
end

B=zeros(n-2,n-2)
for i=1:n-2
if (i==1)
B (i,i)=2-g
B (i,i+1)=-g
elseif (i==n-2)
B (i,i)=2-g
B (i,i-1)=-g
else
B (i,i)=2*(1-g)
B (i,i-1)=-g
B (i,i+1)=-g
end
end

while t<T
ctr = ctr + 1;
t = ctr * tau;

% Crank Nilson scheme
U(2:end-1) = inv(A)*B*u(2:end-1)'

% boundary conditions
U(1) = u(1);
U(end) = u(end);

Temp(ctr+1,:)=U;

% drawing picture
plot(x,u0,'r-',x,U,'r.');

axis([0 1 -2.5 2.5]);
title(['t = ',num2str(t)]);

drawnow;

% change variable
u = U;
end

C = zeros(m,n)
for i = 1:m
t = (i-1)*m
for j = 1:n
x=(j-1)*(n)
C(i,j)=(P/2)-((4*P/pi^2)*(cos((pi*x)/P))*(exp((-pi^2*t)/P^2)))
end
end

error = C-Temp
surf(Temp);
shading interp
colorbar

b. Tampilan Program

You might also like