NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false point,x=g(x)

13
Islamic Azad University Qazvin Branch Faculty of Industrial and Mechanics , Department of Mechanical Engineering Subject Compare Some Algorithms for Solving Nonlinear Equation Thesis Advisor Dr.Marufi By Parham Sagharichi Ha

Transcript of NUMERICAL METHODS WITH MATLAB : bisection,mueller's,newton-raphson,false point,x=g(x)

Islamic Azad University

Qazvin Branch

Faculty of Industrial and Mechanics , Department of Mechanical

Engineering

Subject

Compare Some Algorithms for Solving Nonlinear Equation

Thesis Advisor

Dr.Marufi

By

Parham Sagharichi Ha

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 1

Problem

The speed v of a Saturn V rocket in vertical flight near the surface of earth can

be approximated by

๐‘ฃ = ๐‘ข ln๐‘€0

๐‘€0 โˆ’ ๏ฟฝฬ‡๏ฟฝ๐‘กโˆ’ ๐‘”๐‘ก

๐‘ข = 2510๐‘š

๐‘ = ๐‘ฃ๐‘’๐‘™๐‘œ๐‘๐‘–๐‘ก๐‘ฆ ๐‘œ๐‘“ ๐‘’๐‘ฅโ„Ž๐‘Ž๐‘ข๐‘ ๐‘ก ๐‘Ÿ๐‘’๐‘™๐‘Ž๐‘ก๐‘–๐‘ฃ๐‘’ ๐‘ก๐‘œ ๐‘กโ„Ž๐‘’ ๐‘Ÿ๐‘œ๐‘๐‘˜๐‘’๐‘ก

๐‘€0 = 2.8 โˆ— 106๐‘˜๐‘” = ๐‘š๐‘Ž๐‘ ๐‘  ๐‘œ๐‘“ ๐‘Ÿ๐‘œ๐‘๐‘˜๐‘’๐‘ก ๐‘Ž๐‘ก ๐‘™๐‘–๐‘“๐‘ก๐‘œ๐‘“๐‘“

๏ฟฝฬ‡๏ฟฝ = 13.3 โˆ— 103๐‘˜๐‘”

๐‘ = ๐‘Ÿ๐‘Ž๐‘ก๐‘’ ๐‘œ๐‘“ ๐‘“๐‘ข๐‘’๐‘™ ๐‘๐‘œ๐‘›๐‘ ๐‘ข๐‘š๐‘๐‘ก๐‘–๐‘œ๐‘›

๐‘” = 9.81๐‘š

๐‘ 2= ๐‘”๐‘Ÿ๐‘Ž๐‘ฃ๐‘–๐‘ก๐‘Ž๐‘ก๐‘–๐‘œ๐‘›๐‘Ž๐‘™ ๐‘Ž๐‘๐‘๐‘’๐‘™๐‘’๐‘Ÿ๐‘Ž๐‘ก๐‘–๐‘œ๐‘›

๐‘ก = ๐‘ก๐‘–๐‘š๐‘’

Determine the time when the rocket reaches the speed of sound (335 m/s).

Solution

๐‘ข ๐‘™๐‘›๐‘€0

๐‘€0 โˆ’ ๏ฟฝฬ‡๏ฟฝ๐‘กโˆ’ ๐‘”๐‘ก โˆ’ ๐‘ฃ = 0

Now we want to determine time in the above equation

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 2

Matlab

1) Bisection Method

Script :

clc

close all

clear all

%%

% Subject : Bisect Algorithm

% Author: Parham Sagharichi Ha Email :

[email protected]

%%

%-------------------S------T------A------R------T------------

-------------%

global tolerance

tolerance = 1e-4; % for example : 1e-4 = 10^-4

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

xlower = 0;

xupper = 100;

myfun = @(t)(u.*log(M0./(M0-mdot.*t))-g.*t-v);

[root,iflag] = fbisect(myfun,xlower,xupper);

switch iflag

case -2

disp('Initial range does not only contain one root')

otherwise

disp([' Root = ' num2str(root) ...

' found in ' num2str(iflag) ' iterations'])

end

%---------------F------I------N------I------S------H---------

-------------%

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 3

Function :

function [root,iflag] = fbisect(myfun,a,b)

if a>=b

disp(' attention b>a in [a b] ')

return

end

global tolerance

x = a:0.001:b;

y = feval(myfun,x);

fa = y(1);

fb = y(end);

ymax = max(y);

ymin = min(y);

figure

plot(x,y)

grid on

hold on

plot([a a],[ymin ymax])

plot([b b],[ymin ymax])

iflag = 0;

iterations = 0 ;

while (fa*fb<0) & (b-a)>tolerance

iterations = iterations + 1;

c = (a+b)/2;

fc = feval(myfun,c);

plot([c c],[ymin ymax])

pause

if fa*fc<0

b = c; fb = fc;

elseif fa*fc>0

a = c; fa = fc;

else

iflag = 1;

root = c

return

end

end

switch iterations

case 0

iflag = -2; root = NaN;

otherwise

iflag = iterations; root = c;

end

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 4

Result :

Root = 70.8779 found in 20 iterations

2) Linear Interpolation (False Position) Method :

Script :

clc

close all

clear all

%%

% Subject : False Postion Algorithm

% Author: Parham Sagharichi Ha Email :

[email protected]

%%

%-------------------S------T------A------R------T------------

-------------%

global tolerance

tolerance = 1e-4; % for example : 1e-4 = 10^-4

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

xlower = 0;

xupper = 100;

myfun = @(t)(u.*log(M0./(M0-mdot.*t))-g.*t-v);

[root,iflag] = finter(myfun,xlower,xupper);

switch iflag

case -2

disp('Initial range does not only contain one root')

otherwise

disp([' Root = ' num2str(root) ...

' found in ' num2str(iflag) ' iterations'])

end

%---------------F------I------N------I------S------H---------

-------------%

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 5

Function :

function [root,iflag] = finter(myfun,a,b)

if a>=b

disp(' attention b>a in [a b] ')

return

end

global tolerance

x = a:0.001:b;

y = feval(myfun,x);

fa = y(1);

fb = y(end);

ymax = max(y);

ymin = min(y);

figure

plot(x,y)

grid on

hold on

plot([a a],[ymin ymax])

plot([b b],[ymin ymax])

iflag = 0;

iterations = 0 ;

while (fa*fb<0) & (b-a)>tolerance

iterations = iterations + 1;

c = b - (fb)*(a-b)/(fa-fb);

fc = feval(myfun,c);

plot([c c],[ymin ymax])

pause

if fa*fc<0

b = c; fb = fc;

elseif fa*fc>0

a = c; fa = fc;

else

iflag = 1;

root = c

return

end

end

switch iterations

case 0

iflag = -2; root = NaN;

otherwise

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 6

iflag = iterations; root = c;

end

Result :

Root = 70.878 found in 24 iterations

3) Newton-Raphson Method :

Script :

clc

close all

clear all

%%

% Subject : Newton_Raphson Algorithm

% Author: Parham Sagharichi Ha Email :

[email protected]

%%

%-------------------S------T------A------R------T------------

-------------%

format short E

tolerance = 1e-4; % for example : 1e-4 = 10^-4

xlower = 0;

xupper = 100;

xguess = 45;

if (xguess>xupper)||(xlower>xguess)

disp(' error , repate again ')

return

end

xrange = xlower:0.1:xupper;

s = size(xrange);

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

syms x

myfun = u.*log(M0./(M0-mdot.*x))-g.*x-v;

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 7

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

for i = 1:s(2);

y(i) = double(subs(myfun,[x],[xrange(i)]));

end

fa = y(1);

fb = y(end);

ymax = max(y);

ymin = min(y);

figure

plot(xrange,y)

grid on

hold on

plot([xlower xlower],[ymin ymax])

plot([xupper xupper],[ymin ymax])

plot([xlower xupper],[0 0])

iflag = 0;

iterations = 1 ;

f = double(subs(myfun,[x],xguess));

myfun_prime = jacobian(myfun,x);

fprime = double(subs(myfun_prime,[x],xguess));

xn = xguess;

xnew = xn - f/fprime;

plot([xn xn],[0 f])

pause

plot([xn xnew],[f 0])

while (abs(xnew-xn)>tolerance) & (iterations<30)

iterations = iterations + 1;

xn = xnew;

f = double(subs(myfun,[x],xn));

fprime = double(subs(myfun_prime,[x],xn));

xnew = xn - f/fprime;

root = xnew;

pause

plot([xn xn],[0 f])

pause

plot([xn xnew],[f 0])

end

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 8

switch iterations

case 30

disp(' Not root found ');

otherwise

disp([' Root = ' num2str(root) ...

' found in ' num2str(iterations) ' iterations

'])

end

%---------------F------I------N------I------S------H---------

-------------%

Result :

Root = 70.878 found in 5 iterations

4) Muellerโ€™s Method :

Script :

clc

close all

clear all

%%

% Subject : Muellerโ€™s Algorithm

% Author: Parham Sagharichi Ha Email :

[email protected]

%%

%-------------------S------T------A------R------T------------

-------------%

tolerance = 1e-4; % for example : 1e-4 = 10^-4

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

xlower = 0;

xupper = 100;

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 9

xguess = 45;

if (xguess>xupper)||(xlower>xguess)

disp(' error , repate again ')

return

end

myfun = @(t)(u.*log(M0./(M0-mdot.*t))-g.*t-v);

x = [xlower xguess xupper]';%[x2 x0 x1]

xe = xlower:0.1:xupper;

ye = feval(myfun,xe);

ymax = max(ye);

ymin = min(ye);

figure

plot(xe,ye)

grid on

hold on

rline = plot([xlower xlower],[ymin ymax]);

mline = plot([xguess xguess],[ymin ymax]);

fline = plot([xupper xupper],[ymin ymax]);

pause

iterations = 0;

while (true)

iterations = iterations +1;

y = feval(myfun,x);%[f2 f0 f1]

h1 = x(3)-x(2);

h2 = x(2)-x(1);

gamma = h2/h1;

c = y(2);

a = (gamma*y(3)-y(2)*(1+gamma)+y(1))/(gamma*h1^2*(1+gamma));

b = (y(3)-y(2)-a*h1^2)/h1;

if b>0

root = x(2)-(2*c)/(b+sqrt(b^2-4*a*c));

else

root = x(2)-(2*c)/(b-sqrt(b^2-4*a*c));

end

pause

rootline = plot([root root],[ymin ymax]);

if root>x(2)

x = [x(2) root x(3)];

else

x = [x(1) root x(2)];

end

pause

delete(rootline)

delete(rline)

delete(mline)

delete(fline)

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 10

rline = plot([x(1) x(1)],[ymin ymax]);

mline = plot([x(2) x(2)],[ymin ymax]);

fline = plot([x(3) x(3)],[ymin ymax]);

if (abs(feval(myfun,root))<(10^-8))&(iterations<30)

break

end

end

switch iterations

case 30

disp(' Not root found ');

otherwise

disp([' Root = ' num2str(root) ...

' found in ' num2str(iterations) ' iterations

'])

end

Result :

Root = 70.878 found in 5 iterations

5) ๐‘ฅ = ๐‘”(๐‘ฅ) Method :

๐‘ข ๐‘™๐‘›๐‘€0

๐‘€0 โˆ’ ๏ฟฝฬ‡๏ฟฝ๐‘กโˆ’ ๐‘”๐‘ก โˆ’ ๐‘ฃ = 0

First Equation :

๐‘ก =๐‘ข

๐‘”๐‘™๐‘›

๐‘€0

๐‘€0 โˆ’ ๏ฟฝฬ‡๏ฟฝ๐‘กโˆ’

๐‘ฃ

๐‘”

Second Equation :

๐‘ก =๐‘€0

๏ฟฝฬ‡๏ฟฝ(exp (

๐‘”๐‘ก + ๐‘ฃ๐‘ข ) โˆ’ 1

exp (๐‘”๐‘ก + ๐‘ฃ

๐‘ข ))

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 11

Script :

clc

close all

clear all

%%

% Subject : x=g(x) Algorithm

% Author: Parham Sagharichi Ha Email :

[email protected]

%%

%-------------------S------T------A------R------T------------

-------------%

tolerance = 1e-4; % for example : 1e-4 = 10^-4

u = 2510;

M0 = 2.8*10^6;

mdot = 13.3*10^3;

g = 9.81;

v = 335;

xlower = 0;

xupper = 100;

xguess = 45;

if (xguess>xupper)||(xlower>xguess)

disp(' error , repate again ')

return

end

myfun1 = @(t)((u/g).*log(M0./(M0-mdot.*t))-v/g);

myfun2 = @(t)((M0/mdot).*(exp((g.*t+v)/u)-

1)./exp((g.*t+v)/u));

xold1 = xguess;

xnew1 = feval(myfun1,xold1);

iterations1 = 0;

while (abs(xnew1-xold1)>tolerance)&(iterations1<30)

iterations1 = iterations1 + 1;

xold1 = xnew1;

xnew1 = feval(myfun1,xold1);

Assignment of Numerical Analysis Parham Sagharichi Ha

[email protected] 12

end

root1 = xnew1(end);

switch iterations1

case 30

disp(' Not root found ');

otherwise

disp([' Root1 = ' num2str(root1) ...

' found in ' num2str(iterations1) ' iterations1

'])

end

xold2 = xguess;

xnew2 = feval(myfun2,xold2);

iterations2 = 0;

while (abs(xnew2-xold2)>tolerance)&(iterations2<30)

iterations2 = iterations2 + 1;

xold2 = xnew2;

xnew2 = feval(myfun2,xold2);

end

root2 = xnew2(end)

switch iterations2

case 30

disp(' Not root found ');

otherwise

disp([' Root2 = ' num2str(root2) ...

' found in ' num2str(iterations2) ' iterations2

'])

end

Result :

Not root found

root2 =

7.0878e+01

Root2 = 70.8779 found in 20 iterations2

References

Kiusalaas, J. (2009) Numerical Methods in Engineering with MATLABยฎ