0% found this document useful (0 votes)
34 views9 pages

Assignment 1

Numerical analysis answers and problems

Uploaded by

Roman Fairooz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views9 pages

Assignment 1

Numerical analysis answers and problems

Uploaded by

Roman Fairooz
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 9

NUMERICAL METHODS

FOR ALGEBRIC EQUATION

Assignment # 1

Write programs for Bisection method, Secant Method, False position Method, Newton
Raphson method.

Name: Roman Fairooz

Roll no:BS – 22 – IB - 104339

Submission date: 28 octobar 2024

Department of Mechanical Engineering

Pakistan Institute of Engineering and Applied Sciences Nilore,


Islamabad

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

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
Output:

FALSE POSITION 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
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=u-(y(u)*(l-u))/(y(l)-y(u)); 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, '*r:', 'linewidth', 2.5);
title('FALSE POSITION METHOD');
ylabel('Error');
xlabel('Iteration'); end
Output:
Program: clc

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

NEWTON RAPHSON METHOD


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; E=[];
while (i<=50) && (abs(e)> tol) && (abs(y(ro))>tol)
r=ro-(y(ro)/dy(ro)); e=(r-ro); E=[E
e]; i=i+1; ro=r; end
fprintf('The root is %f ', ro);
plot(E, '*g:', 'linewidth', 2.5);
title('NEWTON-RAPHSON METHOD');
ylabel('Error');
xlabel('Iteration');

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);

SOLUTION OF METHOD WITH MATLAB BUILT


IN FUNCTION

You might also like