Angelina D.

asked • 09/15/23

MATLAB Newton Raphson Storing Iterative Root Estimates

I am looking for help to be able to store all my iterative root estimates and number of iterations in my MATLAB code. It is based off a pseudocode, but I am having trouble doing this because not all the data is in the for loop. I have attached my code below.


function [x] = Newton_savesteps(f,fp,x)

%define arr1, arr2, arr3 here

% Preallocating arrays

% n = [];

% x = [];

% f_x = [];

nmax = 500;

delta = 0.5*10^-6;

epsilon = 0.5*10^-6;

f_x = f(x);

disp([0,x,f_x])

% store values arr1(1) = ...

for n = 1:nmax

f_p = fp(x);

if abs(f_p) < delta

disp('small derivative')

return

end

d = f_x/f_p;

x = x-d;

f_x = f(x);

disp([n,double(subs(x)),double(subs(f_x))])

% store values

%arr1(n+1) = ...

if abs(d) < epsilon

disp('convergence')

x = double(subs(x));

return

end

end

x = double(subs(x));

end


1 Expert Answer

By:

Luke J. answered • 08/06/24

Tutor
4.6 (199)

BS in Mechanical Eng. with 10+ Years of Coding & Tutoring Experience

Still looking for help? Get the right answer, fast.

Ask a question for free

Get a free answer to a quick problem.
Most questions answered within 4 hours.

OR

Find an Online Tutor Now

Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.