Nothing will be showing on plot

3 次查看(过去 30 天)
alam md
alam md 2021-2-23
回答: Rik 2021-2-23
% Solve the problem using netwon raphson method
% f(x)=x^10-1
clc;clear
% Start the computation
xo=0.65;
x=xo;
xold=x;
for iteration=1:22
x=xold;
fx =x^10-1;
der_x=10*x^9;
y=fx./der_x;
xnew=x-y;
xold=xnew;
err=abs(xnew-x);
disp([' Iteration number : ' , num2str(iteration) , ' ; The new value of the x is : ' , num2str(xnew), ' ; The error is : ' , num2str(err) ])
plot(iteration,err)
end

回答(2 个)

KALYAN ACHARJYA
KALYAN ACHARJYA 2021-2-23
xo=0.65;
x=xo;
xold=x;
iteration=1:22;
err=zeros(1,length(iteration));
for i=1:length(iteration)
x=xold;
fx =x^10-1;
der_x=10*x^9;
y=fx./der_x;
xnew=x-y;
xold=xnew;
err(i)=abs(xnew-x);
disp([' Iteration number : ' , num2str(iteration) , ' ; The new value of the x is : ' , num2str(xnew), ' ; The error is : ' , num2str(err) ])
end
plot(iteration,err);

Rik
Rik 2021-2-23
You are plotting single points, without changing the default (where points are not displayed, only the line connecting them). You are also resetting the plot, as you don't use hold.
You should store the results of your calculation in a vector so you can use that in plot.

Community Treasure Hunt

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

Start Hunting!

Translated by