
Luke J. answered 08/06/24
BS in Mechanical Eng. with 10+ Years of Coding & Tutoring Experience
Angelina D.
asked 09/15/23I 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
Luke J. answered 08/06/24
BS in Mechanical Eng. with 10+ Years of Coding & Tutoring Experience
Get a free answer to a quick problem.
Most questions answered within 4 hours.
Choose an expert and meet online. No packages or subscriptions, pay only for the time you need.