MATLAB: How to update an element of vector every cycle

elementerrorfor loopMATLABvector

Hi, i'm trying to create a vector d=zeros(itermax,1), and then update every element of the vector with the value of the variable d_n. These are the script and the function file:
%script
g = @(x) (3*x+2)/(2*x+3);
tol = 10^(-10);
xn=0.5;
y=0.5;
z=0.5;
x_p=0;
x_n=0;
iter_max=100;
d=zeros(iter_max,1);
[xn,d_n] = pfisso(g,tol,iter_max,xn,y,z,d)
function:
function [xn,d_n] = pfisso(g,tol,iter_max,xn,y,z,d)
for i= 0:iter_max
z=y; %x_(n-1)
y = xn; %x_n
xn=g(xn); %x_(n+1)
d_n= abs(y - z);
d(i)=d_n;
if (i>=2) && (d_n<=tol)
break
end
end
end
The program returns:
Array indices must be positive integers or logical values.
Error in pfisso (line 7)
d(i)=d_n;
Error in lab4es5 (line 10)
[xn,d_n] = pfisso(g,tol,iter_max,xn,y,z,d)

Best Answer

MATLAB is 1-based indexing, you light change the for-loop to start at 1
for i= 1:iter_max
or shift the storage index by 1
d(i+1)=d_n;