Info
Questa domanda è chiusa. Riaprila per modificarla o per rispondere.
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 visualizzazione (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
% 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 Commenti
Risposte (1)
  Alan Stevens
      
      
 il 30 Ago 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 Commenti
Questa domanda è chiusa.
Vedere anche
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

