Matlab Solution 1

20
EAB 2113 NUMERICAL METHOD Question 1 Develop an M-file to implement the bisection method. Using this program solve the following problem.  The velocity of falling par achutist is g iven as ) 1 ( ) ( ) / ( t m c e c  gm t v = . Where ) (t v = velocity of parachutist = s m / 40 ,  g = gravitational constant = 2 / 8 . 9 s m , m = the mass of the parachutist = kg 1 . 68 . Find the drag coefficient, c at the time 10 = t seconds using the initial bracket of the root as [13, 16] and iterate until 001 . 0 a ε  %. 1 % Data obtain from the question  f=inline('((9.81*68.1)/x)*(1-exp(-(x/68.1)*10))-40' ,'x'); xl=13; xu=16; es=0.001;  %computation  xr=xl; %initiation while(1) %since we dont know how many interation will take place xrold=xr; %keep the previous xr xr=(xl+xu)/2; %formula for bisection method  if xr~=0, ea=abs((xr-xrold)/xr)*100; end %calculate ea if xr not real answer  test=f(xl)*f(xr);  if test<0 xu=xr;  elseif test>0 xl=xr;  else ea=0;  end if ea<=es, break,end %end loop if the situation is satisfied end  format long; disp('the root for this equation is : ' ) disp(xr)  

Transcript of Matlab Solution 1

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 1/20

EAB 2113 NUMERICAL METHOD

Question 1

Develop an M-file to implement the bisection method. Using this program solve thefollowing problem.

 The velocity of falling parachutist is given as

)1()()/( t mce

c

 gmt v −−= .

Where )(t v = velocity of parachutist = sm /40 ,

 g = gravitational constant = 2/8.9 sm ,

m = the mass of the parachutist = kg 1.68 .

Find the drag coefficient, c at the time 10=t  seconds using the initial bracket

of the root as [13, 16] and iterate until 001.0≤aε   %.

1

% Data obtain from the question f=inline('((9.81*68.1)/x)*(1-exp(-(x/68.1)*10))-40','x');xl=13;xu=16;es=0.001; %computation xr=xl; %initiationwhile(1) %since we dont know how many interation will take place

xrold=xr; %keep the previous xr

xr=(xl+xu)/2; %formula for bisection method 

if xr~=0, ea=abs((xr-xrold)/xr)*100; end %calculate ea if xr not realanswer 

test=f(xl)*f(xr);  if test<0

xu=xr;  elseif test>0

xl=xr;  else ea=0;  end 

if ea<=es, break,end %end loop if the situation is satisfiedend format long;disp('the root for this equation is : ')disp(xr) 

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 2/20

EAB 2113 NUMERICAL METHOD

Question 2

2

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 3/20

EAB 2113 NUMERICAL METHOD

Develop an M-file to implement the false position method. solve the followingproblem.

 The velocity of falling parachutist is given as

)1()()/( t mce

c

 gmt v −−= .

Where )(t v = velocity of parachutist = sm /40 ,

 g = gravitational constant = 2/8.9 sm ,

m = the mass of the parachutist = kg 1.68 .

Find the drag coefficient, c at the time 10=t  seconds using the initial bracket

of the root as [13, 16] and iterate until 001.0≤aε   %.

3

f=inline('((9.81*68.1)/x)*(1-exp (-(x/68.1)*10))-40','x');

xl=13;xu=16;es=0.001; xr=xl while (1) 

xrold=xr;xr=xu-(((f(xu)*(xl-xu))/f(xl)-f(xu)));

 if xr~=0, ea=abs((xr-xrold)/xr)*100; endtest=f(xl)*f(xr);

  if test<0xu=xr;  elseif test>0

xl=xr;  else test>0

ea=0;  end if ea<=es, break, end endformat long; disp ('the root for this equation is : ')

disp(xr)

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 4/20

EAB 2113 NUMERICAL METHOD

Question 3

4

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 5/20

EAB 2113 NUMERICAL METHOD

Locate the root of   x x x  f   −= )sin(2)(  

(a) Using the MATLAB function fzero with an initial guess of  20 = x .

(b) Using Newton-Raphson method by writing a function M-file. Use an

initial guess of  5.00 = x and iterate until 001.0≤aε   %.

a)

5

function root=newraph1(f,df,xr,es)%f=inline('2*sin(sqrt(x))-x')%df=inline('-cos(sqrt(x))/sqrt(x)-1')while(1)

xr_old=xr;xr=xr-(f(xr)/df(xr));

  if xr~=0,ea=abs((xr-xr_old)/xr)*100;  end 

if ea<=es,break,endend fprintf('\n\nthe root for this question is %2.6f\n',xr);

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 6/20

EAB 2113 NUMERICAL METHOD

Question 4

6

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 7/20

EAB 2113 NUMERICAL METHOD

Develop an M-file to implement the modified secant method. Using this

program determine the loest positive root of  1)sin(8)( −= − xe x x  f   with an

initial guess of  3.00 = x and 01.0=δ   . Iterate until %000001.0=aε  .

Question 5

7

f=inline('8*sin(x)*exp(-x)-1','x')Ea=1;xo=0.3;fprintf('The initial guess is %.2f',xo) while(Ea>0.000001)

xold=xo;xo=xo-(0.01*xo*f(xo))/(f(xo+0.01+xo)-f(xo));

 Ea=((abs(xo-xold))/xo)*100;

end 

fprintf('\nThe lowest positive root of function is %10.6f with approximateerror %7.4f',xo,Ea)

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 8/20

EAB 2113 NUMERICAL METHOD

Find the solution of the following set of linear algebraic equations

2332

1433

132

=++

=++

=++

 z  y x

 z  y x

 z  y x

a) Using the left division

b) Using Gaussian elimination

c) Using LU decomposition

8

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 9/20

EAB 2113 NUMERICAL METHOD

Question 6

9

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 10/20

EAB 2113 NUMERICAL METHOD

Develop a function M-file Tridiag.m to solve the following tridiagonal systemwith the Thomas algorithm.

=

×

−−−−−

n

n

n

n

nn

nnn

 x

 x

 x

 x

 x

 f  e

 g  f  e

 g  f  e

 g  f  e

 g  f  

1

3

2

1

1

3

2

1

111

333

222

11

.

.

.

.

.

.

...

...

...

 Thomas Algorithm:

(i) Decomposition:

1−

=k 

k k 

  f  

ee and

1. −−= k k k k  g e  f    f   , where nk  ,,4,3,2 −−−−−−= .

(ii) Forward substitution:

1. −−= k k k k  r er r  , where nk  ,,4,3,2 −−−−−−= .

(iii) Back substitution:

n

nn

 f  

r  x =  

andk 

k k k k 

 f  

 x g r  x

).( 1+−= , where 1,2,,2,1 −−−−−−= nnk  .

Using your program, solve the following tridiagonal system.

 

−−

−−

01475.2020875.0

020875.001475.2020875.0

020875.001475.2020875.0

020875.001475.2

×

4

3

2

1

 x

 x

 x

 x

=

0875.2

0

0

175.4

10

function x=Tri(e,f,g,r)%e=input('e= ');%f=input('f= ');%g=input('g= ');%r=input('r= ');e=[0 -0.020875 -0.020875 -0.020875];f=[2.01475 2.01475 2.01475 2.01475];g=[-0.020875 -0.020875 -0.020875 0];

r=[4.175 0 0 2.0875];n=length(f);for k=2:n

factor=e(k)/f(k-1);f(k)=f(k)-factor*g(k-1);r(k)=r(k)-factor*r(k-1);

 fprintf('factor=%2.4f\t f(k)=%2.4f\t r(k)=%2.4f\n',factor,f(k),r(k))endx(n)=r(n)/f(n);for k=n-1:-1:1

x(k)=(r(k)-g(k)*x(k+1))/f(k);fprintf('x(k)=%2.4f\n',x(k))

end 

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 11/20

EAB 2113 NUMERICAL METHOD

Question 7

11

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 12/20

EAB 2113 NUMERICAL METHOD

Q7. Develop a MATLAB script file to determine the solution of the following system of 

linear equations using the Gauss-Seidel iteration method by performing first seven

iterations.

2110232

5.1241123

143282

5.542329

4321

4321

4321

4321

−=+++−

=−++−

−=+−+

=++−

 x x x x

 x x x x

 x x x x

 x x x x

Question 8

12

i=1;x1=0;x2=0;x3=0;x4=0;disp(' i x1 x2 x3 x4')fprintf('%2.0f %8.5f %8.5f %8.5f %8.5f\n',i,x1,x2,x3,x4)

for i=2:8x1=(54.5-(-2*x2+3*x3+2*x4))/9;x2=(-14-(2*x1-2*x3+3*x4))/8;x3=(12.5-(-3*x1+2*x2-4*x4))/11;x4=(-21-(-2*x1+3*x2+2*x3))/10;fprintf('%2.0f %8.5f %8.5f %8.5f %8.5f\n',i,x1,x2,x3,x4)

end

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 13/20

EAB 2113 NUMERICAL METHOD

. Develop a script M-file to estimate )75.2(  f   using Lagrange interpolating polynomials

of order 1, 2 and 3 for the following data.

 x 0 1 2 3 4 5

 f  ( x)

0 0.5 0.8 0.9 0.941176 0.961538

For each estimate find the true percent relative error if the try function is

given by)1(

)(2

2

 x

 x x  f  

+= .

13

function fint=LagrangeINT(x,y,xint)n=length(x);for i=1:n

L(i)=1;  for j=1:n  if j~=i

L(i)=L(i)*(xint-x(j))/(x(i)-x(j));  end  endendf=(xint^2)/(1+xint^2);et=abs((f-sum(y.*L))/f)/100;fprintf('\nx=%.8f\n',sum(y.*L));fprintf('Error=%6f%%\n',et); 

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 14/20

EAB 2113 NUMERICAL METHOD

Question 9

. The force on a sailboat mast can be represented by the following function:

∫  −  

 

+=

H  H  z  dz e

 z 

 z  F 

0

/5.2

7200

where = z  the elevation above the deck and = H  the height of the mast.Compute F  for the case where 30= H  using

(i) the M-file for Trapezoidal rule with the step size 1.0=h .

the MATLAB trapz function.

Question 10

14

function evaluate = trapezoidal1(f,H,h)%f=inline('200*(z/(7+z))*exp(-25*z/30)')%y(for the trapz function)=200*(z./(7+z)).*exp(-25.*z./30)a=0; 

while(1)n=(H-a)/h;t0=f(a);t1=0;for i=1:n-1

t1=t1+f(a+h*i);endt2=f(H);value=h/2*(t0+2*t1+t2);breakendfprintf('The Trapezoidal Integral: %2.8f\n',value)

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 15/20

EAB 2113 NUMERICAL METHOD

Develop an M-file to implement Simpson’s 1/3 rule. Using your program solvethe following problem.

 The velocity of falling parachutist is given as

)1()( )/( t mcec

 gmt v −−= .

Where )(t v = velocity of parachutist,

 g = gravitational constant = 2/8.9 sm ,

m = mass of the parachutist = kg 45 ,

c = the drag coefficient =  skg /5.32 .

If the distance, d, traveled by the parachutist is given by

∫ =

6

0 )( dt t vd  ,

find the distance using Simpson’s 1/3 rule for the segments 10, 20, 50, and

100.

15

function distance=simpson(V,a,b,n)h=(b-a)/n;t(1)=a;for i=2:n+1

t(i)=t(i-1)+h;

endP=0;for i=2:2:n

P=P+V(t(i));endT=0;for i=3:2:n-1

T=T+V(t(i));endd=(h/3)*(V(a)+(4*P)+(2*T)+V(b));fprintf('\nThe distance travelled by the parachutist=%9.6f\n',d)

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 16/20

EAB 2113 NUMERICAL METHOD

16

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 17/20

EAB 2113 NUMERICAL METHOD

Question 11

Develop an M-file for Euler’s method to solve a first order ordinary differentialequation (ODE).

 The current around the circuit at time t  is governed by the followingdifferential equation

t eidt 

di 2323 −+= , 2)0( =i .

Using your program, solve the above initial value problem over the interval

from 0=t  to 2 with the step size 1.0=h .

17

i(t )

E

function [t,i]= Eulode(didt,tspan,i0,h)%input:%didt= name of the function f(t,i)%tspan= [ti,tf] where ti and tf= initial and final valus of independent%variable%y0= initial value of the dependent variable

%h=step size%output:%[t,i] where t= vector of the independent variable% i= vector of the solution for the dependent variabletx=tspan(1);ty=tspan(2);t=(tx:h:ty)';n=length(t);i=i0*ones(n,1);for x=1:n-1

i(x+1)=i(x)+feval(didt,t(x),i(x))*h;end

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 18/20

EAB 2113 NUMERICAL METHOD

18

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 19/20

EAB 2113 NUMERICAL METHOD

Question 12

Develop an M-file for Fourth-Order Runge-Kutta method to solve a first order

ordinary differential equation (ODE).

Using your program solve the following initial value problem over the interval

from 0= x to 2 with the step size 2.0=h .

8.0)0(,2

=+= y y xdx

dy.

19

function [x,y]=rk40de(dydx,xspan,y0,h)xi=xspan(1);xf=xspan(2);x=(xi:h:xf)';n=length(x);y=y0*ones(n,1);for i=1:n-1

k1=feval(dydx,x(i),y(i));k2=feval(dydx,x(i)+(1/2)*h,y(i)+(1/2)*k1*h);k3=feval(dydx,x(i)+(1/2)*h,y(i)+(1/2)*k2*h);k4=feval(dydx,x(i)+h,y(i)+k3*h);y(i+1)=y(i)+(1/6)*(k1+2*k2+2*k3+k4)*h;

end

7/28/2019 Matlab Solution 1

http://slidepdf.com/reader/full/matlab-solution-1 20/20

EAB 2113 NUMERICAL METHOD

20