# fsolve for equations system with syms

8 views (last 30 days)
dvir schwartz on 6 Aug 2022
Edited: Walter Roberson on 6 Aug 2022
Hello everyone,
Im trying to use fsolve on an equation system with syms parameters.
All the parameters must be real.
I don't know what is the right way to write the code.
thank's for every help and suggestions :)
THE CODE:
syms k1 k2 k3 gs s1 s2 s3 a real %%%% by adding the real statement I force the syms to be real and not comlex
global w_0 k3_0 T1 T2 % some constatnst
j1 = [4*gs*(a+s1*k1)-1/T2,-w_0+4*gs*s2*k1,4*gs*s3*k1];
j2 = [w_0+4*gs*s1*k2, 4*gs*(a+s2*k2)-1/T2,4*gs*s3*k2];
j3 = [4*gs*s1*k3, 4*gs*s2*k2, 4*gs*(a+s3*k3)-1/T1];
J = sym([j1; j2; j3]);
[Vec,Eig]=eig(J);
%%% The system equations: %%%%
ss(1) = -w_0*k2 - gs*(s1-4*a*k1)-k1/T2;
ss(2) = w_0*k1 - gs*(s2-4*a*k2)-k2/T2;
ss(3) = - gs*(s3-4*a*k3)-(k3-k3_0)/T1;
ss(4) = real(Eig(2,2));% only the real part of the
initEq=[conds; 1]; %%initial point
%%%%%%%%% until it works OK %%%%%%%%%%
sol=fsolve(ss,initEq);
The error it gives me is:
Error using lsqfcnchk (line 80)
If FUN is a MATLAB object, it must have an feval method.
Error in fsolve (line 238)

Walter Roberson on 6 Aug 2022
Edited: Walter Roberson on 6 Aug 2022
fsolve must be applied to a function handle (or a character vector that names a function.) There is no fsolve for symbolic expressions.
You could potentially vpasolve(); be careful about how you supply the initial value in this case. You would need to pass vpasolve() a vector of symbol names as the second parameter, and then you would have to pass a column vector of the initial conditions, with values given in the same order as the vector of symbol names. (This mechanism permits establishing upper and lower bounds for real-valued symbols, but if you do that then you cannot specify the starting value.)
You could use matlabFunction on ss to build a function handle for fsolve to use. If you take this approach make sure that you pass the 'vars' option and supply a cell array that contains a single vector of symbol names.
ssFun = matlabFunction(ss, 'vars', {[a gs k1 k2 k3 s1 s2 s3]})

R2021b

### Community Treasure Hunt

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

Start Hunting!