implement the secant method with x0 = 0.5, x1 = 0.6, and |xk −xk−1|
< 10−10 as a convergence criteria. Find the root of f(x) = x + ln(x) for x> 0. I was kinda stuck on finding the ratio of the error.
function [x errors rate] = secant(x0,x1,f,atol)
% x0, x1 initial values
x0 = 0.5;
x1 = 0.6;
% f function
f = @(x) x+ log(x);
% atol |x_{k}-x_{k-1} <= atol
atol = 1e-10;
% x: root, so that f(x) = 0
% error: used to measure convergence |x_{k}-x_{k-1}
% rate: ratio of errors e_{k+1}/e_k
max_iter = 500;
count = 0;
errors = x1-x0; % fill in this row array
rate = ; % fill in this row array
while(count < max_iter) && errors > 1e-10 % add additional stopping criteria here
count = count+1;
fx0 = f(x0);
fx1 = f(x1);
x2 = x1+f(x1)*(x1-x0)/f(x1)-f(x0); % fill this in with the secant formula
errors(count) = x1 - x0; % fill this in with |x_k - x_{k-1}|
end
x = x2;
rate = errors(2)/errors(1); % fill in this array with errors(k+1)/errors(k);
end
230,453 students got unstuck by Course
Hero in the last week
Our Expert Tutors provide step by step solutions to help you excel in your courses