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

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)

 Accepted 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)]

2 Comments

i=1;
p0=[0.5 0 0 50];
x1=0.5;
x2=0;
x3=0;
x4=50;
N=10;
error=0.00001;
syms x1 x2 x4
%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(x1,x2,x4)= [z1*x1*(1-x1/z2);z3*x1*x4/(z4+x4)-1.0617*x2; z5*x2;-z3*x1*x4/(z4+x4)];
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)
i have edit it, then ''Error using symfun/subsref (line 177)
Symbolic function expected 3 input arguments but received 1.'' pop at line p=p0-(f(p0)/df(p0));
sorry i am somewhat new to this.
Your function f now has 3 inputs - x1,x2,x4. You are calling it with just 1, p0.
p=p0-(f(p0(1),p0(2),p0(3))/df(p0(1),p0(2),p0(3)));

Sign in to comment.

More Answers (0)

Tags

Community Treasure Hunt

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

Start Hunting!