NM Assignment
NM Assignment
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)
% 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
Q16
% Script to solve Problem 3.2 using different methods for f(x) = x - 2*exp(-x)
a = 0; % Starting point a
b = 1; % Starting point b
fprintf('Bisection Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n\n', Xs_bisection);
fprintf('Secant Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n\n', Xs_secant);
x0 = 1; % Initial guess
fprintf('Newton''s Method: The root of the equation x - 2*exp(-x) = 0 is approximately: %.6f\n', Xs_newton);
function Xs = BisectionRoot(Fun, a, b)
% 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
for i = 1:maxIter
Xs = (a + b) / 2; % Midpoint of a and b
Fx = Fun(Xs); % Evaluate f(x) at the midpoint
i, a, b, Xs, Fx);
if Fa * Fx < 0
else
end
end
end
for i = 1:maxIter
f1 = Fun(x1);
f2 = Fun(x2);
% Calculate the next approximation using the secant method formula
x1 = x2;
x2 = Xs;
end
end
for i = 1:maxIter
f0 = Fun(x0);
df0 = dFun(x0);
Xs = x0 - f0 / df0;
x0 = Xs;
end
end
Q7
function QuadSecRootTest()
% Problem 3.3: Solve x = 3r/4 for the angle a, where x = sin(a) / a - 3/4
end
maxIter = 100;
tolerance = 1e-6;
iterations = 0;
xNS = (a + b) / 2;
quarterLength = (b - a) / 4;
x1 = a + quarterLength;
x2 = a + 2 * quarterLength;
x3 = a + 3 * quarterLength;
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
b = x1;
a = x1;
b = x2;
a = x2;
b = x3;
else
a = x3;
end
iterations = iterations + 1;
Xs = xNS;
return;
end
end
end
% Run the test function to solve the equations
QuadSecRootTest();
Q18
function LnTest()
% Test cases
for i = 1:length(test_values)
p = test_values(i);
try
ln_value = Ln(p);
catch ME
end
end
function x = Ln(p)
if p <= 0
end
% Define constants
e0 = exp(0); % e^0
e1 = exp(1); % e^1
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
for n = 1:maxIter
% Midpoint
c = (a + b) / 2;
end
b = c; % Root is in [a, c]
else
a = c; % Root is in [c, b]
end
end
end
LnTest();
% Main script
clc;
clear;
% Problem 3.2: Define and solve using QuadSecRoot
a = 0;
b = 1;
a = 0.5;
b = 1.5;
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;
end
x_mid = (a + b) / 2;
x1 = a + (b - a) / 4;
x2 = a + 2 * (b - a) / 4;
x3 = a + 3 * (b - a) / 4;
b = x1;
a = x1;
b = x2;
a = x2;
b = x3;
else
a = x3;
end
Xs = (a + b) / 2;
end
end
error('The method did not converge within the maximum number of iterations.');
end
function y = f_3_2(x)
y = x - 2 * exp(-x);
end
function y = f_3_3(alpha)
end
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
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;
% Newton-Raphson update
Xs_new = Xs - f_Xs / fDer_Xs;
% Initial guess
Xest = 1;
Q22
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
end
else
a = Xs; % Root lies in the right subinterval
end
Xs_prev = Xs;
iteration = iteration + 1;
end
if iteration == max_iterations
end
end
a = 0.1;
b = 1.4;
ErrMax = 1e-6;
% Call the RegulaRoot function to find the root
Xs = RegulaRoot(f, a, b, ErrMax);
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
Q23
% Parameters
fDer_Xs = FunDer(Xs);
end
% Newton-Raphson update
end
iteration = iteration + 1;
end
end
% Initial guess
Xest = 1;
Q24
function main()
% Example usage of SteffensenRoot to solve equations from Problems 3.2 and 3.3
end
f_Xs = Fun(Xs);
% Steffensen's update
end
iteration = iteration + 1;
end
end
main();
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
Q25
R = [];
n_subintervals = 10;
h = (b - a) / n_subintervals;
while true
if length(R) == prevNumRoots
break;
end
end
end
h = (b - a) / n_subintervals;
for i = 1:n_subintervals
x1 = a + (i-1) * h;
x2 = a + i * h;
fprintf('Root found: %.6f, Iterations: %d\n', root, iterations); % Display root and iterations
end
end
end
end
% Initialize variables
iterations = 0;
x2 = x_mid;
else
x1 = x_mid;
end
fprintf('Iteration %d: Midpoint = %.6f, Error = %.6f\n', iterations, x_mid, abs(x2 - x1)); % Display iteration info
end
% Return the midpoint as the root
end
a = -10;
b = 10;
TolMax = 1e-4;
disp('Roots:');
disp(roots);
Roots:
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)
% Initial guess
x_old = 4;
max_iterations = 11;
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