In: Computer Science
Write a program using the following root-finding methods:
Mullers Method
Use your programs to find approximations to within 10^(-4) to all
zeros of the following cubic polynomials.
Use |P_(n+1)-P_n| as a measure of the error in the
iteration. Save all of the iterations. What are your
conclusions?
(a) f(x) =
x^3-5x^2 +
2x
(b) f(x) = x^3-2x^2-5
The program has to be used with MATLAB. I'm still learning how to use the program. I would love some help and tips on solving these methods. Thank you
INPUT endpoints a, b; tolerance TOL; the
maximum number of iterations N0 =
log2[(b-a)/TOL].
OUTPUT approximate solution p or message of
failure.
Step 1 Set i = 1;
FA = f (a).
Step 2 While i ≤ N0 do Steps 3–6.
Step 3 Set p = a + (b − a)/2; (Compute
pi)
FP = f ( p).
Step 4 If FP = 0 or (b − a)/2 < TOL then
OUTPUT (p); (Procedure completed
successfully.)
STOP.
Step 5 Set i = i + 1.
Step 6 If FA · FP > 0 then set a = p; (Compute
ai, bi.)
FA = FP
else set b = p. (FA is unchanged.)
Step 7 OUTPUT (‘Method failed after N0 iterations,
N0 =’, N0);
(The procedure was unsuccessful.)
STOP
The actual code for the above question
% Find the root of f(x) using bisection method
f=@(x) x^3 -5*x^2+2*x; % If f(x) is different then we can change it accordingly.
a = 4; % Initially given the first point, It can change if we start with a different inial guess,
b = 5; % Initially given the second point, It can change if we start with different inial guess.
TOL = 10^(-4); % Given error tolerance.
No = log2[(b-a)/ε]; % Calculation of number of iteration required from error bound equation.
if f(a)*f(b)>0
disp('Wrong choice of a and b; please change the value of and b such that f(a)*f(b)<0')
else
For i=1:No %total number of iteration is No.
c=(a+b)/2; %finding the mid point of a and b.
if f(c)*f(b)>0; %checking the sign of f(c) with respect to f(b)
b=c; %if sign of f(b) and f(c) are the same then change the value of endpoint b as c.
else a=c;%if sign of f(b) and f(c) are not the same then change the value of endpoint a as c.
end
end
fprintf('Root of given equation is %f',c)
% so we got the root of our function f(x).
This is a general code, which you can refer to for your reference.
Here key points -
f(x) = x^3-5x^2 + 2x
f(x) = x^3-2x^2-5