Info
La pregunta está cerrada. Vuélvala a abrir para editarla o responderla.
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
2 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
% 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)
0 comentarios
Respuestas (1)
Alan Stevens
el 30 de Ag. de 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
0 comentarios
La pregunta está cerrada.
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!