0% found this document useful (0 votes)
46 views14 pages

Labreport Module 4 - Maglinte

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)
46 views14 pages

Labreport Module 4 - Maglinte

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/ 14

LA SALLE UNIVERSITY – OZAMIZ CITY

COLLEGE OF COMPUTER STUDIES, ENGINEERING AND ARCHITECTURE


ENGMATH5 LABORATORY

NAME: Mark Andykenn L. Maglinte LABORATORY NO.: 3


COURSE AND YEAR: BSCE - 3 DATE: 12 - 15 - 2020

Solving Nonlinear Equations using GNU Octave

A. Objectives

a. To know how to find the roots of nonlinear equations in different


ways through numerical methods.

b. To know how to solve the roots of nonlinear equations using the GNU
Octave.

c. To show the difference of solving the roots between analytical solution


and the numerical solution.

B. Source code (screen shots/pics)

Bisection Method
Regula Falsi Method
Newton Raphson Method
Secant Method
Fixed Point Method
C. Output

Bisection Method

>> %Determine the root of f(x) = x - 2e^(-x)


>>
>> %f(x)
>> F = @ (x) x - 2 * e^(-x);
>>
>> %inital condition
>> a = 0;
>> b = 1;
>>
>> %setting of parameters
>> imax = 20;
>> tol = 0.001;
>> Fa = F(a);
>> Fb = F(b);
>>
>> %main code
>> if Fa * Fb > 0
fprintf('Error: The function has the same sign at points a and b.')
else
%Display Header
fprintf('iteration a b (xNS) Solution f(xNS)
Tolerance\n')

for i = 1:imax
xNS = (a + b)/2;
toli = (b - a)/2;
FxNS = F(xNS);

%display iteration results


Fprintf
('%3i %11.6f %11.6f %11.6f %11.6f
%11.6f\n', i, a, b, xNS, FxNS, toli)

if FxNS == 0
fprintf('An exact solution x = %11.6f was not found', xNS)

break
end

if toli < tol


xNS

break
end

if 1 == imax
fprintf('Solution was not obtained in %i iterations', imax)

break
end

if F(a) * FxNS < 0


b = xNS;

else
a = xNS;

end
end
end

iteration a b (xNS)
Solution f(xNS) Tolerance

1 0.000000 1.000000 0.500000


-0.713061 0.500000
2 0.500000 1.000000 0.750000
-0.194733 0.250000
3 0.750000 1.000000 0.875000
0.041276 0.125000
4 0.750000 0.875000 0.812500
-0.074995 0.062500
5 0.812500 0.875000 0.843750
-0.016439 0.031250
6 0.843750 0.875000 0.859375
0.012522 0.015625
7 0.843750 0.859375 0.851562
-0.001933 0.007812
8 0.851562 0.859375 0.855469
0.005301 0.003906
9 0.851562 0.855469 0.853516
0.001686 0.001953
10 0.851562 0.853516 0.852539
-0.000123 0.000977

xNS = 0.85254

Regula Falsi Method

>> %DETERMINE THE ROOT OF F(X) = X - 2e^(-X) USING REGULA


FALSI METHOD
>>
>>
>> %F(X)
>> F = @ (x) x - 2 * e^(-x);
>>
>> %INITIAL CONDITION
>> a = 0;
>> b = 1;
>>
>> %SETTING OF PARAMETERS
>> imax = 20;
>> err = 0.001;
>> Fa = F(a);
>> Fb = F(b);
>>
>> %MAIN CODE
>> if Fa*Fb > 0
fprintf('Error: The function has the same sign at points a and b.')
else
%DISPLAY HEADER
fprintf('iteration a b (xNS)
Solution F(xNS) Error\n')

for i = 1:imax
xNS = ((a*Fb)-(b*Fa))/(Fb-Fa);
erro = (b - a)/ b;
FxNS = F (xNS);

%DISPLAY ITERATION RESULTS

fprintf('%3i %11.6f %11.6f %11.6f


%11.6f %11.6f\n', i, a, b, xNS, FxNS, erro)
if FxNS == 0
fprintf('An exact solution x = %11.6f was found', xNS)

break
end

if erro < err


xNS

break
end

if i == imax
fprintf('Solution was not obtained in %i iterations' , imax)

break
end

if F(a)*FxNS < 0
b = xNS;
else
a = xNS;

end
end
End

iteration a b (xNS)
Solution F(xNS) Error

1 0.000000 1.000000 0.883298


0.056464 1.000000
2 0.000000 0.883298 0.780216
-0.136399 1.000000
3 0.780216 0.883298 0.871268
0.034427 0.116702
4 0.780216 0.871268 0.860642
0.014861 0.104506
5 0.780216 0.860642 0.851256
-0.002500 0.093450
6 0.851256 0.860642 0.859547
0.012839 0.010906
7 0.851256 0.859547 0.858579
0.011052 0.009645
8 0.851256 0.858579 0.857725
0.009473 0.008529
9 0.851256 0.857725 0.856970
0.008077 0.007541
10 0.851256 0.856970 0.856303 0.006844
0.006667
11 0.851256 0.856303 0.855714
0.005755 0.005894
12 0.851256 0.855714 0.855194
0.004792 0.005209
13 0.851256 0.855194 0.854734
0.003942 0.004604
14 0.851256 0.854734 0.854328
0.003191 0.004069
15 0.851256 0.854328 0.853970
0.002527 0.003596
16 0.851256 0.853970 0.853653
0.001941 0.003178
17 0.851256 0.853653 0.853374
0.001423 0.002808
18 0.851256 0.853374 0.853126
0.000965 0.002481
19 0.851256 0.853126 .852908
0.000561 0.002192
20 0.851256 0.852908 0.852715
0.000204 0.001937

Newton Raphson Method

>> %DETERMINE THE ROOT OF F(X) = X - 2e^(-X) WITH NEWTON -


RAPHSON METHOD
>>
>> %INITIAL CONDITION
>> Xest = 1;
>>
>> %SETTING OF PARAMETERS
>> Err = 0.0001;
>> imax = 10;
>> format long
>>
>> %DISPLAY HEADER
>> fprintf('i Xi Xi+1 Error \n')
i Xi Xi+1 Error
>>
>> %MAIN CODE
>> for i = 1:imax

%F(X)
y1 = Xest - 2 * e^(-Xest);

%FIRST DERIVATIVE OF F(X)


y2 = 1 + 2 * e^(-Xest);

%EQUATION FOR xNS


Xi = Xest - y1/y2;

%CALCULATION OF ERROR
Error = abs((Xi - Xest)/Xest);

%DISPLAY ANSWER
fprintf('%i %.6f %.6f %.6f \n', i, Xest, Xi, Error)

%WHEN TO STOP THE ITERATION


if abs((Xi - Xest)/Xest) < Err
Xs = Xi;

break
end

%ITERATION LIMIT
if i == imax
fprintf(' Solution wasn not obtained in %i iterations. \n', imax)
Xs = (' No answer');

end

i = i;
Xest = Xi;

End

1 1.000000 0.847766 0.152234


2 0.847766 0.852600 0.005702
3 0.852600 0.852606 0.000006
>>

Secant Method
>> % Determine the root of f(x) = x-2e^(-x) with Secant Method
>>
>> %Initial Condition
>> Xa = 0;
>> Xb = 1;
>>
>> % setting of parameters
>> Err = 0.000001;
>> imax = 10;
>> format long
>>
>> %display header
>> fprintf(' i Xa Xb Error \n')
i Xa Xb Error
>>
>> %Main Code
>> for i = 1:imax

%f(Xb)
yXb = Xb - 2 * e^(-Xb);

%f(Xa)
yXa = Xa - 2 * e^(-Xa);

%equation for xNS


Xi = Xb - yXb * (Xa-Xb) / (yXa - yXb);

%calculation of error
Error = abs((Xi - Xb) / Xb);

%display answer
fprintf (' %i %.6f %.6f %.6f \n', i, Xa, Xb, Error)

%Calculation of error
if abs ((Xi - Xb) / Xb) <Err
Xs = Xi

break
end

i = i;
Xa = Xb;
Xb = Xi;
End

1 0.000000 1.000000 0.116702


2 1.000000 0.883298 0.035904
3 0.883298 0.851584 0.001208
4 0.851584 0.852613 0.000008
5 0.852613 0.852606 0.000000
Xs = 8.526055020137228e-01
>> Xa = Xb;

Fixed Point Method


>> % Determine the root of f(x) = x-2e^(-x) with Fixed Point Iteration Method
>>
>>
>> %initial condition
>> Xest = 1;
>>
>> %Setting of parameters
>> Err = 0.0001;
>> imax = 20;
>> format long
>>
>> %display header
>> fprintf(' i Xi Xi+1 Error \n')
i Xi Xi+1 Error
>>
>> %Main Code
>> for i = 1:imax

%f(x)
Xi = 2*e^(-Xest);

%calculation of error
Error = abs ((Xi - Xest) / Xest);

%display answer
fprintf (' %i %.6f %.6f %.6f \n', i, Xest, Xi, Error)

%when to stop the iteration


if abs ((Xi - Xest) / Xest) < Err
Xs = Xi

break
end

i = i;
Xest = Xi;

end
1 1.000000 0.735759 0.264241
2 0.735759 0.958283 0.302442
3 0.958283 0.767101 0.199505
4 0.767101 0.928714 0.210680
5 0.928714 0.790123 0.149229
6 0.790123 0.907578 0.148655
7 0.907578 0.807000 0.110820
8 0.807000 0.892389 0.105810
9 0.892389 0.819352 0.081844
10 0.819352 0.881434 0.075770
11 0.881434 0.828377 0.060195
12 0.828377 0.873515 0.054490
13 0.873515 0.834963 0.044135
14 0.834963 0.867781 0.039305
15 0.867781 0.839764 0.032286
16 0.839764 0.863625 0.028413
17 0.863625 0.843262 0.023578
18 0.843262 0.860609 0.020571
19 0.860609 0.845809 0.017197
20 0.845809 0.858420 0.014910

D. Conclusion

In conclusion, GNU Octave is useful in solving various applications in


science and in engineering when dealing with nonlinear equations. Through the
use of different methods and GNU Octave in solving the roots of nonlinear
equations it allows you to get more accurate results when solving such problems
and at the same it becomes easy also to determine roots using numerical solution
as compared to analytical solution.

You might also like