Please solve problem 4 only, I attached problem 1 as a reference. Please solve i
ID: 3789594 • Letter: P
Question
Please solve problem 4 only, I attached problem 1 as a reference.
Please solve it by MATLAB. Please solve problem 4 only, I attached problem 1 as a reference.
Please solve it by MATLAB. Problem 4 Steffensen's method is a scheme for finding a numerical solution of an equation of the form f(x) 0 that is similar to Newton's method but does not require the derivative of f(x). The solution process starts by choosing a point xu, near the solution, as the first estimate of the solution. The next estimates of the solution x +1 are calculated by f (x)2 xi+1 Write a MATLAB user-defined function that solves a nonlinear equation with Steffensen's method. Name the function Xs- SteffensenRoot(Fun,Xest), where the output argument Xs is the numerical solution. The input argument Fun is a name for the function that calculates f(x) for a given x (it is a dummy name for the function that is imported into SteffensenRoot), and Xest is the initial estimate of the solution. The iterations should stop when the estimated relative error is smaller than 106. The number of iterations should be limited to 100 (to avoid an infinite loop). If a solution with the required accuracy is not obtained in 100 iterations, the program should stop and display an error message. Use the function SteffensenRoot to solve Problem 1
Explanation / Answer
The Matlab function SteffensenRoot.m
function Xs = SteffensenRoot(Fun,Xest)
tol = 10^-6; % tolerence
Max_itr = 100; % Maximum number of iterations allowed
y = Fun(Xest); % evaluating function at estimated x value
itr = 0;% initially iteration is zeor
while abs(y)>tol && itr<=Max_itr % checking xest is a root or not and Max iter reached or not
Xest = Xest-(y^2)/(Fun(Xest+y)-y);% new x value
y = Fun(Xest);% computing f at new x value
itr = itr +1;% increse the number of iterations
end
if itr>Max_itr % method faild if number of iterations reached Max_itr
fprintf('Method faild to converge ');% error to the user
return
else % if method converges
Xs = Xest;% solution
%fprintf('The root is %f, Obtained after %d iterations ',Xest,itr);
end
end
Testing of SteffensenRoot.m for problem 1
>> f = @(x) sin(x)./x -3/4; % function given in problem 1
>> Xest = 0.5; % initial guess
>> Xs = SteffensenRoot(f,Xest) % calling the function SteffensenRoot() to get the root
Xs =
1.2757
>> f(Xs)% verifying the result obtained
ans =
-1.9387e-11