You are on page 1of 1

% Question 2

% Define inputs for problem


x0=70;
x1 = 72;
emax = 1E-6;
n = 50;

%Get function at first two initial guesses


f_xolder = f_a2q1(x0);
f_xold = f_a2q1(x1);

% Initize iteration count


i = 0;
xolder = x0;
xold = x1;

while abs(f_xold) > emax & i < n%Iteration loop

%Get xnew using Secant


xnew = xold - f_xold * (xolder - xold)/(f_xolder - f_xold);

%Update iteration
i = i + 1;
xolder = xold;
f_xolder = f_xold;
xold = xnew;
f_xold = f_a2q1(xold); %get function value at xold

end

if i<n
'Root is'
xold
'Iterations are'
i
else
'No convergence'
end

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

%Function f_a2q1(x)

function [F, f] = f_a2q1(x)

F = x.^2 - 5248;
f = 2*x;

end

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

You might also like