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

Mapua Institute of Technology

School of EECE
EE Department

EE60L/A6
Machine Problem #1

Instructor:
Prof. Uyvico

Members:
Planco, Ann Dante Mary
Templonuevo, Mark Franz
Tiu, Darren

Machine Problem 1

Write a program that computes all the roots of roots f(x) =0 in a given interval
with bisection method and newton-raphsons method. Utilize the function root
search and bisect.

1. Test the program by finding the roots of


f(x) = x^3-10*x^2+5 =0 that lies in the interval (0.6, 0.8).

2. For the bisection method and x^2-5*x+2=0 for the newton-raphsons that
lies between the interval (0, 1).

1.

SYNTAX

clc
syms x;

n=1;
while abs(x0-x1)>=tol

F=input ('Enter the given f(x): ');

xn=(x0+x1)/2;

pretty (F);

s=subs(F,x,x0);

x0=input ('Enter a number for x0: ');

z=subs(F,x,xn);

fprintf ('x0: ');


disp(x0);

if(s*z<0)

F_x0=subs(F,x,x0);

x1=xn;

fprintf ('f(x0): ')

elseif(s*z>0)

disp(F_x0);

x0=xn;
end

x1=input ('Enter a number for x1: ');


fprintf ('x1: ');
disp(x1);

xn

x1

line=[n,x0,x1,xn,s,z];

F_x1=subs(F,x,x1);

n=n+1;

fprintf ('f(x1): ')

disp(line);

disp(F_x1);

tol=0.00001;

disp(' n
x0
f(x0)
f(xn)');

end

check=abs(x0-x1);
str=['The root is ', num2str(xn), ' . '];
disp(str);

OUTPUT:
Enter the given f(x): x^3-10*x^2+5

x - 10 x + 5
Enter a number for x0: 0.6
x0:

0.6000

f(x0):

1.6160

Enter a number for x1: 0.8


x1:

0.8000

f(x1):

-0.8880

n
1.0000
n
2.0000
n
3.0000
n
4.0000
n
5.0000
n
6.0000

x0
0.7000
x0
0.7000
x0
0.7250
x0
0.7250
x0
0.7312
x0
0.7344

x1
0.8000
x1
0.7500
x1
0.7500
x1
0.7375
x1
0.7375
x1
0.7375

xn
0.7000
xn
0.7500
xn
0.7250
xn
0.7375
xn
0.7312
xn
0.7344

f(x0)

f(xn)

1.6160

0.4430

f(x0)

f(xn)

0.4430 -0.2031
f(x0)

f(xn)

0.4430

0.1248

f(x0)

f(xn)

0.1248 -0.0379
f(x0)

f(xn)

0.1248

0.0438

f(x0)

f(xn)

0.0438

0.0030

n
7.0000
n
8.0000
n
9.0000
n
10.0000
n
11.0000
n
12.0000
n
13.0000
n
14.0000
n
15.0000

x0
0.7344
x0
0.7344
x0
0.7344
x0
0.7346
x0
0.7346
x0
0.7346
x0
0.7346
x0
0.7346
x0
0.7346

The root is 0.7346.

x1
0.7359
x1
0.7352
x1
0.7348
x1
0.7348
x1
0.7347
x1
0.7346
x1
0.7346
x1
0.7346
x1
0.7346

xn
0.7359
xn
0.7352
xn
0.7348
xn
0.7346
xn
0.7347
xn
0.7346
xn
0.7346
xn
0.7346
xn
0.7346

f(x0)

f(xn)

0.0030 -0.0175
f(x0)

f(xn)

0.0030 -0.0072
f(x0)

f(xn)

0.0030 -0.0021
f(x0)

f(xn)

0.0030

0.0004

f(x0)

f(xn)

0.0004 -0.0008
f(x0)

f(xn)

0.0004 -0.0002
f(x0)

f(xn)

0.0004

0.0001

f(x0)

f(xn)

0.0001 -0.0000
f(x0)

f(xn)

0.0001

0.0000

2.)

SYNTAX:

clc
syms x;
A=input ('Enter the given f(x): ');
xn=input ('Enter a number: ');
pretty (A);
fprintf('\n \n');
fprime=diff(A);
fprintf('the first derivative of f(x) is');
fprintf('\n \n');
pretty (fprime);
fprintf('\n \n');
n=1;
for i=0:10
w=subs (A,x,xn);
t=subs(fprime,x,xn);
xn=xn-(w/t);
%
disp('n
fprime(xn)');
%
line=[n,xn,w, t];
%
n=n+1;
%
disp(line);

xn

f(xn)

end
fprintf('The root of the function is %.5f. \n
\n',xn);

OUTPUT:
Enter the given f(x): x^2-5*x+2
Enter a number: 0

2
x -5x+2

the first derivative of f(x) is

2x-5

The root of the function is 0.43845.

You might also like