Matlab Solution
Matlab Solution
Matlab Solution
Question 1
Develop an M-file to implement the bisection method. Using this program solve the
following problem.
The velocity of falling parachutist is given as
v(t ) =
gm
(1 e ( c / m ) t ) .
c
format long;
disp('the root for this equation is : ')
disp(xr)
Question 2
2
Develop an M-file to implement the false position method. solve the following
problem.
The velocity of falling parachutist is given as
v(t ) =
gm
(1 e ( c / m ) t ) .
c
Where v (t ) = velocity of parachutist = 40 m / s ,
Question 3
4
(a)
(b)
a)
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,end
end
fprintf('\n\nthe root for this question is %2.6f\n',xr);
Question 4
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 approximate
error %7.4f',xo,Ea)
Question 5
c) Using LU decomposition
8
Question 6
f1
e
2
g1
f2
g2
e3
f3
g3
.
.
.
.
e n 1
f n 1
en
x1 r1
x r
2 2
x3 r3
. = .
. .
. .
g n 1 x n 1 rn 1
f n x n rn
Thomas Algorithm:
(i)
Decomposition:
e
e k = k and
f k 1
f k = f k ek . g k 1 , where k = 2,3,4, , n .
(ii)
Forward substitution:
rk = rk e k . rk 1 , where k = 2,3,4, , n .
(iii)
Back substitution:
r
xn = n
fn
and x k =
( rk g k . x k +1 )
, where k = n 1, n 2, ,2,1 .
fk
Using
your program, solve the following tridiagonal system.
function
x=Tri(e,f,g,r)
%e=input('e= ');
%f=input('f=
01475
0.020875
2.');
x1 4.175
0.');
x 0
%g=input('g=
020875
2.01475
0.020875
%r=input('r=
');
x3 = 0
0
.
020875
2
.
01475
0
.
020875
e=[0 -0.020875 -0.020875 -0.020875];
f=[2.01475
0.020875
2.01475 x 2.0875
2.01475 2.01475 2.01475];
4
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
end
x(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
r(k)=%2.4f\n',factor,f(k),r(k))
10
Question 7
11
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.
9 x1 2 x 2 + 3 x3 + 2 x 4 = 54.5
2 x1 + 8 x 2 2 x3 + 3x 4 = 14
3 x1 + 2 x 2 + 11x3 4 x 4 = 12.5
2 x1 + 3 x 2 + 2 x3 + 10 x 4 = 21
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:8
x1=(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
Question 8
12
0
0
1
0.5
2
0.8
3
0.9
4
0.941176
5
0.961538
For each estimate find the true percent relative error if the try function is
given by f ( x ) =
x2
.
(1 + x 2 )
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
end
end
f=(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);
13
Question 9
.
where z = the elevation above the deck and H = the height of the mast.
Compute F for the case where H = 30 using
(i)
the M-file for Trapezoidal rule with the step size h = 0.1 .
Question 10
14
Develop an M-file to implement Simpsons 1/3 rule. Using your program solve
the following problem.
The velocity of falling parachutist is given as
v (t ) =
gm
(1 e ( c / m ) t ) .
c
45 kg ,
d = v(t ) dt ,
0
find the distance using Simpsons 1/3 rule for the segments 10, 20, 50, and
100.
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;
end
P=0;
for i=2:2:n
P=P+V(t(i));
end
T=0;
for i=3:2:n-1
T=T+V(t(i));
end
d=(h/3)*(V(a)+(4*P)+(2*T)+V(b));
fprintf('\nThe distance travelled by the parachutist=%9.6f\n',d)
15
16
Question 11
Develop an M-file for Eulers method to solve a first order ordinary differential
equation (ODE).
i(t)
E
di
= 2i + 3e 2t ,
dt
Using your program, solve the above initial value problem over the interval
from t = 0 to 2 with the step size h = 0.1 .
17
18
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 x = 0 to 2 with the step size h = 0.2 .
dy
= x2 + y ,
dx
y ( 0 ) = 0 .8 .
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
19
20