Info

This question is closed. Reopen it to edit or answer.

hi I have a code to plot the results of a for- loop I would though like this to start at my first value of x

1 view (last 30 days)
% EMTH171
% script using newtons method
clear
clc
close all
% function
f = @(x) 3*x.^4 + 7*x.^3 - 4*x.^2 - 10*x + 1/5;
% derivative
d = @(x) 12*x.^3 + 21*x.^2 - 8*x - 10;
% test value
x = -3;
z = x;
N = 20;
tol = 1e-4;
for ii = 1 : N
x = x - f(x)/d(x);
position = ii + 1;
appArray(position, 1) = x;
x = x;
residual = abs(f(x));
residualArray(position, 1) = residual;
lastvalue = appArray(ii, 1);
diff = abs(x - lastvalue);
absoluteArray(ii, 1) = diff;
if (diff < tol) && (residual < tol)
break
end
anArray(ii,1) = x;
end
fprintf('%.4f\n' ,anArray);
plot(anArray)

Answers (1)

Alan Stevens
Alan Stevens on 30 Aug 2020
Change your loop slightly:
for ii = 1 : N
anArray(ii) = x; %%%%%%%%%%%%%%
x = x - f(x)/d(x);
position = ii + 1;
appArray(position, 1) = x;
x = x;
residual = abs(f(x));
residualArray(position, 1) = residual;
lastvalue = appArray(ii, 1);
diff = abs(x - lastvalue);
absoluteArray(ii, 1) = diff;
if (diff < tol) && (residual < tol)
break
end
% anArray(position,1) = x;
end

Tags

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!