MATLAB: Its been displaying ” Index exceeds the number of array elements (1). Error in sym/subsref (line 890) R_tilde = builtin(‘s​ubsref’,L_​tilde,Idx)​;” can someone help me with this

nonlinear

i=1;
p0=[0.5 0 0 50];
N=10;
error=0.00001;
syms 'x'
%To simplify the equation,we need to make the coefficients
z1=0.949;
z2=3.439;
z3=18.72;
z4=37.51;
z5=1.169;
%equation for the 4 components
f(x)= [z1*x(1)*(1-x(1)/z2);z3*x(1)*x(4)/(z4+x(4))-1.0617*x(2); z5*x(2);-z3*x(1)*x(4)/(z4+x(4))];
df= diff(f)
while i<= N
p=p0-(f(p0)/df(p0));
if (abs(p-p0))/(abs(p))< error
fprintf('solution is %\n',double(p))
return
end
i=i+1;
p0=p;
end
fprintf('solution did not converge within %d iterationat require precision',N,error)

Best Answer

You didn't point it out, but the error is coming from the line where you define f(x).
The issue is with any time you index x with a value other than 1. Your variable x is a symbolic variable, not an array. Therefore, indexing does not work.
syms x
x(1)
ans = 
x
x(2)
Index exceeds the number of array elements (1).

Error in sym/subsref (line 902)
R_tilde = builtin('subsref',L_tilde,Idx);
If you need different values, create different variables.
syms x1 x2 x4
f(x1,x2,x4)= [z1*x1*(1-x1/z2);z3*x1*x4/(z4+x4)-1.0617*x2; z5*x2;-z3*x1*x4/(z4+x4)]