0% found this document useful (0 votes)
68 views

College of Engineering: NGN-509 (Computational Methods For Engineers)

This document contains MATLAB code and results for solving a second order differential equation using two numerical methods: (1) the Shooting Method and (2) the finite difference method. For both methods, the code approximates the solution of the equation y''= 0.15*y for 0<=x<=10 with boundary conditions y(0)=240 and y(10)=150. The code then compares the numerical solutions to an analytical solution and plots the results.
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)
68 views

College of Engineering: NGN-509 (Computational Methods For Engineers)

This document contains MATLAB code and results for solving a second order differential equation using two numerical methods: (1) the Shooting Method and (2) the finite difference method. For both methods, the code approximates the solution of the equation y''= 0.15*y for 0<=x<=10 with boundary conditions y(0)=240 and y(10)=150. The code then compares the numerical solutions to an analytical solution and plots the results.
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/ 9

College of Engineering

NGN-509 (Computational Methods for Engineers)

Homework-6

Fall 2019

Instructor: Dr. Nai-Shyong Yeh

Submission Date: 7/12/2019

Name ID

Rana M. Nauman Javed 81862


1c) MATLAB code using the Shooting Method with step size ∆x = 1
% Matlab code using the Shooting Method with step size = 1
% Approximate the solution of y"= 0.15*y ;
% for 0<=x<=10 with y(0)=240 and y(10)=150 ;
% Compare the Above equation with y" = p(x)*y'+ q(x)*y + r(x);
p = @(x) 0;
q = @(x) 0.15;
r = @(x) 0;

a=0; b=10; alpha =240; beta = 150; n = 10;

fprintf(' x w\n');
h = (b-a)/n;
u1 = alpha;
u2 = 0;
v1 = 0;
v2 = 1;
u = zeros(2,n);
v = zeros(2,n);

for i = 1 : n
x = a+(i-1)*h;
t = x+0.5*h;
k11 = h*u2;
k12 = h*(p(x)*u2+q(x)*u1+r(x));
k21 = h*(u2+0.5*k12);
k22 = h*(p(t)*(u2+0.5*k12)+q(t)*(u1+0.5*k11)+r(t));
k31 = h*(u2+0.5*k22);
k32 = h*(p(t)*(u2+0.5*k22)+q(t)*(u1+0.5*k21)+r(t));
t = x+h;
k41 = h*(u2+k32);
k42 = h*(p(t)*(u2+k32)+q(t)*(u1+k31)+r(t));
u1 = u1+(k11+2*(k21+k31)+k41)/6;
u2 = u2+(k12+2*(k22+k32)+k42)/6;
k11 = h*v2;
k12 = h*(p(x)*v2+q(x)*v1);
t = x+0.5*h;
k21 = h*(v2+0.5*k12);
k22 = h*(p(t)*(v2+0.5*k12)+q(t)*(v1+0.5*k11));
k31 = h*(v2+0.5*k22);
k32 = h*(p(t)*(v2+0.5*k22)+q(t)*(v1+0.5*k21));
t = x+h;
k41 = h*(v2+k32);
k42 = h*(p(t)*(v2+k32)+q(t)*(v1+k31));
v1 = v1+(k11+2*(k21+k31)+k41)/6;
v2 = v2+(k12+2*(k22+k32)+k42)/6;
u(1,i) = u1;
u(2,i) = u2;
v(1,i) = v1;
v(2,i) = v2;
end
w1 = alpha;
z = (beta-u(1,n))/v(1,n);
x = a;
i = 0;
fprintf('%5.4f %11.8f \n', x, w1);

for i = 1 : n
x = a+i*h;
w1 = u(1,i)+z*v(1,i);
w2 = u(2,i)+z*v(2,i);
fprintf('%5.4f %11.8f \n', x, w1);
end
y1 = w;
x1 = [0:1:10]
b = 0.3872*x1;
y2 = 3.016944*exp(b)+(236.9831*exp(-1*b))
plot (x,y1,x1,y2);

 Comparison of MATLAB results with Analytical Solution

x T (Matlab) T (Analytical)
0 240 240.000044
1 165.3461279 165.3444385
2 115.7927381 115.7891642
3 83.81747435 83.81141705
4 64.56646002 64.55677289
5 55.1174742 55.10225501
6 54.03637147 54.0126078
7 61.15941058 61.12241595
8 77.56845472 77.51099327
9 105.7552873 105.6662294
10 150 149.8622673

 Plot
As both Matlab and analytical solution are quite same so both graphs coincide very well. X-axis is for
x and Y-axis is for T values.
2b) MATLAB code using the finite difference approach

% Finite difference method


% Approximate the solution of y"= 0.15*y ;
% for 0<=x<=10 with y(0)=240 and y(10)=150 ;
% Compare the Above equation with y" = p(x)*y'+ q(x)*y + r(x);
p = @(x) 0;
q = @(x) 0.15;
r = @(x) 0;
aa = 0; bb = 10; alpha = 240; beta = 150; n=9;
% h = (bb-aa)/(n+1); h=1
fprintf(' x w \n');
h = (bb-aa)/(n+1);
a = zeros(1,n+1);
b = zeros(1,n+1);
c = zeros(1,n+1);
d = zeros(1,n+1);
l = zeros(1,n+1);
u = zeros(1,n+1);
z = zeros(1,n+1);
w = zeros(1,n+1);
x = aa+h;
a(1) = 2+h^2*q(x);
b(1) = -1+0.5*h*p(x);
d(1) = -h^2*r(x)+(1+0.5*h*p(x))*alpha;
m = n-1;

for i = 2 : m
x = aa+i*h;
a(i) = 2+h^2*q(x);
b(i) = -1+0.5*h*p(x);
c(i) = -1-0.5*h*p(x);
d(i) = -h^2*r(x);
end

x = bb-h;
a(n) = 2+h^2*q(x);
c(n) = -1-0.5*h*p(x);
d(n) = -h^2*r(x)+(1-0.5*h*p(x))*beta;
l(1) = a(1);
u(1) = b(1)/a(1);
z(1) = d(1)/l(1);

for i = 2 : m
l(i) = a(i)-c(i)*u(i-1);
u(i) = b(i)/l(i);
z(i) = (d(i)-c(i)*z(i-1))/l(i);
end

l(n) = a(n)-c(n)*u(n-1);
z(n) = (d(n)-c(n)*z(n-1))/l(n);
w(n) = z(n);

for j = 1 : m
i = n-j;
w(i) = z(i)-u(i)*w(i+1);
end
i = 0;
fprintf('%5.4f %11.8f\n', aa, alpha);
for i = 1 : n
x = aa+i*h;
fprintf('%5.4f %11.8f\n', x, w(i));
end
i = n+1;
fprintf('%5.4f %11.8f\n', bb, beta);
y1 = w;
x1 = [0:1:10]
b = 0.3872*x1;
y2 = 3.016944*exp(b)+(236.9831*exp(-1*b))
plot (x,y1,x1,y2);

 Comparison of MATLAB results with Analytical Solution


x T (Matlab) T (Analytical)
0 240 240.000044
1 165.7573029 165.3444385
2 116.3782013 115.7891642
3 84.45582981 83.81141705
4 65.20183283 64.55677289
5 55.72811076 55.10225501
6 54.61360532 54.0126078
7 61.69114067 61.12241595
8 78.02234712 77.51099327
9 106.0569056 105.6662294
10 150 149.8622673

 Plot
As both Matlab and analytical solution are quite same so both graphs coincide very well. X-axis is for
x and Y-axis is for T values.

You might also like