Newton's Method Plot of Errors

조회 수: 2 (최근 30일)
Kaeden Beaty
Kaeden Beaty 2021년 2월 17일
답변: Image Analyst 2021년 2월 17일
Hi! I am new to MATLAB and I am using this code
%to solve nonlinear equations using newton
%f(x)=(x+1)*(x-1/2)
%df(x)=2*x+1/2
%initial conditions
x0=-1.2;
maxIter=10;
tolX=0.001;
%computation using newton
x=x0;
xold=x0;
for i=1:maxIter
f=(x+1)*(x-1/2);
df=2*x+1/2;
x=x-f/df
err=abs(x-xold);
xold=x;
if (err<tolX)
break;
end
end
to try and plot logarithms of the errors against each other. My though process is to write
>> prevErr=err(1:9);
>> currErr=err(2:10);
>> plot (log(prevErr),log(currErr),'--r')
but after the first line I am getting the notice "Index exceeds the number of array elements (1)." if anyone know how I can fix this please let me know!

답변 (1개)

Image Analyst
Image Analyst 2021년 2월 17일
You need to index err:
err(i) = abs(x - xold);
Otherwise it's just a scalar, not a vector with 9 elements because you're overwriting it every time.

카테고리

Help CenterFile Exchange에서 Physics에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by