0% found this document useful (0 votes)
205 views

Laboratory Activity #4 Numsol

1. The document contains the results and code for 4 numerical methods - bisection, regula falsi, Newton-Raphson, and secant - to find the roots of non-linear equations. 2. For the bisection method, it returns a root of 1.855591 for the equation x^4-x-10. 3. For regula falsi, it returns a root of 0.701394 for 5*sin(x)^2 - 8*cos(x)^5. 4. Newton-Raphson returns a root of 0.450184 for cos(x)-2*x. 5. Secant method returns a root of

Uploaded by

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

Laboratory Activity #4 Numsol

1. The document contains the results and code for 4 numerical methods - bisection, regula falsi, Newton-Raphson, and secant - to find the roots of non-linear equations. 2. For the bisection method, it returns a root of 1.855591 for the equation x^4-x-10. 3. For regula falsi, it returns a root of 0.701394 for 5*sin(x)^2 - 8*cos(x)^5. 4. Newton-Raphson returns a root of 0.450184 for cos(x)-2*x. 5. Secant method returns a root of

Uploaded by

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

CEBALLOS, JAMES CARL G.

CE22S5 - CE316
LABORATORY ACTIVITY #4

SUMMARY OF ANSWERS:

1) X = 1.855591

2) X = 0.701394

3) X = 0.450184

4) X = -1.0991

CODES:

1) BISECTION METHOD:

% Clearing Screen
clc

% Setting x as symbolic variable


syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');

% Finding Functional Value


fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));

% Implementing Bisection Method


if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = (a+b)/2;
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
else
a =c;
end
c = (a+b)/2;
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end

COMMAND WINDOW:
Enter non-linear equations: x^(4)-x-10
Enter first guess: 1.5
Enter second guess: 2
Tolerable error: 0.001

a b c f(c)
1.500000 2.000000 1.750000 -2.371094
1.750000 2.000000 1.875000 0.484619
1.750000 1.875000 1.812500 -1.020248
1.812500 1.875000 1.843750 -0.287734
1.843750 1.875000 1.859375 0.093378
1.843750 1.859375 1.851562 -0.098433
1.851562 1.859375 1.855469 -0.002843
1.855469 1.859375 1.857422 0.045189
1.855469 1.857422 1.856445 0.021153
1.855469 1.856445 1.855957 0.009150
1.855469 1.855957 1.855713 0.003152

Root is: 1.855591

2) REGULA FALSI METHOD:

% Clearing Screen
clc

% Setting x as symbolic variable


syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter first guess: ');
b = input('Enter second guess: ');
e = input('Tolerable error: ');

% Finding Functional Value


fa = eval(subs(y,x,a));
fb = eval(subs(y,x,b));

% Implementing Bisection Method


if fa*fb > 0
disp('Given initial values do not bracket the root.');
else
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
fprintf('\n\na\t\t\tb\t\t\tc\t\t\tf(c)\n');
while abs(fc)>e
fprintf('%f\t%f\t%f\t%f\n',a,b,c,fc);
if fa*fc< 0
b =c;
fb = eval(subs(y,x,b));
else
a =c;
fa = eval(subs(y,x,a));
end
c = a - (a-b) * fa/(fa-fb);
fc = eval(subs(y,x,c));
end
fprintf('\nRoot is: %f\n', c);
end

COMMAND WINDOW:
Enter non-linear equations: 5*sin(x).^2 - 8*cos(x).^5
Enter first guess: 0
Enter second guess: 1
Tolerable error: 0.001

a b c f(c)
0.000000 1.000000 0.716076 0.199874
0.000000 0.716076 0.698621 -0.037756

Root is: 0.701394

3) NEWTON-RAPHSON METHOD:

% Clearing Screen
clc

% Setting x as symbolic variable


syms x;

% Input Section
y = input('Enter non-linear equations: ');
a = input('Enter initial guess: ');
e = input('Tolerable error: ');
N = input('Enter maximum number of steps: ');
% Initializing step counter
step = 1;

% Finding derivate of given function


g = diff(y,x);

% Finding Functional Value


fa = eval(subs(y,x,a));

while abs(fa)> e
fa = eval(subs(y,x,a));
ga = eval(subs(g,x,a));
if ga == 0
disp('Division by zero.');
break;
end

b = a - fa/ga;
fprintf('step=%d\ta=%f\tf(a)=%f\n',step,a,fa);
a = b;

if step>N
disp('Not convergent');
break;
end
step = step + 1;
end

fprintf('Root is %f\n', a);


COMMAND WINDOW:
Enter non-linear equations: cos(x)-2x
Enter non-linear equations: cos(x)-2*x
Enter initial guess: 0
Tolerable error: 0.001
Enter maximum number of steps: 5
step=1 a=0.000000 f(a)=1.000000
step=2 a=0.500000 f(a)=-0.122417
step=3 a=0.450627 f(a)=-0.001079
step=4 a=0.450184 f(a)=-0.000000

Root is 0.450184

4) SECANT METHOD:

% Secant Method in MATLAB


a=input('Enter function:','s');
f=inline(a)

x(1)=input('Enter first point of guess interval: ');


x(2)=input('Enter second point of guess interval: ');
n=input('Enter allowed Error in calculation: ');
iteration=0;

for i=3:1000
x(i) = x(i-1) - (f(x(i-1)))*((x(i-1) - x(i-2))/(f(x(i-1)) -
f(x(i-2))));
iteration=iteration+1;
if abs((x(i)-x(i-1))/x(i))*100<n
root=x(i)
iteration=iteration
break
end
end

COMMAND WINDOW:
Enter first point of guess interval: 0
Enter second point of guess interval: 1
Enter allowed Error in calculation: 0.001

root = -1.0991
iteration = 5

You might also like