Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala
Numerical Analysis: MATLAB Practical (Autumn 2020) B.E. III Semester Thapar Institute of Engineering & Technology Patiala
f=@(x) x^2 - 29
N=10;
h=1;
eps=0.001;
for i=-N:h:N
if f(i)*f(i+h)<0
a=i;
b=i+h;
end
end
while(abs(a-b)>eps)
c=(a+b)/2;
if f(a)*f(c)<0
b=c;
else
a=c;
end
end
Answer:
The root of the equation is 5.385742e+00
OUTPUT:
3.(ii)
Code:
f=@(x) sin(x) - cos(x) + 0.5
h=1;
N=1;
eps=0.001;
for i=0.001:h:1
if f(i)*f(i+h)<0
a=i;
b=i+h;
end
end
while(abs(a-b)>eps)
c=(a+b)/2;
if f(a)*f(c)<0
b=c;
else
a=c;
end
end
fprintf("The smallest positive Root is %d",c);
Answer:
The smallest positive Root is 4.238516e-01
OUTPUT:
3.(iii)
Code:
f=@(x) x^3 + 4*(x^2) - 10
h=1;
eps=0.001;
a=1;
b=2;
itr=0;
while(abs(a-b)>eps)
c=(a+b)/2;
if f(a)*f(c)<0
b=c;
else
a=c;
end
itr=itr+1;
end
fprintf("The root of the equation is %d",c);
fprintf("\nNumber of iterations = %d",itr);
Answer:
The root of the equation is 1.364258e+00
Number of iterations = 10
OUTPUT: