Math 542 Numerical Solutions of Differential Equations
Math 542 Numerical Solutions of Differential Equations
DIFFERENTIAL EQUATIONS
GRADIENT FIELDS WITH
SOLUTIONS
AUTONOMOUS SYSTEMS
GRAD FIELD FOR A 2 EQ.
SYSTEM WITH SOL
INTRODUCTION
FIRST ORDER PDE EXAMPLE
TRAFFIC MODEL W/O
SHOCK
TRAFFIC MODEL WITH
SHOCK
INITIAL VALUE PROBLEMS
(MAIN AND FUNCTION)
RUNGE-KUTTA
VARIABLE STEP RUNGEKUTTA (1,2)
INITIAL VALUE BACKWARDS
PROBLEMS(ODE) DIFFERENTIATION
FORMULAS
QUASI-VARIABLE STEP 2ND
ORDER BDF
ADAMS METHODS
A-STABILITY REGION
FINITE
DIFFERENCE
METHODS
EXAMPLE 1
EXAMPLE 2
HANGING CABLE EXAMPLE
LAPLACE'S EQUATION
HEAT EQUATION
WAVE EQUATION
LINEAR SUBDOMAIN
COLLOCATION
FINITE ELEMENT GALERKIN
METHOD
TRIANGULAR GALERKIN
TRIANGULAR GRID
REFINEMENT
HOME
INDEX
function F=Ode_function(x,y)
global func_f
F=eval(func_f);
MAIN PROGRAM
%plots the direction field and solutions for the
%equation dy/dx = f(x,y)
%enter f as a character string MATLAB code for f
clear
global func_f
clf reset
%enter grid limits and size from the keyboard
%x_min=input('x minimum = '); x_max=input('x maximum = ');
%y_min=input('y minimum = '); y_max=input('y maximum = ');
%delta_x=input('delta x = '); delta_y=input('delta y = ');
%or just edit the following line
x_min = 0; x_max = 3; y_min = 0; y_max = 4; delta_x = .2; delta_y = .2;
[x,y] = meshgrid(x_min:delta_x:x_max, y_min:delta_y:y_max);
[n,m]=size(x);
%enter righthand side function
%func_f=input('dy/dx = ','s');
%or just edit the following line
func_f ='-9*(x-1).*y'
Heading = ['\n\n\n', 'Window Parameters for the Equation \n\n',' dy/dx = ', func_f, '\n\n'];
fprintf(Heading)
fprintf('\n x-min = %g x-max = %g y-min = %g y-max = %g \n',...
x_min, x_max, y_min, y_max)
fprintf('\n delta-x = %g delta-y = %g \n\n', delta_x, delta_y)
z=eval(func_f);
One=ones(n,m);
L=sqrt(One.^2+z.^2);
quiver(x, y, One./L,z./L,.9)
axis([x_min,x_max,y_min,y_max])
grid on
xlabel('x axis')
ylabel('y axis')
Title=['dy/dx = ', func_f];
title(Title)
Test=input('Plot a solution? ', 's');
while Test(1) == 'y'
fprintf('\n Use the mouse to locate the initial values in the Figure Window \n')
[int_x, int_y]=ginput(1);
hold on
plot(int_x,int_y,'ko','LineWidth',2,'MarkerSize',7)
[u,v]=ode45('Ode_function',[int_x, x_max], int_y);
plot(u,v, 'r','LineWidth',2)
Test = input('Plot another solution? ', 's');
end
TOP
BACK
AUTONOMOUS SYSTEMS
Also requires the following function subprogram
function F=ASystem_function(t,X)
global func_f func_g
x=X(1);
y=X(2);
F=[eval(func_f); eval(func_g)];
MAIN PROGRAM
TOP
BACK
Main Program
%plots the direction field and solutions for the
%system dy/dx = f(x,y,z) dz/dx = g(x,y,z)
%enter f and g as a character string
clear
global func_f func_g
clf reset
%set the figure window size and grid size
x_min = 0; x_max = 20; y_min = -2; y_max = 2; z_min = -2; z_max = 2;
delta_x = 2; delta_y = 1; delta_z = 1;
[x,y,z] = meshgrid(x_min:delta_x:x_max, y_min:delta_y:y_max,
z_min:delta_z:z_max);
[n,m,p]=size(x);
%enter righthand side functions f and g
func_f='z'
func_g ='-sin(y)';
%enter the initial conditions at x=0
int_y=0; int_z=1.5;
Heading = ['\n\n\n', 'Window Parameters for the System \n\n',' dy/dx = ',
func_f, '\n\n',' dz/dx = ', func_g, '\n\n'];
fprintf(Heading)
fprintf('\n x-min = %g x-max = %g y-min = %g y-max = %g z-min = %g z-max =
%g \n',...
x_min, x_max, y_min, y_max, z_min, z_max)
TOP
BACK
TOP
BACK
TOP
BACK
end
end
c=ones(n,m);
for j=1:m
a=0; b=0;
for i=1:n
if X(i,j)>0 & a==0
is=i; a=1;
end
if X(n-i+1,j)<0 & b==0
ib=n-i+1; b=1;
end
end
if is<ib
for k=is:ib
X(k,j)=0;
c(k,j)=0;
end
end
end
surf(X,T,D,X)
shading interp
colormap hsv
view(-20,30)
colorbar
grid on
xlabel('x axis')
ylabel('t axis')
zlabel('d axis')
rotate3d on
TOP
BACK
EXAMPLE 1
%Example 1
clear, clf
C = 1; D = .01; K = .1; A=[]; n = 4;
for i = 1:8
flops(0)
h = 1/n;
%Construct the Tridiagonal System
l=ones(1,n-2); d=-(2+K*h^2/D)*ones(1,n-1); b=[zeros(1,n-2), -C];
%Solve the Tridiagonal System
U = tridiag(l,d,l,b);
p=flops;
%Plot the Approx. Solution
X = 0:h:1; U = [0, U, C];
plot(X,U,'b')
grid on, xlabel('x-axis'), ylabel('u-axis'), title('Example 2.6')
hold on
%Save values at the midpoint for error analysis
A=[A;n,p,X(n/2+1),U(n/2+1)];
n = n*2;
end
hold off
%error analysis
[nn,mm]=size(A);
diff(1)=0;ratio(1)=0; ratio(2)=0;
for i=1:nn-1,
diff(i+1)=A(i+1,4)-A(i,4);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,4) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)';A(:,4)'; diff; ratio; xacc];
fprintf(1,'\n\n n flops y diff ratio y acc\n\n')
fprintf(1,'%3.0f %7.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
EXAMPLE 2
%Example 2
clear, clf
C = 1; D = .01; K = .1; A=[];n = 4;
for i = 1:8
h = 1/n;
flops(0)
%Using Backward Diff at endpoint
%l=[ones(1,n-2),-1]; u=ones(1,n-1); d=[-(2+K*h^2/D)*ones(1,n-1),1];
%b=[zeros(1,n-1), h*C/D];Y = tridiag(l,d,u,b);
%Using an imaginary node at x=1+h
l=[ones(1,n-2),2]; u=ones(1,n-1); d=[-(2+K*h^2/D)*ones(1,n)];
b=[zeros(1,n-1), -2*h*C/D];Y = tridiag(l,d,u,b);
%Using a second order approx at x=1
%l=[ones(1,n-2),-4];u=ones(1,n-1);d=[-(2+K*h^2/D)*ones(1,n-1),3];
%b=[zeros(1,n-1), 2*h*C/D];
%M=zeros(n,n)+diag(d)+diag(l,-1)+diag(u,1);M(n,n-2)=1;
%Y=(M\b')';
p=flops;
X = 0:h:1; Y = [0, Y];
plot(X,Y,'b')
grid on, xlabel('x-axis'), ylabel('u-axis'), title('Example 2.7')
hold on
%Save values at the midpoint for error analysis
A=[A;n,p,X(n/2+1),Y(n/2+1)];
n = n*2;
end
hold off
A;
[nn,mm]=size(A);
diff(1)=0;ratio(1)=0; ratio(2)=0;
for i=1:nn-1,
diff(i+1)=A(i+1,4)-A(i,4);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,4) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)';A(:,4)'; diff; ratio; xacc];
fprintf(1,'\n\n n flops u(0.5) diff ratio u acc\n\n')
fprintf(1,'%3.0f %7.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
MAIN PROGRAM
clear, clf
L=3; a=1; b=2; Ha=1; Hb=2;s=-5;A=[];n=4;
for ii=1:8
flops(0)
%Initial Guess for Newton's Method
h=(b-a)/n; x=a-h:h:b+h;
y=(Hb-Ha)/((b-a)^2)*(x-a).^2+Ha+(s-(s/(b-a))*(x-a)).*(x-a);
%Newton's Method to solve the system
for i=1:8
r=DFHC(y,Ha,Hb,h,L)\(-FHC(y,Ha,Hb,h,L));
y=y+r';
end
p=flops;
xx=x(2:n+2);
yy=y(2:n+2);
plot(xx,yy)
grid on,hold on, xlabel('x axis'), ylabel('y axis')
A=[A;n,p,x(n/2+2),y(n/2+2)];
n=2*n;
end
[nn,mm]=size(A);
diff(1)=0;ratio(1)=0; ratio(2)=0;
for i=1:nn-1,
diff(i+1)=A(i+1,4)-A(i,4);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,4) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)';A(:,4)'; diff; ratio; xacc];
fprintf(1,'\n\n n flops y(1.5) diff ratio y acc\n\n')
fprintf(1,'%3.0f %8.0f %16.14f %13.10f %13.10f %16.14f\n',z)
FUNCTION SUBPROGRAM
function z=FHC(y,Ha,Hb,h,L)
m=length(y);
Lc=1/(4*L*L); hc=1/(h*h);
for i=1:m-2
z(i)=hc*(y(i+2)-2*y(i+1)+y(i))^2-Lc*((y(m)-y(m-2)-y(3)+y(1))^2)*(1+
(hc/4)*(y(i+2)-y(i))^2);
end
z(m-1)=y(2)-Ha;
z(m)=y(m-1)-Hb;
z=z';
function M=DFHC(y,Ha,Hb,h,L)
m=length(y);
Lc=1/(4*L*L); hc=1/(h*h);
for i=1:m-2
d(i)=2*hc*(y(i+2)-2*y(i+1)+y(i))+Lc*((y(m)-y(m-2)y(3)+y(1))^2)*(hc/2)*(y(i+2)-y(i));
u1(i)=-4*hc*(y(i+2)-2*y(i+1)+y(i));
u2(i)=2*hc*(y(i+2)-2*y(i+1)+y(i))-Lc*((y(m)-y(m-2)y(3)+y(1))^2)*(hc/2)*(y(i+2)-y(i));
k1(i)=-2*Lc*(y(m)-y(m-2)-y(3)+y(1))*(1+(hc/4)*(y(i+2)-y(i))^2);
k2(i)=2*Lc*(y(m)-y(m-2)-y(3)+y(1))*(1+(hc/4)*(y(i+2)-y(i))^2);
k3(i)=2*Lc*(y(m)-y(m-2)-y(3)+y(1))*(1+(hc/4)*(y(i+2)-y(i))^2);
k4(i)=-2*Lc*(y(m)-y(m-2)-y(3)+y(1))*(1+(hc/4)*(y(i+2)-y(i))^2);
end
M=diag([d,0,0])+diag([u1,0],1)+diag(u2,2);
for i=1:m-2
M(i,1)=k1(i)+M(i,1); M(i,3)=k2(i)+M(i,3); M(i,m-2)=k3(i)+M(i,m-2);
M(i,m)=k4(i)+M(i,m);
end
M(m-1,2)=1; M(m,m-1)=1;
TOP
BACK
Main
clear all
global func_f func_g
clf reset
%Initial step size h, starting and ending values of a<x<b
h=.125; a=0; b=5.3;
%enter righthand side function
func_f ='-9*(x-1).*y';
%func_f ='[y(2),sqrt(1+y(2)^2)]';
equation = ['dy/dx = ', func_f]
%enter the Jacobian of the righthand side
func_g = '-9*(x-1)'; ['Jacobian = ', func_g]
%func_g = '[0,1;0,y(2)/sqrt(1+y(2)^2)]'; ['Jacobian = ', func_g]
%Initial condition
ya=[1];
for j=1:6 flops(0)
[x,y] = feuler(a,b,h,ya);%choose the method
p=flops;
n=length(x);
A(j,3)=y(n,1); A(j,2)=p; A(j,1)=h;
h=h/2;
plot(x,y(:,1),'b')
grid on
xlabel('x-axis')
ylabel('y-axis')
title(equation)
hold on
end
%error estimation
hold off
[nn,mm]=size(A);diff=zeros(1,nn); ratio=zeros(1,nn); xacc=zeros(1,nn);
for i=1:nn-1,
diff(i+1)=A(i+1,3)-A(i,3);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,3) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)';A(:,3)'; diff; ratio; xacc];
fprintf(1,'\n\n h flops y diff ratio y acc\n\n')
fprintf(1,'%7.5f %7.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
RUNGE-KUTTA
Forward Euler
function [u,v] = feuler(a,b,h,ya)
u= (a:h:b)';
n=length(u);
v=ya(:)';
for i=1:n-1,
v(i+1,:) = v(i,:) + h*Ode_function(u(i),v(i,:));
end
Heun
function [u,v] = heun(a,b,h,ya)
u= (a:h:b)';
n=length(u);
v=ya(:)';
for i=1:n-1,
w= v(i,:) + h*Ode_function(u(i),v(i,:));
v(i+1,:) = v(i,:)+ (.5)*h*(Ode_function(u(i),v(i,:))
+Ode_function(u(i+1),w));
end
4th Order RK
function [u,v] = rk4(a,b,h,ya)
u= (a:h:b)';
n=length(u);
v=ya(:)';
for i=1:n-1,
u0=u(i); h2=h/2;
v0=v(i,:); f0=Ode_function(u0,v0);
v1=v0+h2*f0; f1=Ode_function(u0+h2,v1);
v2=v0+h2*f1; f2=Ode_function(u0+h2,v2);
v3=v0+h*f2; f3=Ode_function(u0+h,v3);
v(i+1,:) = v0+(h/6)*(f0+2*f1+2*f2+f3);
end
TOP
BACK
v=ya(:)';
for i=1:n-1,
p=v(i,:)+h*Ode_function(u(i),v(i,:));
for j =1:2
c = v(i,:)+h*Ode_function(u(i+1),p);
J = h*Jacobian_function(u(i+1),p) - eye(m);
V = J\(p-c)';
v(i+1,:) = p + V';
p=v(i+1,:);
end
end
TOP
BACK
if errest~=0.0
h=min(hmax,0.9*h*sqrt(tolerr/errest));
end
end
if (x<b)
display('Integration did not reach end point requested')
x
end
[count, h, x, y, errest, tolerr]
subplot(2,1,1)
plot(xout,yout(:,1)')
title(equation)
grid on, xlabel('x-axis'), ylabel('y-axis')
subplot(2,1,2)
plot(xout,sout)
grid on, xlabel('x-axis'), ylabel('step size')
TOP
BACK
end
%Compute Beginning Diffs
X=xout; Y=yout;
D1(1,:)=(Y(2,:)-Y(1,:));D1(2,:)=(Y(3,:)-Y(2,:));
D2(1,:)=(D1(2,:)-D1(1,:));
count=1; index=3;
%main loop
while (X(index)<b) & (h>hmin)& (count<7500)
count=count+1;
%calculate the BDF2 step
pp= Y(index,:)+D1(index-1,:)+D2(index-2,:);
t=1; j=1; p=pp;
while (t > 1e-16) & (j<6)
c= (4/3)*Y(index,:)-(1/3)*Y(index-1,:)+(2/3)*h*Ode_function(X(index)
+h,p);
J=2/3*h*Jacobian_function(X(index)+h,p) - eye(m);
p1= (J\(p-c)')' + p;
t= norm(p1-p,'inf');
j=j+1; p=p1;
end
%calculate the error estimate
errest=(2/11)*norm((pp-p),'inf');
%use rel err if |y|>1 and abs err if |y|<1
tolerr=tol*max(norm(p,'inf'),1.0);
%take step if true
if errest<=tolerr
X(index+1)=X(index)+h; xout=[xout,X(index+1)];
Y(index+1,:)=p; yout=[yout;Y(index+1,:)]; sout=[sout,h];
%calculate possible new step
hnew=(tolerr/errest)^(1/3)*h;
if hnew>2*h & index>4 & 2*h < hmax %remesh at 2h if true
h=2*h;
X(1)=X(index-4);X(2)=X(index-2);X(3)=X(index);
Y(1,:)=Y(index-4,:);Y(2,:)=Y(index-2,:);Y(3,:)=Y(index,:);
D1(1,:)=(Y(2,:)-Y(1,:));D1(2,:)=(Y(3,:)-Y(2,:));
D2(1,:)=(D1(2,:)-D1(1,:)); index=3;
else
D1(index,:)=(Y(index+1,:)-Y(index-1,:)); D2(index1,:)=(D1(index,:)-D1(index-1,:));
index=index+1;
end
else
%remesh at h/2
h=h/2;
X(1)=X(index-1);X(2)=X(1)+h;X(3)=X(index);
Y(1,:)=Y(index-1,:);Y(2,:)=Y(index,:)-D1(index-1,:)/2-D2(index2,:)/8;Y(3,:)=Y(index,:);
D1(1,:)=(Y(2,:)-Y(1,:));D1(2,:)=(Y(3,:)-Y(2,:));
D2(1,:)=(D1(2,:)-D1(1,:)); index=3;
end
end %end of main loop
if (X(index)<b)
display('Integration did not reach end point requested')
X(index)
end
[count, h, index, errest, tolerr]
subplot(2,1,1)
plot(xout,yout(:,1)')
%plot(yout(:,1),yout(:,2))
title(equation)
TOP
BACK
ADAMS METHODS
TOP
BACK
set(h,'linewidth',2,'edgecolor','g')
%colormap([0,0,1])
hold on
%Heun's Method
M=abs(1+Z/2.*(2+Z));
[c,h]=contour(X,Y,M,[1,1]);
set(h,'linewidth',2,'edgecolor','r')
%RK4
M=abs(1+Z/6.*(1+Z.*(1+Z/2.*(1+Z/2))));
[c,h]=contour(X,Y,M,[1,1]);
set(h,'linewidth',2,'edgecolor','k')
grid on
axis equal
title('Runge-Kutta A-Stabiity Regions')
%RKF4
M=abs(1+Z.*(1+Z/2.*(1+Z/3.*(1+Z/4.*(1+Z/(13/3))))));
[c,h]=contour(X,Y,M,[1,1]);
set(h,'linewidth',2,'edgecolor','b')
legend('Eulers Method', 'Heuns Method','RK4','RKF4')
%Computation of the A-Stability Region(BDF1 and BDF2)
clear, clf reset
x=linspace(-3,3,21);
y=linspace(-3,3,21);
[X,Y]=meshgrid(x,y);
Z=X +Y*i;
%Backward Euler's Method
M=abs(1./(1-Z));
mesh(X,Y,M)
rotate3d on
colormap('copper')
axis([-3,3,-3,3,0,10])
title('Stability Surface for Backward Euler')
%BDF2 Method
M=abs((2+sqrt(1+2*Z))./(3-2*Z));
figure(2)
clf
mesh(X,Y,M)
rotate3d on
colormap('copper')
axis([-3,3,-3,3,0,10])
title('Stability Surface for BDF2 (first root)')
M=abs((2-sqrt(1+2*Z))./(3-2*Z));
figure(3)
clf
mesh(X,Y,M)
rotate3d on
colormap('copper')
axis([-3,3,-3,3,0,1])
title('Stability Surface for BDF2 (second root)')
TOP
BACK
LAPLACE'S EQUATION
Class Example: System Matrix is explicitly calculated
clear all
%Irregular Geometry Finite Diff Elliptic PDE Example
h=0.5; n=9; maxer=1; count=0;
%Molecule vertex list
M=[15,2,5,2;16,3,6,1;17,4,7,2;3,8,8,3;1,6,10,6;2,7,11,5;3,8,12,6;4,9,13,7;8
,14,14,8]
A=zeros(17,17);B=zeros(17,1);
for i=1:9
A(i,i)=-4;
for j=1:4
A(i,M(i,j))=A(i,M(i,j))+1;
end
end
%Boundary Conditions
for i=10:13
A(i,i)=1;A(i,i-9)=-1;A(i,i-5)=2*h*0.2; B(i)=2*h*0.2*20;
end
A(14,14)=1;A(14,8)=-1;A(14,9)=2*h*0.2; B(14)=2*h*0.2*20;
for i=15:17
A(i,i)=1; B(i)=100;
end
fprintf('\n %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f
%4.1f %4.1f %4.1f %4.1f %4.1f %4.1f %4.1f \n',A')
B
u=(A\B)'
%plot the result
x=0:.5:2;
y=0:.5:1;
z=[100,100,100,u(3),u(7);u(1:4),u(8);u(5:9)]
hold off
surf(x,y,z)
view(160,20)
shading interp
rotate3d on
colorbar
maxer=max(maxer,abs(r-u(i)));
u(i)=r;
end
for i=1:4
u(9+i)=2*h*(0.2)*(20-u(i+4))+u(i);
end
u(14)=2*h*(0.2)*(20-u(9))+u(8);
end
count, flops
u(1:9);
%plot the result
x=0:.5:2;
y=0:.5:1;
z=[100,100,100,u(3),u(7);u(1:4),u(8);u(5:9)]
hold off
surf(x,y,z)
view(160,20)
shading interp
rotate3d on
colorbar
Using SOR
clear
%Irregular Geometry Finite Diff Elliptic PDE Example
h=0.25; n=30; maxer=1; count=0;w=1.65
%Molecule vertex list
M=[40 2 7 2;41 3 8 1;42 4 9 2;43 5 10 3; 44 6 11 4; 5 12 12 5;
1 8 14 8;2 9 15 7;3 10 16 8;4 11 17 9; 5 12 18 10;6 13 19 11;12 20 20 12;
7 15 22 15;8 16 23 14;9 17 24 15;10 18 25 16;11 19 26 17;12 20 27 18;13 21
28 19;20 29 29 20;
14 23 31 23;15 24 32 22;16 25 33 23;17 26 34 24;18 27 35 25;19 28 36 26;20
29 37 27;21 30 38 28;29 39 39 29]
%Initialize solution
u=60*ones(1,44);
u(40)=100; u(41)=100; u(42)=100; u(43)=100; u(44)=100;
%Solve the linear system with SOR (Gauss-Seidel=> w=1)
flops(0);
while maxer > .00001
count=count+1;
maxer=0;
for i=1:30
%r=(u(M(i,1))+u(M(i,2))+u(M(i,3))+u(M(i,4)))/4;
r=(1-w)*u(i)+w*(u(M(i,1))+u(M(i,2))+u(M(i,3))+u(M(i,4)))/4;
maxer=max(maxer,abs(r-u(i)));
u(i)=r;
end
for i=1:8
u(30+i)=2*h*(0.2)*(20-u(i+21))+u(i+13);
end
u(39)=2*h*(0.2)*(20-u(30))+u(29);
end
count,flops
u;
%plot the result
x=0:.25:2;
y=0:.25:1;
z=[100,100,100,100,100,u(5),u(11),u(18),u(26);u(1:6),u(12),u(19),u(27);u(7:
13),u(20),u(28);
u(14:21),u(29);u(22:30)]
hold off
surf(x,y,z)
shading interp
view(150,20)
colorbar
rotate3d
view(160,20)
76 93 109 93;77 94 110 92;78 95 111 93;79 96 112 94;80 97 113 95;81 98 114
96;82 99 115 97;83 100 116 98;84 101 117 99;85 102 118 100;86 103 119
101;87 104 120 102;88 105 121 103;89 106 122 104;90 107 123 105;91 108 124
106;107 125 125 107];
%Initialize solution
u=80*ones(1,134);
u(126:134)=100;
%Solve the linear system with SOR (Gauss-Seidel=> w=1)
flops(0);
while maxer > .00001
count=count+1;
maxer=0;
for i=1:108
r=(1-w)*u(i)+w*(u(M(i,1))+u(M(i,2))+u(M(i,3))+u(M(i,4)))/4;
maxer=max(maxer,abs(r-u(i)));
u(i)=r;
end
%[count, maxer]
for i=92:107
u(i+17)=2*h*(0.2)*(20-u(i))+u(i-16);
end
u(125)=2*h*(0.2)*(20-u(108))+u(107);
end
count,flops
u;
%plot the result
x=0:.125:2;
y=0:.125:1;
z=[100,100,100,100,100,100,100,100,100,u(9),u(19),u(30),u(42),u(55),u(69),u
(84),u(100);
u(1:10),u(20),u(31),u(43),u(56),u(70),u(85),u(101);
u(11:21),u(32),u(44),u(57),u(71),u(86),u(102);
u(22:33),u(45),u(58),u(72),u(87),u(103);
u(34:46),u(59),u(73),u(88),u(104);
u(47:60),u(74),u(89),u(105);
u(61:75),u(90),u(106);
u(76:91),u(107);
u(92:108)]
hold off
surf(x,y,z)
shading interp
view(150,20)
colorbar
rotate3d
TOP
BACK
HEAT EQUATION
General 1-dimensional Heat Equation Solver using three different methods for comparison
%Heat Equation Solvers
clear, clf reset
%input parameters
a=0, b=1, k=1, Tfinal=.1, n=20
Const=5 %C=k*DeltaT/DeltaX^2
DeltaX=(b-a)/n
DeltaT=(Const*DeltaX^2)/k
% Initial condition
Uinitial=inline('exp(-((x-(b-a)/2)*100).^2)','x','a','b')
%Uinitial=inline('0*x+1','x','a','b')
%Boundary Conditions
Uleft=inline('0*t','t')
Uright=inline('0*t','t')
%Uleft=inline('0*t+1','t')
%Uright=inline('cos(8*t)','t')
%begin algorithm
t=0; count=1; X=linspace(a,b,n+1);T=[t];
Uout(:,1)=Uinitial(X,a,b)';
while 0
%Crank-Nicolson Matrices
A=diag((-Const/2)*ones(n-2,1),-1)+diag((1+Const)*ones(n-1,1))+diag((Const/2)*ones(n-2,1),1);
B=diag((Const/2)*ones(n-2,1),-1)+diag((1-Const)*ones(n-1,1))
+diag((Const/2)*ones(n-2,1),1);
C=inv(A); D=C*B; MaxEigenvalue=norm(eig(D),'inf')
while T(count)<Tfinal & count<1000
%Crank-Nicolson
U=C*(B*Uout(2:n,count) + (Const/2)*[Uleft(T(count))+Uleft(T(count)
+DeltaT);zeros(n-3,1);...
Uright(T(count))+Uright(T(count)+DeltaT)]);
%update solution
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
end
end
while 0
%Leap Frog Matrices
A=diag((-Const)*ones(n-2,1),-1)+diag((1+2*Const)*ones(n-1,1))+diag((Const)*ones(n-2,1),1);
B=2*Const*(diag(ones(n-2,1),-1)+diag((-2)*ones(n-1,1))+diag(ones(n2,1),1));
%Start-up
U=A\(Uout(2:n,1)+Const*[Uleft(DeltaT);zeros(n-3,1);Uright(DeltaT)]);
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
while T(count)<Tfinal & count<1000
%Leap Frog
U=B*Uout(2:n,count)+Uout(2:n,count1)+(2*Const)*[Uleft(T(count));zeros(n-3,1);Uright(T(count))];
%update solution
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
end
end
%while 0
%BDF Matrices
A=diag((-Const)*ones(n-2,1),-1)+diag((1+2*Const)*ones(n-1,1))+diag((Const)*ones(n-2,1),1);
B=(-2/3*Const)*diag(ones(n-2,1),-1)+diag((1+4*Const/3)*ones(n-1,1))+(2/3*Const)*diag(ones(n-2,1),1);
C=inv(B);
%Start-up
U=A\(Uout(2:n,1)+Const*[Uleft(DeltaT);zeros(n-3,1);Uright(DeltaT)]);
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
while T(count)<Tfinal & count<1000
%BDF Method
U=C*(4/3*Uout(2:n,count)-1/3*Uout(2:n,count1)+(2*Const/3)*[Uleft(T(count)+DeltaT);zeros(n-3,1);Uright(T(count)
+DeltaT)]);
%update solution
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
end
%end
count
surf(X,T,Uout'),xlabel('x-axis'),ylabel('t-axis')
colormap([.9,.9,.9])
rotate3d on
%shading interp
TOP
BACK
WAVE EQUATION
%Wave Equation Solvers
clear, clf reset
%input parameters
a=0, b=1, c=1, Tfinal=2, n=200
DeltaX=(b-a)/n
DeltaT=.005
Const=(c*DeltaT/DeltaX)^2
% Initial condition
Uinitial=inline('exp(-((x-(b+a)/2)*100).^2)','x','a','b')
dUinitial=inline('0*x','x','a','b')
%Boundary Conditions
Uleft=inline('0*t','t')
Uright=inline('0*t','t')
%begin algorithm
t=0; count=1; X=linspace(a,b,n+1);T=[t];
Uout(:,1)=Uinitial(X,a,b)'; Temp=dUinitial(X,a,b)';dU(:,1)=Temp(2:n);
%Leap Frog Matrices
A=diag((Const)*ones(n-2,1),-1)+diag((2-2*Const)*ones(n-1,1))
+diag((Const)*ones(n-2,1),1);
%Start-up
U=Uout(2:n,1)+DeltaT*dU(:,1);
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
while T(count)<Tfinal & count<1000
%Leap Frog
U=A*Uout(2:n,count)-Uout(2:n,count-1)+
(Const)*[Uleft(T(count));zeros(n-3,1);Uright(T(count))];
%update solution
T=[T,T(count)+DeltaT]; count=count+1;
Uout(:,count)=[Uleft(T(count));U;Uright(T(count))];
end
count
for j=1:length(T)
figure(1)
plot(X,Uout(:,j)','b')
axis([a,b,-1,1])
text(.3,-.6,['t= ',num2str(T(j),3)])
M(j) = getframe;
end
movie(M,1)
figure(2)
surf(X,T,Uout')
%colormap([.9,.9,.9])
rotate3d on
shading interp
TOP
BACK
LINEAR SUBDOMAIN
Code for Example 3.1 followed by Code for Example 3.2
%Solves Example 3.1 Using Subdomain Method
clear all, clf reset
% n=#of nodes
n=3, A=[];
for i=1:6
deltax=1/(n-1)
% Build the n by n Tridiagonal System
LowerD= (-1/2+deltax/8)*ones(1,n-1);
MainD=[1, (3/4)*deltax*ones(1,n-2), 3/8*deltax + 1/2];
UpperD=[0, (1/2+deltax/8)*ones(1,n-2)];
B=[2, deltax*ones(1,n-2), deltax/2];
u=tridiag(LowerD, MainD, UpperD, B);
x=linspace(0,1,n);
plot(x,u)
xlabel('x-axis'), ylabel('u-axis'), title('Example 3.1')
grid on, hold on
A=[A;n,u(n)];
n=(n-1)*2+1
end
hold off
[nn,mm]=size(A);
diff(1)=0;ratio(1)=0; ratio(2)=0;
for i=1:nn-1,
diff(i+1)=A(i+1,2)-A(i,2);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,2) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)'; diff; ratio; xacc];
fprintf(1,'\n\n n y diff ratio y acc\n\n')
fprintf(1,'%3.0f %16.14f %13.10f %13.10f %16.14f\n',z)
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=A(i-1,2) + diff(i)/(1-ratio(i+1));
end
z=[A(:,1)';A(:,2)'; diff; ratio; xacc];
fprintf(1,'\n\n n y diff ratio y acc\n\n')
fprintf(1,'%3.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
COLLOCATION
Code for the equation du/dx + u =1, u(0) = 2 using Piecewise Linear Basis Functions
clear
hold off
a=0, b=1, n=6, u0=2
Dx=(b-a)/(n-1)
d=[1,(1/2+1/Dx)*ones(1,n-1)]
l=(1/2-1/Dx)*ones(1,n-1)
%build the matrix A
A=diag(d)+diag(l,-1)
B=[u0, ones(1,n-1)]
y=A\B'
x=linspace(a,b,n)
plot(x,y)
grid on
for i=1:n-1,
M=hcoll(x(i),x(i+1));
A(2*i:2*i+1,2*i-1:2*i+2)=M;
end
u = A\B;
for i=1:n,
v(i)=u(2*i-1); dv(i)=u(2*i);
end
[x',v'];
hold on
pchpltv2(x,v,dv)
grid on, xlabel('r axis'), ylabel('temperature axis')
AA=[AA;n,v((n+1)/2)];
nn=(nn-1)*2+1
end
hold off
[nn,mm]=size(AA);
diff(1)=0;ratio(1)=0; ratio(2)=0;
for i=1:nn-1,
diff(i+1)=AA(i+1,2)-AA(i,2);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=AA(i-1,2) + diff(i)/(1-ratio(i+1));
end
z=[AA(:,1)';AA(:,2)'; diff; ratio; xacc];
fprintf(1,'\n\n n y diff ratio y acc\n\n')
fprintf(1,'%3.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
GALERKIN
%PLGalerkin for the Linear Second Order Equation
%cC d2u/dx2 + cB du/dx +cA u = f on the interval [a,b]
clear, clf reset
a=0, b=1, nn=3, AA=[]; cC=1,cB=0,cA=1
%Master element constants
I=[2/3 1/3;1/3 2/3]; DI = [-1/2 1/2; -1/2 1/2]; DD = [1/2 -1/2; -1/2 1/2];
diff(i+1)=AA(i+1,2)-AA(i,2);
end
for i=2:nn-1,
ratio(i+1)=diff(i+1)/diff(i);
xacc(i+1)=AA(i-1,2) + diff(i)/(1-ratio(i+1));
end
z=[AA(:,1)';AA(:,2)'; diff; ratio; xacc];
fprintf(1,'\n\n n y diff ratio y acc\n\n')
fprintf(1,'%3.0f %16.14f %13.10f %13.10f %16.14f\n',z)
TOP
BACK
TRIANGULAR GALERKIN
%TRIANGULARGalerkin
% coordinates of the nodes
clear, clf reset
p=pi/12;
x=[1 2 cos(2*p) 3/2 2*cos(p) 2*cos(2*p) 1 2*cos(3*p) cos(4*p) 1/2
2*cos(4*p) 2*cos(5*p) 0 0];
y=[0 0 sin(2*p) 1/2 2*sin(p) 2*sin(2*p) 1 2*sin(3*p) sin(4*p) 3/2
2*sin(4*p) 2*sin(5*p) 1 2];
nn=length(x);
%incidence matrix
M=[1 2 4;1 4 3;2 5 4;3 4 7;4 6 7;4 5 6;3 7 9;7 6 8;9 7 10;7 11 10;7 8 11;9
10 13;10 11 12;13 10 14;10 12 14];
[n,r]=size(M);
%Step #1 set A matrix and B vector to all zeros
A = zeros(nn); B = diag(A);
%Step #2 build A matrix for each element
for i=1:n,
xi=[x(M(i,:))]; yi=[y(M(i,:))];
a=det([1 1 1;xi;yi])/2;
Mi=[M(i,:) M(i,:)]; xi=[xi xi xi]; yi=[yi yi yi];
for j=1:3,
for k=0:2,
l=j+k;
A(Mi(j),Mi(l))=A(Mi(j),Mi(l))-...
((yi(j+1)-yi(j+2))*(yi(l+1)-yi(l+2))+(xi(j+2)-xi(j+1))*(xi(l+2)-xi(l+1)))/
(4*a);
end
end
end
%Step #3 build the B vector
%Step #4 correct for the boundary conditions
%type 1 boundary conditions (replace that row with the condition equation)
Z=zeros(1,nn); B=Z';
A(1,:)=Z; A(1,1)=1; B(1)=2;A(2,:)=Z; A(2,2)=1; B(2)=3;
A(3,:)=Z; A(3,3)=1; B(3)=2;A(9,:)=Z; A(9,9)=1; B(9)=2;
A(13,:)=Z; A(13,13)=1; B(13)=2;A(14,:)=Z; A(14,14)=1; B(14)=3;
%type 2 boundary conditions (adjust the B vector)
L=sqrt((x(2)-x(5))^2+(y(2)-y(5))^2);
B(5)=B(5)-L; B(6)=B(6)-L;B(8)=B(8)-L; B(11)=B(11)-L;B(12)=B(12)-L;
%type 3 boundary conditions (adjust both the B vector and A matrix)
%A(1,1)=A(1,1)-.125; A(1,2)=A(1,2)-.125; B(1)=B(1)-.25;%Example #4
%A(2,1)=A(2,1)-.125; A(2,2)=A(2,2)-.125; B(2)=B(2)-.25;
%Step #5 solve the system
u=A\B;
[x;y;u'], spy(A), pause
trisurf(M,x,y,zeros(1,nn),'EdgeColor','g','LineWidth',2)
hold on
trisurf(M,x,y,u','FaceColor','interp','EdgeColor','b','LineWidth',1)
xlabel('x axis'),ylabel('y axis'),zlabel('u axis'), colorbar
grid on, rotate3d
TOP
BACK
for i=1:length(x)
hold on
text(x(i),y(i),num2str(i),'fontweight','bold')
end
TOP
BACK
Revised
By Don Short, [email protected]