Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

function [R] = myNewton1(f, x0, tol) which takes as inputf: a function handle x0

ID: 3817849 • Letter: F

Question

function [R] = myNewton1(f, x0, tol)

which takes as inputf: a function handle x0: the initial guess of the root tol: a tolerance above which the algorithm will keep iterating. Tips: • The code should calculate the value of the derivative of the function numerically. You may use your “myPartialDeriv” or implement a finite difference approximation. • Be sure to include an iteration counter which will stop the while-loop if the number of iterations get greater than 100. • It is not necessary to print out a convergence table within the while loop. (I.e., there should be no fprintf statements in your code)

Test Case: >> format longg

>> f = @(x) 2*(1-cos(x))+4*(1-sqrt(1-(0.5*sin(x)).^2)) - 1.2;

>> [root] = myNewton1(f, 1, 1e-8)

root = 0.958192178746275

Explanation / Answer

% I have completed almost 95% of the answer. I don't have matlab and I am unable to run diff(F,x) method to get the symbolic derivative of the given function. Please use diff(..) method to properly where I highlighted the code and assign to df variable there.

pkg load symbolic

function [R] = myNewton1(f,xo, error_tolerance)
    x_old = 10000; % some dummy value
  
    error_tolerance = 0.0001;
    absolute_error = abs(x_old-xo);

    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
    % Here you need to use diff method appropriately to get differntiation of a f(x)
    % I don't have matlab, I am unable to use diff method online
    syms x
    df = diff(f(x), x);
    %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

  
    i = 0;
    while (absolute_error > error_tolerance) && (xo ~= 0) && (i <= 100)
        x_old = xo;
        xo = xo - feval(f,xo)/ feval(df,xo);
        absolute_error = abs(x_old-xo);
        i = i+1;
    end
    R = xo;
end

f = @(x) 2*(1-cos(x))+4*(1-sqrt(1-(0.5*sin(x)).^2)) - 1.2;
result = myNewton1(f, 1, 1e-8);
disp(result);