OBJECTIVE: To understand Bi-section method.
SOFTWARE USED: Matlab
COMPUTER SPECIFICATIONS:
FLOWCHART:
MATLAB
CODE:
close all
clear all
clc
f=@(x)(x^3-x-2);
xl=input(‘Enter the first approximation xl:’);
xu=input(‘Enter the second approximation xu:’);
acc=input(‘Enter the value of accuracy:’);
while((f(xl)*f(xu))>0)
xl=input(‘Enter the value of first approximation xl:’);
xu=input(‘Enter the value of second approximation xu:’);
end
while(abs(xu-xl)>acc)
xm=(xl-xu)/2;
if(f(x)*f(xm)<0)
xu=xm;
else
xl=xm;
end
end
fprintf(‘The root of equation is:’,xm);
Plot of Error of Bisection Method:-
OUTPUT/ITERATIONS:
The input for the method is a continuous function f, an interval [a, b], and the function
values f(a) and f(b). The function values are of opposite sign (there is at least one zero
crossing within the interval). Each iteration performs these steps:
1. Calculate c, the midpoint of the interval, c = a + b/2.
2. Calculate the function value at the midpoint, f(c).
3. If convergence is satisfactory (that is, c - a is sufficiently small, or |f(c)| is
sufficiently small), return c and stop iterating.
4. Examine the sign of f(c) and replace either (a, f(a)) or (b, f(b)) with (c, f(c)) so that
there is a zero crossing within the new interval.
Iteration an bn cn f(cn)
1 1 2 1.5 −0.125
2 1.5 2 1.75 1.6093750
3 1.5 1.75 1.625 0.6660156
4 1.5 1.625 1.5625 0.2521973
5 1.5 1.5625 1.5312500 0.0591125
6 1.5 1.5312500 1.5156250 −0.0340538
7 1.5156250 1.5312500 1.5234375 0.0122504
8 1.5156250 1.5234375 1.5195313 −0.0109712
9 1.5195313 1.5234375 1.5214844 0.0006222
10 1.5195313 1.5214844 1.5205078 −0.0051789
11 1.5205078 1.5214844 1.5209961 −0.0022794
12 1.5209961 1.5214844 1.5212402 −0.0008289
13 1.5212402 1.5214844 1.5213623 −0.0001034
14 1.5213623 1.5214844 1.5214233 0.0002594
15 1.5213623 1.5214233 1.5213928 0.0000780
After 15 iterations, it becomes apparent that there is a convergence to about 1.521: a root for the
polynomial.
RESULT: The method is guaranteed to converge to a root of f if f is a continuous function on
the interval [a, b] and f(a) and f(b) have opposite signs. The absolute error is halved at each step so
the method converges linearly, which is comparatively slow.
OBJECTIVE: To understand regula falsi method.
SOFTWARE USED: Matlab
COMPUTER SPECIFICATIONS:
PLOT OF ERROR FOR REGULA-FALSI METHOD:
FLOWCHART:
MATLAB CODE:
Clear all
close all
clc;
f=@(x) x^3-2*x-5;
a=2; b=3;
for i=1:10
x0=a; x1=b;
fprintf('\n Hence root lies between (%.4f,%.0f)',a,b)
x2(i)=x0-(x1-x0)/(f(x1)-f(x0))*f(x0);
if f(x2(i))>0
b=x2(i);
else a=x2(i);
end
fprintf('\n Therefore, x2=%.4f \n Here, f(x2)=%.4f',x2(i),f(x2(i)))
p=x2(i);
end
OUTPUT/ITERATIONS:
RESULT:
The fact that regula falsi always converges, and has versions that do well at avoiding
slowdowns, makes it a good choice when speed is needed. However, its rate of
convergence can drop below that of the bisection method.
OBJECTIVE: To understand Newton-Raphson method.
SOFTWARE USED: Matlab
COMPUTER SPECIFICATIONS:
FLOWCHART:
MATLAB CODE:
a=input('Enter the function in the form of variable x:','s');
x(1)=input('Enter Initial Guess:');
error=input('Enter allowed Error:');
f=inline(a)
dif=diff(sym(a));
d=inline(dif);
for i=1:100
x(i+1)=x(i)-((f(x(i))/d(x(i))));
err(i)=abs((x(i+1)-x(i))/x(i));
if err(i)<error
break
end
end
root=x(i)
Sample output: