0% found this document useful (0 votes)
19 views43 pages

NM Assignment

...

Uploaded by

92msmoverloaded
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)
19 views43 pages

NM Assignment

...

Uploaded by

92msmoverloaded
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/ 43

Q15

% Define the function f(x) as an anonymous function


F = @(x) 8 - 4.5 * (x - sin(x));

% Assign initial values to a and b


a = 2; % Lower bound
b = 3; % Upper bound
imax = 20; % Maximum number of iterations
tol = 0.001; % Tolerance for stopping criterion

Fa = F(a); % Calculate f(a)


Fb = F(b); % Calculate f(b)

% Check if the function has the same sign at points a and b


if Fa * Fb > 0
disp('Error: The function has the same sign at points a and b.')
else
disp('Iteration | a | b | xNS | f(xNS) | Tolerance')

for i = 1:imax
xNS = (a + b)/2; % Calculate the midpoint (numerical solution)
toli = (b - a)/2; % Calculate the current tolerance
FxNS = F(xNS); % Calculate f(xNS)

% Display iteration information


fprintf('%4d | %8.6f | %8.6f | %8.6f | %8.6f | %8.6f\n', i, a, b, xNS, FxNS, toli)

% Check if the exact solution is found


if FxNS == 0
fprintf('An exact solution x = %.6f was found at iteration %d\n', xNS, i)
break
end

% Stop the iterations if the tolerance is smaller than the desired tolerance
if toli < tol
fprintf('Tolerance reached: xNS = %.6f after %d iterations\n', xNS, i)
break
end

% Now, instead of calling F(a) again, use the previously stored Fa


if Fa * FxNS < 0
b = xNS; % The root is in the left half
else
a = xNS; % The root is in the right half
Fa = FxNS; % Update Fa for the next iteration
end

% If maximum iterations reached


if i == imax
fprintf('Solution was not obtained in %d iterations.\n', imax)
end
end
end

Iteration | a | b | xNS | f(xNS) | Tolerance


1 | 2.000000 | 3.000000 | 2.500000 | -0.556875 | 0.500000
2 | 2.000000 | 2.500000 | 2.250000 | 1.376329 | 0.250000
3 | 2.250000 | 2.500000 | 2.375000 | 0.434083 | 0.125000
4 | 2.375000 | 2.500000 | 2.437500 | -0.055709 | 0.062500
5 | 2.375000 | 2.437500 | 2.406250 | 0.190661 | 0.031250
6 | 2.406250 | 2.437500 | 2.421875 | 0.067838 | 0.015625
7 | 2.421875 | 2.437500 | 2.429688 | 0.006154 | 0.007812
8 | 2.429688 | 2.437500 | 2.433594 | -0.024755 | 0.003906
9 | 2.429688 | 2.433594 | 2.431641 | -0.009295 | 0.001953
10 | 2.429688 | 2.431641 | 2.430664 | -0.001569 | 0.000977
Tolerance reached: xNS = 2.430664 after 10 iterations

Q16

% Script to solve Problem 3.2 using different methods for f(x) = x - 2*exp(-x)

% Define the function for Problem 3.2: f(x) = x - 2*exp(-x)

fun = @(x) x - 2*exp(-x);

% Part (a): Bisection Method

a = 0; % Starting point a

b = 1; % Starting point b

% Call the BisectionRoot function to find the root

Xs_bisection = BisectionRoot(fun, a, b);

% Display the result for Bisection Method

fprintf('Bisection Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n\n', Xs_bisection);

% Part (b): Secant Method

x1 = 0; % First initial guess

x2 = 1; % Second initial guess

Xs_secant = SecantMethod(fun, x1, x2);


% Display the result for Secant Method

fprintf('Secant Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n\n', Xs_secant);

% Part (c): Newton's Method

x0 = 1; % Initial guess

Xs_newton = NewtonMethod(fun, x0);

% Display the result for Newton's Method

fprintf('Newton''s Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n', Xs_newton);

% Define the BisectionRoot function for part (a)

function Xs = BisectionRoot(Fun, a, b)

tol = 1e-6; % Tolerance for stopping criterion

maxIter = 3; % Limiting iterations to 3 as per the problem statement

% Check if a and b bracket the root (i.e., f(a) and f(b) must have opposite signs)

Fa = Fun(a);

Fb = Fun(b);

if Fa * Fb > 0

error('Error: The function must have opposite signs at points a and b.');

end

% Perform the bisection method for the first 3 iterations

for i = 1:maxIter

Xs = (a + b) / 2; % Midpoint of a and b
Fx = Fun(Xs); % Evaluate f(x) at the midpoint

% Display current iteration

fprintf('Bisection Iteration %d: a = %.6f, b = %.6f, Xs = %.6f, f(Xs) = %.6f\n', ...

i, a, b, Xs, Fx);

% Update the interval [a, b] based on the sign of f(Xs)

if Fa * Fx < 0

b = Xs; % The root is in the left subinterval

else

a = Xs; % The root is in the right subinterval

Fa = Fx; % Update Fa to avoid recalculating Fun(a)

end

end

end

% Define the SecantMethod function for part (b)

function Xs = SecantMethod(Fun, x1, x2)

tol = 1e-6; % Tolerance for stopping criterion

maxIter = 3; % Limiting iterations to 3 as per the problem statement

for i = 1:maxIter

% Calculate the function values at x1 and x2

f1 = Fun(x1);

f2 = Fun(x2);
% Calculate the next approximation using the secant method formula

Xs = x2 - f2 * (x2 - x1) / (f2 - f1);

% Display current iteration

fprintf('Secant Iteration %d: x1 = %.6f, x2 = %.6f, Xs = %.6f, f(Xs) = %.6f\n', ...

i, x1, x2, Xs, Fun(Xs));

% Update x1 and x2 for the next iteration

x1 = x2;

x2 = Xs;

end

end

% Define the NewtonMethod function for part (c)

function Xs = NewtonMethod(Fun, x0)

tol = 1e-6; % Tolerance for stopping criterion

maxIter = 3; % Limiting iterations to 3 as per the problem statement

% Define the derivative of the function f(x) = x - 2*exp(-x)

dFun = @(x) 1 + 2*exp(-x); % Derivative of f(x)

for i = 1:maxIter

% Evaluate function and its derivative at x0

f0 = Fun(x0);
df0 = dFun(x0);

% Update approximation using Newton's method formula

Xs = x0 - f0 / df0;

% Display current iteration

fprintf('Newton Iteration %d: x0 = %.6f, Xs = %.6f, f(Xs) = %.6f\n', ...

i, x0, Xs, Fun(Xs));

% Update x0 for the next iteration

x0 = Xs;

end

end

Bisection Iteration 1: a = 0.000000, b = 1.000000, Xs = 0.500000, f(Xs) = -0.713061

Bisection Iteration 2: a = 0.500000, b = 1.000000, Xs = 0.750000, f(Xs) = -0.194733

Bisection Iteration 3: a = 0.750000, b = 1.000000, Xs = 0.875000, f(Xs) = 0.041276

Bisection Method: The root of the equation x - 2*exp(-x) = 0 is approximately: 0.875000

Secant Iteration 1: x1 = 0.000000, x2 = 1.000000, Xs = 0.883298, f(Xs) = 0.056464

Secant Iteration 2: x1 = 1.000000, x2 = 0.883298, Xs = 0.851584, f(Xs) = -0.001892

Secant Iteration 3: x1 = 0.883298, x2 = 0.851584, Xs = 0.852613, f(Xs) = 0.000013

Secant Method: The root of the equation x - 2*exp(-x) = 0 is approximately: 0.852613

Newton Iteration 1: x0 = 1.000000, Xs = 0.847766, f(Xs) = -0.008975

Newton Iteration 2: x0 = 0.847766, Xs = 0.852600, f(Xs) = -0.000010


Newton Iteration 3: x0 = 0.852600, Xs = 0.852606, f(Xs) = -0.000000

Newton's Method: The root of the equation x - 2*exp(-x) = 0 is approximately: 0.852606

Q7

function QuadSecRootTest()

% Test QuadSecRoot on problem 3.2 and 3.3

% Problem 3.2: Solve f(x) = x - 2*exp(-x) = 0 using QuadSecRoot method

fprintf('Solving Problem 3.2: f(x) = x - 2*exp(-x)\n');

Fun_3_2 = @(x) x - 2*exp(-x);

[root_3_2, iter_3_2] = QuadSecRoot(Fun_3_2, 0, 1);

fprintf('Root of Problem 3.2 is: %.6f, found in %d iterations\n\n', root_3_2, iter_3_2);

% Problem 3.3: Solve x = 3r/4 for the angle a, where x = sin(a) / a - 3/4

fprintf('Solving Problem 3.3: sin(a) / a - 3/4 = 0\n');

Fun_3_3 = @(a) sin(a) / a - 3/4;

[root_3_3, iter_3_3] = QuadSecRoot(Fun_3_3, 0.5, 1.5);

fprintf('Root of Problem 3.3 is: %.6f, found in %d iterations\n', root_3_3, iter_3_3);

end

function [Xs, iterations] = QuadSecRoot(Fun, a, b)

% QuadSecRoot: Solves a nonlinear equation using the proposed new method

% Inputs: Fun - function handle for f(x)

% a - left boundary of the interval

% b - right boundary of the interval

% Outputs: Xs - the solution (root)

% iterations - number of iterations performed


% Maximum number of iterations and tolerance

maxIter = 100;

tolerance = 1e-6;

% Initialize iteration counter

iterations = 0;

% Main iteration loop

while iterations < maxIter

% Midpoint of the interval

xNS = (a + b) / 2;

% Divide the interval into four equal sections

quarterLength = (b - a) / 4;

x1 = a + quarterLength;

x2 = a + 2 * quarterLength;

x3 = a + 3 * quarterLength;

% Calculate function values at the new points

f_a = Fun(a);

f_x1 = Fun(x1);

f_x2 = Fun(x2);

f_x3 = Fun(x3);

f_b = Fun(b);
% Determine the new interval that brackets the root

if f_a * f_x1 < 0

b = x1;

elseif f_x1 * f_x2 < 0

a = x1;

b = x2;

elseif f_x2 * f_x3 < 0

a = x2;

b = x3;

else

a = x3;

end

% Increment iteration counter

iterations = iterations + 1;

% Check for convergence (tolerance)

if abs(b - a) < tolerance * abs(xNS)

Xs = xNS;

return;

end

end

% If no solution found within max iterations

error('Solution not obtained within %d iterations', maxIter);

end
% Run the test function to solve the equations

QuadSecRootTest();

Solving Problem 3.2: f(x) = x - 2*exp(-x)

Root of Problem 3.2 is: 0.852605, found in 11 iterations

Solving Problem 3.3: sin(a) / a - 3/4 = 0

Root of Problem 3.3 is: 1.275698, found in 10 iterations

Q18

function LnTest()

% Function to test the Ln function with various inputs

% Test cases

test_values = [510, 1.35, 1, -7]; % Values to test

for i = 1:length(test_values)

p = test_values(i);

fprintf('Calculating natural logarithm of %.2f:\n', p);

try

ln_value = Ln(p);

fprintf('The natural logarithm of %.2f is: %.6f\n\n', p, ln_value);

catch ME

fprintf('Error: %s\n\n', ME.message);


end

end

end

function x = Ln(p)

% Function to calculate the natural logarithm using the bisection method

% Input: p - the number whose natural logarithm is to be determined

% Output: x - the estimated natural logarithm of p

% Check if the input number is positive

if p <= 0

error('Input must be a positive number.');

end

% Define constants

e0 = exp(0); % e^0

e1 = exp(1); % e^1

% Determine the initial bounds based on the value of p

if b > e1

a = e^0; % e^0

b = p; % p

else

a = -1 / p; % -1/p

b = e^0; % e^0

end
% Initialize variables

maxIter = 100; % Maximum number of iterations

tolerance = 1e-6; % Tolerance for stopping criterion

% Bisection method iterations

for n = 1:maxIter

% Midpoint

c = (a + b) / 2;

f_a = exp(a) - p; % f(a)

f_c = exp(c) - p; % f(c)

% Display current iteration

fprintf('Iteration %d: a = %.6f, b = %.6f, c = %.6f, f(c) = %.6f\n', n, a, b, c, f_c);

% Check for convergence

if abs(b - a)/2 < tolerance

fprintf('Converged to the solution: ln(%.2f) = %.6f\n', p, c);

x = c; % Return the result

return; % Exit the function

end

% Update the bounds based on the sign of f(c)

if f_a * f_c < 0

b = c; % Root is in [a, c]

else
a = c; % Root is in [c, b]

end

end

% If the maximum number of iterations is reached without convergence

error('Solution not obtained in %d iterations.', maxIter);

end

% Run the test function

LnTest();

Calculating natural logarithm of 510.00:

Error: Unrecognized function or variable 'b'.

Calculating natural logarithm of 1.35:

Error: Unrecognized function or variable 'b'.

Calculating natural logarithm of 1.00:

Error: Unrecognized function or variable 'b'.

Calculating natural logarithm of -7.00:

Error: Input must be a positive number.

% Main script

clc;

clear;
% Problem 3.2: Define and solve using QuadSecRoot

a = 0;

b = 1;

% Solve for Problem 3.2 using QuadSecRoot

root_3_2 = QuadSecRoot(@f_3_2, a, b);

fprintf('The root of f(x) = x - 2exp(-x) is %.6f\n', root_3_2);

% Problem 3.3: Define and solve using QuadSecRoot

a = 0.5;

b = 1.5;

% Solve for Problem 3.3 using QuadSecRoot

root_3_3 = QuadSecRoot(@f_3_3, a, b);

fprintf('The root of the equation for the centroid is alpha = %.6f radians\n', root_3_3);

% -------------------------------------------------------------

% Function definitions

% -------------------------------------------------------------

% QuadSecRoot function

function Xs = QuadSecRoot(Fun, a, b)

max_iterations = 100;

tolerance = 1e-6;

if Fun(a) * Fun(b) > 0


error('Initial points a and b must bracket the root.');

end

for iter = 1:max_iterations

x_mid = (a + b) / 2;

x1 = a + (b - a) / 4;

x2 = a + 2 * (b - a) / 4;

x3 = a + 3 * (b - a) / 4;

if Fun(a) * Fun(x1) < 0

b = x1;

elseif Fun(x1) * Fun(x2) < 0

a = x1;

b = x2;

elseif Fun(x2) * Fun(x3) < 0

a = x2;

b = x3;

else

a = x3;

end

Xs = (a + b) / 2;

if abs(b - a) < tolerance * abs(Xs)

fprintf('Converged after %d iterations.\n', iter);


return;

end

end

error('The method did not converge within the maximum number of iterations.');

end

% Problem 3.2: Define the function f(x) = x - 2exp(-x)

function y = f_3_2(x)

y = x - 2 * exp(-x);

end

% Problem 3.3: Define the function for the centroid equation

function y = f_3_3(alpha)

y = (sin(alpha) / alpha) - 0.75;

end

Converged after 11 iterations.

The root of f(x) = x - 2exp(-x) is 0.852605

Converged after 10 iterations.

The root of the equation for the centroid is alpha = 1.275698 radians

Q20
function Xs = BiRegRoot(Fun, a, b, ErrMax)
% Check if a and b are on opposite sides of the root
if Fun(a) * Fun(b) > 0
error('The function must have opposite signs at points a and b.');
end

% Initialize variables
iteration = 0; % Counter for iterations
max_iterations = 100; % Maximum allowed iterations

fprintf('Iteration\tBisection Estimate\tRegula Falsi Estimate\tError\n'); % Header for output

% First estimate using Bisection Method


while iteration < max_iterations
% Bisection Method Estimate
Xs_bisect = (a + b) / 2; % Midpoint
% Calculate function values
f_a = Fun(a);
f_b = Fun(b);
f_Xs_bisect = Fun(Xs_bisect);

% Update intervals based on Bisection


if f_Xs_bisect == 0 % Exact root found
Xs = Xs_bisect;
fprintf('Exact root found: %.6f\n', Xs);
return;
elseif f_a * f_Xs_bisect < 0
b = Xs_bisect; % Update b to the midpoint
else
a = Xs_bisect; % Update a to the midpoint
end

% Now use Regula Falsi Method for the next estimate


Xs_regula = (a * Fun(b) - b * Fun(a)) / (Fun(b) - Fun(a)); % Regula Falsi Estimate
f_Xs_regula = Fun(Xs_regula);

% Calculate estimated relative error (Eq. (3.9))


estimated_error = abs((Xs_regula - Xs_bisect) / Xs_regula);

% Display current iteration results


fprintf('%d\t\t%.6f\t\t\t%.6f\t\t\t%.6e\n', iteration + 1, Xs_bisect, Xs_regula, estimated_error);

% Check if the estimated error is within the specified tolerance


if estimated_error < ErrMax
Xs = Xs_regula; % Assign the result to Xs
return; % Return the root if tolerance is achieved
end

% Update intervals for Regula Falsi method


if f_Xs_regula * f_a < 0
b = Xs_regula; % Root lies in the left subinterval
else
a = Xs_regula; % Root lies in the right subinterval
end

% Increment iteration counter


iteration = iteration + 1;
end

% If max iterations reached, display an error message


if iteration == max_iterations
error('Solution not found within the maximum number of iterations.');
end
end
% Define the function for Problem 3.3
f = @(x) (sin(x) / x) - (3 / 4); % Replace this with the actual function

% Initial values for a and b


a = 0.1;
b = 1.4;

% Set the maximum error (tolerance)


ErrMax = 0.00001;

% Call the BiRegRoot function to find the root


Xs = BiRegRoot(f, a, b, ErrMax);

% Display the final solution


fprintf('The root of the equation is: %.6f\n', Xs);

Iteration Bisection Estimate Regula Falsi Estimate Error


1 0.750000 1.253777 4.018074e-01
2 1.326888 1.275414 4.035904e-02
3 1.301151 1.275696 1.995371e-02
4 1.288424 1.275698 9.975407e-03
5 1.282061 1.275698 4.987699e-03
6 1.278880 1.275698 2.493849e-03
7 1.277289 1.275698 1.246925e-03
8 1.276493 1.275698 6.232067e-04
9 1.276494 1.275698 6.233344e-04
10 1.276494 1.275698 6.233344e-04
11 1.276494 1.275698 6.233344e-04
12 1.276494 1.275698 6.233344e-04
13 1.276494 1.275698 6.233344e-04
14 1.276494 1.275698 6.233344e-04
15 1.276494 1.275698 6.233344e-04
16 1.276494 1.275698 6.233344e-04
17 1.276494 1.275698 6.233344e-04
18 1.276494 1.275698 6.233344e-04
19 1.276494 1.275698 6.233344e-04
20 1.276494 1.275698 6.233344e-04
21 1.276494 1.275698 6.233344e-04
22 1.276494 1.275698 6.233344e-04
23 1.276494 1.275698 6.233344e-04
24 1.276494 1.275698 6.233344e-04
25 1.276494 1.275698 6.233344e-04
26 1.276494 1.275698 6.233344e-04
27 1.276494 1.275698 6.233344e-04
28 1.276494 1.275698 6.233344e-04
29 1.276494 1.275698 6.233344e-04
30 1.276494 1.275698 6.233344e-04
31 1.276494 1.275698 6.233344e-04
32 1.276494 1.275698 6.233344e-04
33 1.276494 1.275698 6.233344e-04
34 1.276494 1.275698 6.233344e-04
35 1.276494 1.275698 6.233344e-04
36 1.276494 1.275698 6.233344e-04
37 1.276494 1.275698 6.233344e-04
38 1.276494 1.275698 6.233344e-04
39 1.276494 1.275698 6.233344e-04
40 1.276494 1.275698 6.233344e-04
41 1.276494 1.275698 6.233344e-04
42 1.276494 1.275698 6.233344e-04
43 1.276494 1.275698 6.233344e-04
44 1.276494 1.275698 6.233344e-04
45 1.276494 1.275698 6.233344e-04
46 1.276494 1.275698 6.233344e-04
47 1.276494 1.275698 6.233344e-04
48 1.276494 1.275698 6.233344e-04
49 1.276494 1.275698 6.233344e-04
50 1.276494 1.275698 6.233344e-04
51 1.276494 1.275698 6.233344e-04
52 1.276494 1.275698 6.233344e-04
53 1.276494 1.275698 6.233344e-04
54 1.276494 1.275698 6.233344e-04
55 1.276494 1.275698 6.233344e-04
56 1.276494 1.275698 6.233344e-04
57 1.276494 1.275698 6.233344e-04
58 1.276494 1.275698 6.233344e-04
59 1.276494 1.275698 6.233344e-04
60 1.276494 1.275698 6.233344e-04
61 1.276494 1.275698 6.233344e-04
62 1.276494 1.275698 6.233344e-04
63 1.276494 1.275698 6.233344e-04
64 1.276494 1.275698 6.233344e-04
65 1.276494 1.275698 6.233344e-04
66 1.276494 1.275698 6.233344e-04
67 1.276494 1.275698 6.233344e-04
68 1.276494 1.275698 6.233344e-04
69 1.276494 1.275698 6.233344e-04
70 1.276494 1.275698 6.233344e-04
71 1.276494 1.275698 6.233344e-04
72 1.276494 1.275698 6.233344e-04
73 1.276494 1.275698 6.233344e-04
74 1.276494 1.275698 6.233344e-04
75 1.276494 1.275698 6.233344e-04
76 1.276494 1.275698 6.233344e-04
77 1.276494 1.275698 6.233344e-04
78 1.276494 1.275698 6.233344e-04
79 1.276494 1.275698 6.233344e-04
80 1.276494 1.275698 6.233344e-04
81 1.276494 1.275698 6.233344e-04
82 1.276494 1.275698 6.233344e-04
83 1.276494 1.275698 6.233344e-04
84 1.276494 1.275698 6.233344e-04
85 1.276494 1.275698 6.233344e-04
86 1.276494 1.275698 6.233344e-04
87 1.276494 1.275698 6.233344e-04
88 1.276494 1.275698 6.233344e-04
89 1.276494 1.275698 6.233344e-04
90 1.276494 1.275698 6.233344e-04
91 1.276494 1.275698 6.233344e-04
92 1.276494 1.275698 6.233344e-04
93 1.276494 1.275698 6.233344e-04
94 1.276494 1.275698 6.233344e-04
95 1.276494 1.275698 6.233344e-04
96 1.276494 1.275698 6.233344e-04
97 1.276494 1.275698 6.233344e-04
98 1.276494 1.275698 6.233344e-04
99 1.276494 1.275698 6.233344e-04
100 1.276494 1.275698 6.233344e-04

Q21
function Xs = NewtonSol(Fun, FunDer, Xest)
% Parameters
ErrMax = 1e-6; % Maximum error tolerance
max_iterations = 100; % Maximum iterations allowed
iteration = 0; % Initialize iteration counter

% Initial guess
Xs = Xest;

fprintf('Iteration\tSolution\t\tError\n'); % Header for output

while iteration < max_iterations


% Calculate function value and derivative
f_Xs = Fun(Xs);
fDer_Xs = FunDer(Xs);

% Check if derivative is zero to avoid division by zero


if abs(fDer_Xs) < eps
error('Derivative is zero. No solution found.');
end

% Newton-Raphson update
Xs_new = Xs - f_Xs / fDer_Xs;

% Estimated relative error


estimated_error = abs((Xs_new - Xs) / Xs_new);

% Display current iteration results


fprintf('%d\t\t%.6f\t\t%.6e\n', iteration + 1, Xs_new, estimated_error);

% Check for convergence


if estimated_error < ErrMax
Xs = Xs_new; % Update the solution
return; % Return the solution if tolerance is met
end

% Update the current solution


Xs = Xs_new;

% Increment iteration counter


iteration = iteration + 1;
end

% If max iterations reached, display an error message


error('Solution not found within the maximum number of iterations.');
end

% Define the function and its derivative for Example 3-2


Fun = @(x) x^3 - x - 2; % Example function
FunDer = @(x) 3*x^2 - 1; % Derivative of the function
% Initial guess
Xest = 1;

% Call the NewtonSol function to find the root


Xs = NewtonSol(Fun, FunDer, Xest);

% Display the final solution


fprintf('The root of the equation is: %.6f\n', Xs);

% Define the function and its derivative for Example 3-2


Fun = @(x) x^3 - x - 2; % Example function
FunDer = @(x) 3*x^2 - 1; % Derivative of the function

% Initial guess
Xest = 1;

% Maximum error and iterations


ErrMax = 1e-6; % Error tolerance
imax = 100; % Maximum iterations

% Call the NewtonRootMod function to find the root


[Xs, FXs, iact] = NewtonRootMod(Fun, FunDer, Xest, ErrMax, imax);

% Display the final solution and function value


fprintf('The root of the equation is: %.6f\n', Xs);
fprintf('The function value at the root is: %.6f\n', FXs);
fprintf('The actual number of iterations performed: %d\n', iact);

Iteration Solution Error


1 2.000000 5.000000e-01
2 1.636364 2.222222e-01
3 1.530392 6.924473e-02
4 1.521441 5.882965e-03
5 1.521380 4.059171e-05
6 1.521380 1.924950e-09
The root of the equation is: 1.521380

Q22

function Xs = RegulaRoot(Fun, a, b, ErrMax)

% Check if a and b are on opposite sides of the root

if Fun(a) * Fun(b) > 0

error('The function must have opposite signs at points a and b.');

end

% Initialize variables
Xs_prev = a; % Previous estimate (starting as a)

Xs = (a * Fun(b) - b * Fun(a)) / (Fun(b) - Fun(a)); % First estimate using Regula Falsi formula

iteration = 0; % Counter for iterations

max_iterations = 100; % Maximum allowed iterations

fprintf('Iteration\tEstimate\t\tError\n'); % Header for output

while iteration < max_iterations

% Update Xs using the Regula Falsi formula

Xs = (a * Fun(b) - b * Fun(a)) / (Fun(b) - Fun(a));

% Calculate the estimated relative error using Eq. (3.9)

estimated_error = abs((Xs - Xs_prev) / Xs);

% Display current iteration, estimate, and error

fprintf('%d\t\t%.6f\t\t%.6e\n', iteration + 1, Xs, estimated_error);

% Check if the error is smaller than the specified tolerance (ErrMax)

if estimated_error < ErrMax

return; % Return the root if tolerance is achieved

end

% Update a or b depending on the sign of f(Xs)

if Fun(Xs) * Fun(a) < 0

b = Xs; % Root lies in the left subinterval

else
a = Xs; % Root lies in the right subinterval

end

% Update previous Xs for next iteration

Xs_prev = Xs;

% Increment iteration counter

iteration = iteration + 1;

end

% If max iterations reached, display an error message

if iteration == max_iterations

error('Solution not found within the maximum number of iterations.');

end

end

% Define the function for Problem 3.3

f = @(a) (sin(a) / a) - (3 / 4);

% Initial values for a and b

a = 0.1;

b = 1.4;

% Set the maximum error (tolerance)

ErrMax = 1e-6;
% Call the RegulaRoot function to find the root

Xs = RegulaRoot(f, a, b, ErrMax);

% Display the final solution

fprintf('The root of the equation is: %.6f\n', Xs);

Iteration Estimate Error

1 1.196430 9.164180e-01

2 1.273226 6.031646e-02

3 1.275625 1.880241e-03

4 1.275696 5.583114e-05

5 1.275698 1.655311e-06

6 1.275698 4.907534e-08

The root of the equation is: 1.275698

Q23

function [Xs, FXs, iact] = NewtonRootMod(Fun, FunDer, Xest, ErrMax, imax)

% Parameters

iteration = 0; % Initialize iteration counter

Xs = Xest; % Initial guess

fprintf('Iteration\tSolution\t\tFunction Value\t\tEstimated Error\n'); % Header for output

while iteration < imax

% Calculate function value and derivative


f_Xs = Fun(Xs);

fDer_Xs = FunDer(Xs);

% Check if derivative is zero to avoid division by zero

if abs(fDer_Xs) < eps

error('Derivative is zero. No solution found.');

end

% Newton-Raphson update

Xs_new = Xs - f_Xs / fDer_Xs;

% Estimated relative error

estimated_error = abs((Xs_new - Xs) / Xs_new);

% Display current iteration results

fprintf('%d\t\t%.6f\t\t%.6f\t\t%.6e\n', iteration + 1, Xs_new, f_Xs, estimated_error);

% Check for convergence

if estimated_error < ErrMax

FXs = Fun(Xs_new); % Value of the function at the root

iact = iteration + 1; % Actual number of iterations

Xs = Xs_new; % Update the solution

return; % Return the results if tolerance is met

end

% Update the current solution


Xs = Xs_new;

% Increment iteration counter

iteration = iteration + 1;

end

% If max iterations reached, display an error message

error('Solution not found within the maximum number of iterations.');

end

% Define the function and its derivative for Example 3-2

Fun = @(x) x^3 - x - 2; % Example function

FunDer = @(x) 3*x^2 - 1; % Derivative of the function

% Initial guess

Xest = 1;

% Maximum error and iterations

ErrMax = 1e-6; % Error tolerance

imax = 100; % Maximum iterations

% Call the NewtonRootMod function to find the root

[Xs, FXs, iact] = NewtonRootMod(Fun, FunDer, Xest, ErrMax, imax);

Iteration Solution Function Value Estimated Error

1 2.000000 -2.000000 5.000000e-01

2 1.636364 4.000000 2.222222e-01


3 1.530392 0.745304 6.924473e-02

4 1.521441 0.053939 5.882965e-03

5 1.521380 0.000367 4.059171e-05

6 1.521380 0.000000 1.924950e-09

Q24

function main()

% Example usage of SteffensenRoot to solve equations from Problems 3.2 and 3.3

% Problem 3.2: Solve f(x) = x - 2*x^2

Fun1 = @(x) x - 2*x^2; % Define the function for Problem 3.2

Xest1 = 0.5; % Initial estimate

Xs1 = SteffensenRoot(Fun1, Xest1);

fprintf('Problem 3.2: The root is %.6f\n', Xs1);

% Problem 3.3: Solve the equation for x = -sqrt(r) * sin(a)

Fun2 = @(a) -a + (3*pi/4); % Rearranging the original equation

Xest2 = 1; % Initial estimate

Xs2 = SteffensenRoot(Fun2, Xest2);

fprintf('Problem 3.3: The root is %.6f\n', Xs2);

end

function Xs = SteffensenRoot(Fun, Xest)

% Function to find the root using Steffensen's method

ErrMax = 1e-6; % Maximum error tolerance

imax = 100; % Maximum number of iterations


iteration = 0; % Initialize iteration counter

Xs = Xest; % Initial guess

fprintf('Iteration\tSolution\t\tEstimated Error\n'); % Header for output

while iteration < imax

% Calculate function values

f_Xs = Fun(Xs);

f_Xs_plus_f_Xs = Fun(Xs + f_Xs);

% Steffensen's update

Xs_new = Xs - (f_Xs^2) / (f_Xs_plus_f_Xs - f_Xs);

% Estimated relative error

estimated_error = abs((Xs_new - Xs) / Xs_new);

% Display current iteration results

fprintf('%d\t\t%.6f\t\t%.6e\n', iteration + 1, Xs_new, estimated_error);

% Check for convergence

if estimated_error < ErrMax

return; % Return if tolerance is met

end

% Update the current solution


Xs = Xs_new;

% Increment iteration counter

iteration = iteration + 1;

end

% If max iterations reached, display an error message

error('Solution not found within the maximum number of iterations.');

end

% Call the main function to execute

main();

Iteration Solution Estimated Error

1 NaN NaN

2 NaN NaN

3 NaN NaN

4 NaN NaN

5 NaN NaN

6 NaN NaN

7 NaN NaN

8 NaN NaN

9 NaN NaN

10 NaN NaN

11 NaN NaN

12 NaN NaN
13 NaN NaN

14 NaN NaN

15 NaN NaN

16 NaN NaN

17 NaN NaN

18 NaN NaN

19 NaN NaN

20 NaN NaN

21 NaN NaN

22 NaN NaN

23 NaN NaN

24 NaN NaN

25 NaN NaN

26 NaN NaN

27 NaN NaN

28 NaN NaN

29 NaN NaN

30 NaN NaN

31 NaN NaN

32 NaN NaN

33 NaN NaN

34 NaN NaN

35 NaN NaN

36 NaN NaN

37 NaN NaN

38 NaN NaN
39 NaN NaN

40 NaN NaN

41 NaN NaN

42 NaN NaN

43 NaN NaN

44 NaN NaN

45 NaN NaN

46 NaN NaN

47 NaN NaN

48 NaN NaN

49 NaN NaN

50 NaN NaN

51 NaN NaN

52 NaN NaN

53 NaN NaN

54 NaN NaN

55 NaN NaN

56 NaN NaN

57 NaN NaN

58 NaN NaN

59 NaN NaN

60 NaN NaN

61 NaN NaN

62 NaN NaN

63 NaN NaN

64 NaN NaN
65 NaN NaN

66 NaN NaN

67 NaN NaN

68 NaN NaN

69 NaN NaN

70 NaN NaN

71 NaN NaN

72 NaN NaN

73 NaN NaN

74 NaN NaN

75 NaN NaN

76 NaN NaN

77 NaN NaN

78 NaN NaN

79 NaN NaN

80 NaN NaN

81 NaN NaN

82 NaN NaN

83 NaN NaN

84 NaN NaN

85 NaN NaN

86 NaN NaN

87 NaN NaN

88 NaN NaN

89 NaN NaN

90 NaN NaN
91 NaN NaN

92 NaN NaN

93 NaN NaN

94 NaN NaN

95 NaN NaN

96 NaN NaN

97 NaN NaN

98 NaN NaN

99 NaN NaN

100 NaN NaN

Q25

function R = BisecAllRoots(Fun, a, b, TolMax)

% Initialize the output array of roots

R = [];

% Divide the domain [a, b] into 10 equal subintervals

n_subintervals = 10;

h = (b - a) / n_subintervals;

% Step 1: Search for roots in subintervals

R = findRootsInSubintervals(Fun, a, b, n_subintervals, TolMax, R);

% Step 2: Repeat for 100 subintervals


n_subintervals = 100;

R = findRootsInSubintervals(Fun, a, b, n_subintervals, TolMax, R);

% Step 3: Refine the search by multiplying subintervals by 10 if needed

while true

prevNumRoots = length(R); % Number of roots already found

n_subintervals = n_subintervals * 10; % Multiply subintervals by 10

R = findRootsInSubintervals(Fun, a, b, n_subintervals, TolMax, R);

% If no new roots are found, stop the loop

if length(R) == prevNumRoots

break;

end

end

end

function R = findRootsInSubintervals(Fun, a, b, n_subintervals, TolMax, R)

% Divide the domain into equal subintervals

h = (b - a) / n_subintervals;

for i = 1:n_subintervals

x1 = a + (i-1) * h;

x2 = a + i * h;

% Check if there is a sign change (indicating a root) in the subinterval

if Fun(x1) * Fun(x2) < 0


% Use bisection method to find the root

[root, iterations] = Bisection(Fun, x1, x2, TolMax);

% Avoid adding duplicate roots (within the tolerance)

if isempty(R) || all(abs(R - root) > TolMax)

R = [R, root]; % Append the root to the output vector

fprintf('Root found: %.6f, Iterations: %d\n', root, iterations); % Display root and iterations

end

end

end

end

function [root, iterations] = Bisection(Fun, x1, x2, TolMax)

% Initialize variables

iterations = 0;

while abs(x2 - x1) > TolMax

x_mid = (x1 + x2) / 2;

if Fun(x1) * Fun(x_mid) < 0

x2 = x_mid;

else

x1 = x_mid;

end

iterations = iterations + 1; % Count the iterations

fprintf('Iteration %d: Midpoint = %.6f, Error = %.6f\n', iterations, x_mid, abs(x2 - x1)); % Display iteration info

end
% Return the midpoint as the root

root = (x1 + x2) / 2;

end

% Define the function f(x)

Fun = @(x) x^4 - 5.5*x^3 - 7.2*x^2 + 43*x + 36;

% Define the domain and tolerance

a = -10;

b = 10;

TolMax = 1e-4;

% Call the BisecAllRoots function to find all roots

roots = BisecAllRoots(Fun, a, b, TolMax);

% Display the roots

disp('Roots:');

disp(roots);

Iteration 1: Midpoint = -3.000000, Error = 1.000000

Iteration 2: Midpoint = -2.500000, Error = 0.500000

Iteration 3: Midpoint = -2.250000, Error = 0.250000

Iteration 4: Midpoint = -2.375000, Error = 0.125000

Iteration 5: Midpoint = -2.437500, Error = 0.062500

Iteration 6: Midpoint = -2.406250, Error = 0.031250

Iteration 7: Midpoint = -2.390625, Error = 0.015625


Iteration 8: Midpoint = -2.398438, Error = 0.007812

Iteration 9: Midpoint = -2.394531, Error = 0.003906

Iteration 10: Midpoint = -2.392578, Error = 0.001953

Iteration 11: Midpoint = -2.391602, Error = 0.000977

Iteration 12: Midpoint = -2.392090, Error = 0.000488

Iteration 13: Midpoint = -2.392334, Error = 0.000244

Iteration 14: Midpoint = -2.392456, Error = 0.000122

Iteration 15: Midpoint = -2.392517, Error = 0.000061

Root found: -2.392548, Iterations: 15

Iteration 1: Midpoint = -1.000000, Error = 1.000000

Iteration 2: Midpoint = -0.500000, Error = 0.500000

Iteration 3: Midpoint = -0.750000, Error = 0.250000

Iteration 4: Midpoint = -0.875000, Error = 0.125000

Iteration 5: Midpoint = -0.812500, Error = 0.062500

Iteration 6: Midpoint = -0.781250, Error = 0.031250

Iteration 7: Midpoint = -0.796875, Error = 0.015625

Iteration 8: Midpoint = -0.804688, Error = 0.007812

Iteration 9: Midpoint = -0.808594, Error = 0.003906

Iteration 10: Midpoint = -0.806641, Error = 0.001953

Iteration 11: Midpoint = -0.805664, Error = 0.000977

Iteration 12: Midpoint = -0.805176, Error = 0.000488

Iteration 13: Midpoint = -0.805420, Error = 0.000244

Iteration 14: Midpoint = -0.805298, Error = 0.000122

Iteration 15: Midpoint = -0.805237, Error = 0.000061

Root found: -0.805206, Iterations: 15

Iteration 1: Midpoint = 3.000000, Error = 1.000000


Iteration 2: Midpoint = 3.500000, Error = 0.500000

Iteration 3: Midpoint = 3.750000, Error = 0.250000

Iteration 4: Midpoint = 3.875000, Error = 0.125000

Iteration 5: Midpoint = 3.812500, Error = 0.062500

Iteration 6: Midpoint = 3.843750, Error = 0.031250

Iteration 7: Midpoint = 3.859375, Error = 0.015625

Iteration 8: Midpoint = 3.867188, Error = 0.007812

Iteration 9: Midpoint = 3.871094, Error = 0.003906

Iteration 10: Midpoint = 3.873047, Error = 0.001953

Iteration 11: Midpoint = 3.874023, Error = 0.000977

Iteration 12: Midpoint = 3.873535, Error = 0.000488

Iteration 13: Midpoint = 3.873779, Error = 0.000244

Iteration 14: Midpoint = 3.873657, Error = 0.000122

Iteration 15: Midpoint = 3.873596, Error = 0.000061

Root found: 3.873627, Iterations: 15

Iteration 1: Midpoint = 5.000000, Error = 1.000000

Iteration 2: Midpoint = 4.500000, Error = 0.500000

Iteration 3: Midpoint = 4.750000, Error = 0.250000

Iteration 4: Midpoint = 4.875000, Error = 0.125000

Iteration 5: Midpoint = 4.812500, Error = 0.062500

Iteration 6: Midpoint = 4.843750, Error = 0.031250

Iteration 7: Midpoint = 4.828125, Error = 0.015625

Iteration 8: Midpoint = 4.820312, Error = 0.007812

Iteration 9: Midpoint = 4.824219, Error = 0.003906

Iteration 10: Midpoint = 4.822266, Error = 0.001953

Iteration 11: Midpoint = 4.823242, Error = 0.000977


Iteration 12: Midpoint = 4.823730, Error = 0.000488

Iteration 13: Midpoint = 4.823975, Error = 0.000244

Iteration 14: Midpoint = 4.824097, Error = 0.000122

Iteration 15: Midpoint = 4.824158, Error = 0.000061

Root found: 4.824188, Iterations: 15

Iteration 1: Midpoint = -2.300000, Error = 0.100000

Iteration 2: Midpoint = -2.350000, Error = 0.050000

Iteration 3: Midpoint = -2.375000, Error = 0.025000

Iteration 4: Midpoint = -2.387500, Error = 0.012500

Iteration 5: Midpoint = -2.393750, Error = 0.006250

Iteration 6: Midpoint = -2.390625, Error = 0.003125

Iteration 7: Midpoint = -2.392187, Error = 0.001562

Iteration 8: Midpoint = -2.392969, Error = 0.000781

Iteration 9: Midpoint = -2.392578, Error = 0.000391

Iteration 10: Midpoint = -2.392383, Error = 0.000195

Iteration 11: Midpoint = -2.392480, Error = 0.000098

Iteration 1: Midpoint = -0.900000, Error = 0.100000

Iteration 2: Midpoint = -0.850000, Error = 0.050000

Iteration 3: Midpoint = -0.825000, Error = 0.025000

Iteration 4: Midpoint = -0.812500, Error = 0.012500

Iteration 5: Midpoint = -0.806250, Error = 0.006250

Iteration 6: Midpoint = -0.803125, Error = 0.003125

Iteration 7: Midpoint = -0.804687, Error = 0.001563

Iteration 8: Midpoint = -0.805469, Error = 0.000781

Iteration 9: Midpoint = -0.805078, Error = 0.000391

Iteration 10: Midpoint = -0.805273, Error = 0.000195


Iteration 11: Midpoint = -0.805176, Error = 0.000098

Iteration 1: Midpoint = 3.900000, Error = 0.100000

Iteration 2: Midpoint = 3.850000, Error = 0.050000

Iteration 3: Midpoint = 3.875000, Error = 0.025000

Iteration 4: Midpoint = 3.862500, Error = 0.012500

Iteration 5: Midpoint = 3.868750, Error = 0.006250

Iteration 6: Midpoint = 3.871875, Error = 0.003125

Iteration 7: Midpoint = 3.873438, Error = 0.001562

Iteration 8: Midpoint = 3.874219, Error = 0.000781

Iteration 9: Midpoint = 3.873828, Error = 0.000391

Iteration 10: Midpoint = 3.873633, Error = 0.000195

Iteration 11: Midpoint = 3.873535, Error = 0.000098

Iteration 1: Midpoint = 4.900000, Error = 0.100000

Iteration 2: Midpoint = 4.850000, Error = 0.050000

Iteration 3: Midpoint = 4.825000, Error = 0.025000

Iteration 4: Midpoint = 4.812500, Error = 0.012500

Iteration 5: Midpoint = 4.818750, Error = 0.006250

Iteration 6: Midpoint = 4.821875, Error = 0.003125

Iteration 7: Midpoint = 4.823438, Error = 0.001563

Iteration 8: Midpoint = 4.824219, Error = 0.000781

Iteration 9: Midpoint = 4.823828, Error = 0.000391

Iteration 10: Midpoint = 4.824023, Error = 0.000195

Iteration 11: Midpoint = 4.824121, Error = 0.000098

Iteration 1: Midpoint = -2.390000, Error = 0.010000

Iteration 2: Midpoint = -2.395000, Error = 0.005000

Iteration 3: Midpoint = -2.392500, Error = 0.002500


Iteration 4: Midpoint = -2.393750, Error = 0.001250

Iteration 5: Midpoint = -2.393125, Error = 0.000625

Iteration 6: Midpoint = -2.392812, Error = 0.000313

Iteration 7: Midpoint = -2.392656, Error = 0.000156

Iteration 8: Midpoint = -2.392578, Error = 0.000078

Iteration 1: Midpoint = -0.810000, Error = 0.010000

Iteration 2: Midpoint = -0.805000, Error = 0.005000

Iteration 3: Midpoint = -0.807500, Error = 0.002500

Iteration 4: Midpoint = -0.806250, Error = 0.001250

Iteration 5: Midpoint = -0.805625, Error = 0.000625

Iteration 6: Midpoint = -0.805312, Error = 0.000313

Iteration 7: Midpoint = -0.805156, Error = 0.000156

Iteration 8: Midpoint = -0.805234, Error = 0.000078

Iteration 1: Midpoint = 3.870000, Error = 0.010000

Iteration 2: Midpoint = 3.875000, Error = 0.005000

Iteration 3: Midpoint = 3.872500, Error = 0.002500

Iteration 4: Midpoint = 3.873750, Error = 0.001250

Iteration 5: Midpoint = 3.873125, Error = 0.000625

Iteration 6: Midpoint = 3.873438, Error = 0.000312

Iteration 7: Midpoint = 3.873594, Error = 0.000156

Iteration 8: Midpoint = 3.873672, Error = 0.000078

Iteration 1: Midpoint = 4.830000, Error = 0.010000

Iteration 2: Midpoint = 4.825000, Error = 0.005000

Iteration 3: Midpoint = 4.822500, Error = 0.002500

Iteration 4: Midpoint = 4.823750, Error = 0.001250

Iteration 5: Midpoint = 4.824375, Error = 0.000625


Iteration 6: Midpoint = 4.824063, Error = 0.000312

Iteration 7: Midpoint = 4.824219, Error = 0.000156

Iteration 8: Midpoint = 4.824141, Error = 0.000078

Roots:

-2.3925 -0.8052 3.8736 4.8242

Q26
% Define the function and its derivative
f = @(x) 0.5 * exp(2 + x) - 40; % f(x)
df = @(x) 0.5 * exp(2 + x); % f'(x)

% Exact solution of the equation


x_exact = log(80) - 2;

% Initial guess
x_old = 4;
max_iterations = 11;

% Preallocate table for results: iteration number, x value, TRE, ERE


results = zeros(max_iterations, 4);

% Newton's method iterations


for i = 1:max_iterations
% Calculate the new x value using Newton's method formula
x_new = x_old - f(x_old) / df(x_old);

% Calculate the True Relative Error (TRE)


TRE = abs((x_exact - x_new) / x_exact) * 100;

% Calculate the Estimated Relative Error (ERE) if i > 1


if i > 1
ERE = abs((x_new - x_old) / x_new) * 100;
else
ERE = NaN; % No ERE for the first iteration
end

% Store the results for this iteration


results(i, :) = [i, x_new, TRE, ERE];

% Update x_old for the next iteration


x_old = x_new;
end
% Display the results in a table
disp('Iteration Solution True Relative Error (%) Estimated Relative Error (%)');
disp(results);

Iteration Solution True Relative Error (%) Estimated Relative Error (%)
1.0000 3.1983 34.2680 NaN
2.0000 2.6404 10.8458 21.1305
3.0000 2.4127 1.2878 9.4365
4.0000 2.3825 0.0196 1.2680
5.0000 2.3820 0.0000 0.0195
6.0000 2.3820 0.0000 0.0000
7.0000 2.3820 0.0000 0.0000
8.0000 2.3820 0.0000 0.0000
9.0000 2.3820 0.0000 0.0000
10.0000 2.3820 0.0000 0.0000
11.0000 2.3820 0.0000 0.0000

You might also like