0% found this document useful (1 vote)
166 views8 pages

Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 8

NUMERICAL ANALYSIS

MATLAB Practical (Autumn 2020)


B.E. III Semester
Thapar Institute of Engineering & Technology Patiala

Name: Aarohan Verma


Roll No.: 101903497
Group: COE19
Instructor: Mrs. Mamta Gulati
(Assignment 2- Newton’s and Secant Methods)
2.(i)
Code:

f=@(x) x^2-17;
g=@(x) 2*x;
x0=4.1;
eps=0.00001;
itr=1;
N=1000;
while i<=N
x1= x0-(f(x0)/g(x0));
if (abs(x1-x0)<=eps)
fprintf("The root of the equation is: %d",x1);
break;
else
itr=itr+1;
x0=x1;
end
end

Answer:
The root of the equation is: 4.123106e+00

OUTPUT:
2.(ii)
Code:

f=@(x) cos(x) - (1/2) - sin(x);


g=@(x) -cos(x) -sin(x);
x0=0.4;
eps=0.00001;
itr=1;
N=1000;
while i<=N
x1=x0-f(x0)/g(x0);
if(abs(x1-x0)<=eps)
fprintf("The root of the equation is: %d",x1);
break;
else
itr=itr+1;
x0=x1;
end
end

Answer:
The root of the equation is: 4.240310e-01

OUTPUT:
2.(iii)

Code:
f=@(x) exp(-x) * (x^2 + 5*x + 2) + 1;
g=@(x) exp(-x) * (-x^2 - 3*x + 3);
for x0=1:2
x0=input("\n\nEnter initial guess: ");
eps=0.00001;
N=1000;
itr=1;
while i<=N
x1=x0-f(x0)/g(x0);
if(abs(x1-x0)<=eps)
fprintf("The root of the equation is: %d",x1);
break;
else
itr=itr+1;
x0=x1;
end
end
end

Answer:
Enter initial guess: -2
The root of the equation is: -5.791589e-01
Enter initial guess: -1
The root of the equation is: -5.791589e-01

OUTPUT:
2.(iv)
Code:

f=@(x) x - (2*sin(x));
g=@(x) 1 - (2*cos(x));
h=0.5;
N=10;
for i=-N:h:N
if f(i)*f(i+h)<0
a=i;
b=i+h;
end
end
x0=2;
eps=0.00001;
N=1000;
itr=1;
while i<=N
x1=x0-f(x0)/g(x0);
if(abs(x1-x0)<=eps)
fprintf("The solution of the equation is: %d",x1);
break;
else
itr=itr+1;
x0=x1;
end
end

Answer:
The solution of the equation is: 1.895494e+00

OUTPUT:
3.
Code:
f=@(x) (4*x*x) - exp(x) - exp(-x);
g=@(x) (8*x) - exp(x) + exp(-x);

for i=1:8
x0=input("\nx0= ");
e=0.00001;
N=1000;
itr=1;
while itr<=N
x1=x0-f(x0)/g(x0);
if(abs(x1-x0)<=e)
fprintf("Solution: %d",x1);
break;
else
itr=itr+1;
x0=x1;
end
end
end

Answer:
x0=-10
Solution: -4.306245e+00
x0= -5
Solution: -4.306245e+00
x0= -3
Solution: 8.244986e-01
x0= 1
Solution: 8.244986e-01
x0= 3
Solution: -8.244986e-01
x0= 5
Solution: 4.306245e+00
x0= 10
Solution: 4.306245e+00
x0= 0
OUTPUT:

You might also like