You are on page 1of 3

# Sample MATLAB codes

1.
%Newton Cooling Law
clear; close all; clc;
h = 1;
T(1) = 10; %T(0)
error = 1;
TOL = 1e-6;
k = 0;
dt = 1/10;
while error > TOL,
k = k+1;
T(k+1) = h*(1-T(k))*dt+T(k);
error = abs(T(k+1)-T(k));
end
t = linspace(0,dt*(k+1),k+1);
plot(t,T),hold on, plot(t,1,'r-.')
xlabel('Time'),ylabel('Temperature'),
title(['T_0 = ',num2str(T(1)), ',
T_\infty = 1']),

2.
%Boltzman Cooling Law
clear; close all; clc;
h = 1;
T(1) = 10; %T(0)
error = 1;
TOL = 1e-6;
k = 0;
dt = 1/10000;
while error > TOL,
k = k+1;
T(k+1) = h*(1-(T(k))^4)*dt+T(k);
error = abs(T(k+1)-T(k));
end
t = linspace(0,dt*(k+1),k+1);
plot(t,T),hold on, plot(t,1,'r-.')
xlabel('Time'),ylabel('Temperature'),
title(['T_0 = ',num2str(T(1)), ',
T_\infty = 1']),

3.
%Fourier Heat conduction
clear; close all; clc;
h = 1;
n = 11;
T = ones(n,1); Told = T;
T(1) = 1; %Left boundary
T(n) = 10; %Right boundary
x = linspace(0,1,n);
dx = x(2)-x(1);
dt = dx^2/3; %cfl condition
error = 1;

out) xlabel('x').1) = 1. Wave Translation %Oscillations .. end subplot(2. while error > TOL. while error > TOL k = k+1.1. %legend('Cooling Trend'. + Told(i.1.y..j)+Told(i. 2D Heat Equation %2D Heat Equation.ylabel('Temperature').contour(x. close all.j))/dx^2 .T).. %RIGHT dt = dx^2/4. close all.xlabel('x').1). error = 1.'Steady State') 4. k = 0.:) = T.2). clc n = 10. %grid has n .pcolor(x. T = zeros(n).translation left and right clear. out(k.1:n) = 10.j) = dt*((Told(i+1.colorbar subplot(2.1.shading interp. y = x. dx = x(2)-x(1). k = k+1. Told = T.j).colorbar 5.j+1)-2*Told(i.n) = 1.1:n) = 1. clear.ylabel('y'). dy = dx. k = 0. T(1. dt = dx. %TOP T(n.ylabel('y').13.j)-2*Told(i.5)==0. title('Temperature (Steady State)').. clc. Told = T. dx = x(2)-x(1). %LEFT T(1:n.j-1))/dy^2) .xlabel('x'). k = 0.y. + (Told(i. TOL = 1e-6. TOL = 1e-6. for c = [1 -1] cc = 0.TOL = 1e-6. end error = max(abs(T-Told)). for i = 2:n-1 T(i) = dt*(Told(i+1)-2*Told(i)+Told(i-1))/dx^2+Told(i).1). . x = linspace(0. title(['Fourier Heat Conduction']). if mod(k.n). %BOTTOM T(1:n. for i = 2:n-1 for j = 2:n-1 T(i. error = 1. end end error = max(max(abs(Told-T))). title('Temperature (Steady State)').j)+Told(i-1. u = zeros(n.n).2 interior points per dimension (overlapping) x = linspace(0.T). end end plot(x. n = 261. u(121:141) = sin(pi*x(121:141)).

c = 1.out(hh. u(:. end u = zeros(n. u(121:141) = sin(pi*x(121:141)).u) xlabel('u(x)'). end u(n. end u = zeros(n. end plot(x.10)==0.k+1) = 0. %wave equation clear.1) = sin(pi*x). end %c = 1 if c == -1.ylabel('Time'). plot(x.1)). u(1. k = k+1. close all. title('Translation to the Right') end end 6. u(i) = dt*c*(uold(i)-uold(i-1))/dx+uold(i).k))/dx^2+2*u(i.hold on.1))/dx^2+2*u(i.u).u) xlabel('u(x)').out(hh. title('Translation to the Left') elseif c == -1 subplot(2.2).hold on. for hh = 1:cc plot(x.2) = 0.1. clc. for i = 2:n-1 u(i. k = 1. for hh = 1:cc plot(x. x = linspace(0.:)+hh). for i = 2:n-1 u(i.1).k+1) = 0. for i = 2:n-1 if c == 1. u(121:141) = sin(pi*x(121:141)). end end if c == 1 subplot(2. dt = dx.k-1). while k < 100 k = k+1.5*(dt^2*c^2*(u(i+1. if mod(k. out(cc. u(i) = dt*c*(uold(i+1)-uold(i))/dx+uold(i).k+1) = dt^2*c^2*(u(i+1.k)-u(i.while k < 110 uold = u. u(1.ylabel('y') .2) = 0.:)+hh).:) = u.ylabel('Time').1)-2*u(i. n = 21.1).k)+u(i-1.n).1). end %c = -1 end error = max(abs(u-uold)).1. dx = 1/(n-1). xlabel('x').2) = 0.k)-2*u(i. plot(x.1. cc = cc+1.1)+u(i-1. end u(n. error = 1.