assignment 12

Download as pdf or txt
Download as pdf or txt
You are on page 1of 10

NUMERICAL METHODS

FOR NON 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
Program:
clc y=@(x)
5*(x)

BISECTION METHOD
-exp(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, '*r:', 'linewidth', 2.5);
title('BISECTION METHOD');
ylabel('Error');
xlabel('Iteration');

end

Output:

FALSE POSITION METHOD


-exp(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
Program:
clc y=@(x)
5*(x)
fprintf('The root is %f ', ro);
plot(E, '*b:', 'linewidth', 2.5);
title('FALSE POSITION METHOD');
ylabel('Error');
xlabel('Iteration'); end

Output:

SECANT METHOD
-exp(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, '*g:', 'linewidth', 2.5);
title('SECANT METHOD');
ylabel('Error');
xlabel('Iteration');
Output:
Program:
clc y=@(x)
5*(x)
NEWTON RAPHSON METHOD
Program:
clc
y=@(x) 5*(x)-exp(x)+4; dy=@(x) 3-exp(x);
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, '*k:', 'linewidth', 2.5);
title('NEWTON-RAPHSON METHOD');
ylabel('Error');
xlabel('Iteration');

Output:

ERRORS
Comaprison of methods
NON-ALGEBRIC PROGRAM
clc
y=@(x) 5*(x)-exp(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, '*r:', 'linewidth', 2.5);
title('BISECTION METHOD');
ylabel('Error');
xlabel('Iteration');

end
y=@(x) 5*(x)-exp(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, '*b:', 'linewidth', 2.5);
title('FALSE POSITION METHOD');
ylabel('Error');
xlabel('Iteration'); end
y=@(x) 5*(x)-exp(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, '*g:', 'linewidth', 2.5);
title('SECANT METHOD');
ylabel('Error');
xlabel('Iteration');
y=@(x) 5*(x)-
exp(x)+4; dy=@(x) 3-
exp(x);
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, '*k:', 'linewidth', 2.5);
title('NEWTON-RAPHSON METHOD');
ylabel('Error');
xlabel('Iteration');
hold on; plot(E,
'r','linewidth', 0.5); hold on;
plot(E1, 'b', 'linewidth', 0.5);
hold on; plot(E2,'g',
'linewidth', 0.5); hold on;
plot(E3, 'k', 'linewidth', 0.5);

You might also like