Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

% Question 1C

% Part C

% define variables
x0 = 1.2;
i = 0;
n = 100;
emax = 1E-6;

%Get function and derivative values associated with initial guess


[fx_xold, fxprime_xold] = f_a2q1c(x0);

xold = x0;

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

%Get xnew using Newton


xnew = xold - fx_xold/fxprime_xold;

%Update iteration
i = i + 1;
xold = xnew;
[fx_xold, fxprime_xold] = f_a2q1c(xold); %get function value at xold

end

'Part I Newton Solution'

if i<n
'Root is'
xold
'Number of iterations'
i
else
'No convergence'
end

%--------------------------------------------------------
% modified newton when multiplicity is known

ex0 = 1.2;
j = 0;
z = 100;
m=2;
emax = 1E-6;

%Get function and derivative values associated with initial guess


[fx_xold, fxprime_xold] = f_a2q1c(ex0);

xold = ex0;

while abs(fx_xold) > emax & j < z

%Get xnew using Newton


xnew = xold - m*(fx_xold/fxprime_xold);
%Update iteration
j = j + 1;
xold = xnew;
[fx_xold, fxprime_xold] = f_a2q1c(xold); %get function value at xold

end

'Part II Modified Newton Solution'

if j<z
'Root is'
xold
'Number of iterations'
j
else
'No convergence'
end

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

function [F, f] = f_a2q1c(x)

F = x.^3 - 2*x.^2 - 4*x + 8;


f = 3*x.^2 - 4*x - 4;

end

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

You might also like