MATLAB: How to solve the non linear algebraic equation system with a parameter

fsolve with symbolic variable

I tried as shown below :
function F=root2d(x)
syms s
F(1)=46.263e+5*x(1)+0.288875*x(3)^2*x(2)*sin(x(4))-0.187375*x(2)^2*x(3)*sin(x(6))-1.5e+3*sin(x(5)); F(2)=237.807241*s*x(1)-0.7765*x(1)^3-2.07225*x(1)*x(2)^2-0.288875*x(1)^2*x(2)*cos(x(4))+0.187375*x(2)^2*x(3)*cos(x(6))-4.341625*x(1)*x(3)^2+1.5e3*cos(x(5)); F(3)=249.85e5*x(2)-0.09625*x(1)^3*sin(x(4))+0.374875*x(1)*x(2)*x(3)*sin(x(6)); F(4)=2311.76211*x(2)*(z-801)-10.7815*x(2)^3-2.07225*x(1)^2*x(2)-0.09625*x(1)^3*cos(x(4))+0.374875*x(1)*x(2)*x(3)*cos(x(6))-16.2345*x(3)^2*x(2); F(5)=938.16e5*x(3)+0.187375*x(1)*x(2)^2*sin(x(6)); F(6)=(11252.3953*s+3866749.2347199973)*x(3)+0.187375*x(1)*x(2)^2*cos(x(6))-4.341625*x(1)^2*x(3)-16.2345*x(2)^2*x(3)-51.815125*x(3)^3;
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt); fun = @root2d; x0=[1,1,1,1,1,1]; x=fsolve(fun,x0,options)
This is the Error I am geting :
The following error occurred converting from sym to double: DOUBLE cannot convert the input expression into a double array. If the input expression contains a symbolic variable, use VPA.
Error in root2d (line 6) F(2)=237.807241*s*x(1)-0.7765*x(1)^3-2.07225*x(1)*x(2)^2-0.288875*x(1)^2*x(2)*cos(x(4))+0.187375*x(2)^2*x(3)*cos(x(6))-4.341625*x(1)*x(3)^2+1.5e3*cos(x(5));
Error in fsolve (line 218) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

Best Answer

You can use the matlabFunction function to create an anonymous function from ‘F’ so you can use it with fsolve. However, you still need to provide values for ‘s’ and ‘z’ in order to solve it for ‘x’ (that appears in the ‘root2d’ anonymous function as ‘in1’).
The Code —
syms s x z
x = sym('x', [1,6]);
F(1)=46.263e+5*x(1)+0.288875*x(3)^2*x(2)*sin(x(4))-0.187375*x(2)^2*x(3)*sin(x(6))-1.5e+3*sin(x(5));
F(2)=237.807241*s*x(1)-0.7765*x(1)^3-2.07225*x(1)*x(2)^2-0.288875*x(1)^2*x(2)*cos(x(4))+0.187375*x(2)^2*x(3)*cos(x(6))-4.341625*x(1)*x(3)^2+1.5e3*cos(x(5));
F(3)=249.85e5*x(2)-0.09625*x(1)^3*sin(x(4))+0.374875*x(1)*x(2)*x(3)*sin(x(6));
F(4)=2311.76211*x(2)*(z-801)-10.7815*x(2)^3-2.07225*x(1)^2*x(2)-0.09625*x(1)^3*cos(x(4))+0.374875*x(1)*x(2)*x(3)*cos(x(6))-16.2345*x(3)^2*x(2);
F(5)=938.16e5*x(3)+0.187375*x(1)*x(2)^2*sin(x(6));
F(6)=(11252.3953*s+3866749.2347199973)*x(3)+0.187375*x(1)*x(2)^2*cos(x(6))-4.341625*x(1)^2*x(3)-16.2345*x(2)^2*x(3)-51.815125*x(3)^3;
root2d = matlabFunction(F, 'Vars',{[x],s,z})
s = rand; % Provide Correct Value (Scalar)

z = rand; % Provide Correct Value (Scalar)
options = optimoptions('fsolve','Display','none','PlotFcn',@optimplotfirstorderopt);
fun = @(in1)root2d(in1,s,z);
x0=[1,1,1,1,1,1];
x=fsolve(fun,x0,options)