Assignment 1
Assignment 1
Assignment # 1
Write programs for Bisection method, Secant Method, False position Method, Newton
Raphson method.
BISECTION METHOD
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-50*(x^2)+2*(x)-
4; u=input('Enter the upper bound '); l=input('Enter the
lower bound '); tol=input('Enter tolerance'); if
Program: clc
end
Output:
SECANT METHOD
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-
50*(x^2)+2*(x)-4; u=input('Enter the upper bound ');
l=input('Enter the lower bound ');
tol=input('Enter tolerance');
i=1;
E=[]; e=(l-
u); ro=l;
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=u-(y(u)*(l-u))/(y(l)-y(u)); u=l;
l=r; e=(r-ro); E=[E e];
i=i+1; ro=r; end
fprintf('The root is %f ', ro);
plot(E, '*y:', 'linewidth', 2.5);
title('SECANT METHOD');
ylabel('Error');
xlabel('Iteration');
Output:
Program: clc
Output:
ERRORS
Comaprison of methods
ALGEBRIC
Input
clc
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-50*(x^2)+2*(x)-4;
u=input('Enter the upper bound '); l=input('Enter the
lower bound '); tol=input('Enter tolerance'); if
y(u)*y(l)>0
fprintf('Root does not exist between this Interval');
else i=1; E=[]; e=(l-u); ro=l;
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=(l+u)/2; if y(u)*y(r)<0 l=r;
else u=r; end e=(r-ro);
E=[E e]; i=i+1; ro=r; end
fprintf('The root is %f ', ro);
plot(E, '*b:', 'linewidth', 2.5);
title('BISECTION METHOD');
ylabel('Error');
xlabel('Iteration');
end
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-50*(x^2)+2*(x)-4;
u=input('Enter the upper bound '); l=input('Enter the
lower bound '); tol=input('Enter tolerance'); if
y(u)*y(l)>0
fprintf('Root does not exist between this Interval');
else i=1; E1=[]; e=(l-u); ro=l;
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=u-(y(u)*(l-u))/(y(l)-y(u)); if y(u)*y(r)<0
l=r; else u=r; end
e=(r-ro); E1=[E1 e]; i=i+1;
ro=r; end
fprintf('The root is %f ', ro);
plot(E1, '*r:', 'linewidth', 2.5);
title('FALSE POSITION METHOD');
ylabel('Error');
xlabel('Iteration'); end
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-50*(x^2)+2*(x)-4;
u=input('Enter the upper bound '); l=input('Enter the
lower bound '); tol=input('Enter tolerance');
i=1;
E2=[]; e=(l-
u); ro=l;
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=u-(y(u)*(l-u))/(y(l)-y(u)); u=l;
l=r; e=(r-ro); E2=[E2 e];
i=i+1; ro=r; end
fprintf('The root is %f ', ro);
plot(E2, '*y:', 'linewidth', 2.5);
title('SECANT METHOD');
ylabel('Error');
xlabel('Iteration');
y=@(x) 300*(x^5)+250*(x^4)-150*(x^3)-50*(x^2)+2*(x)-
4; dy=@(x) 2400*(x^3)-1650*(x^2)+400*(x)-20;
ro=input('Enter an appropriate guess value ');
tol=input('Enter tolerance '); i=1; e=1; E3=[];
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=ro-(y(ro)/dy(ro)); e=(r-ro); E3=[E3
e]; i=i+1; ro=r;
end
fprintf('The root is %f ', ro);
plot(E3, '*g:', 'linewidth', 2.5);
title('NEWTON-RAPHSON METHOD');
ylabel('Error');
xlabel('Iteration');
hold on;
plot(E, 'b','linewidth', 0.5);
hold on;
plot(E1, 'r', 'linewidth', 0.5);
hold on;
plot(E2,'y', 'linewidth', 0.5);
hold on;
plot(E3, 'g', 'linewidth', 0.5);