## Solving non-linear equations using symbolic variables, jacobian, fsolve.

on 22 Mar 2019
Latest activity Commented on by Walter Roberson

### Walter Roberson (view profile)

on 22 Mar 2019
Hi there,
I am trying to solve a set of non-linear equations using Jacobian, fsolve and symbolic variables.
I am unable to figure out how to pass a jacobian computed symbolically to fsolve.
The function file is:
function [eq,J]=solve_inp(q)
global A m n k fout t time;
fsub=subs(fout,t,time);
eq=matlabFunction(fsub);
if nargout>1
jac=jacobian(fsub);
J=matlabFunction(jac);
end
end
the main file is(part of):
j=1;
for time= 0:2*pi/(omega*step):2*pi/omega
time
if time==0
x0=[0 5 5 5];
else
x0=xy(:,j-1);
end
[soln,fval]=fsolve(solve_inp(q),x0,options);
xy(:,j)=soln;
j=j+1;
end
here 'fout' is a function of input variables and time.

Walter Roberson

### Walter Roberson (view profile)

on 22 Mar 2019
What is q here in the context of solve_inp(q) ?

on 22 Mar 2019
q is an array of symbolic variables to be computed.
q=sym('v',[1 2*(n+k)]);
Walter Roberson

### Walter Roberson (view profile)

on 22 Mar 2019
Perhaps that is what should be passed in the 'vars' parameter of matlabFunction
matlabFunction(fun, 'vars', {q})

### Tags ### Walter Roberson (view profile)

on 22 Mar 2019

Your x0 has more than one value. That tells us that your function is a function of multiple variables. fsolve() passes all of the trial versions of the variables together as a single vector. However, matlabFunction() by default will create different variables as separate arguments.
You need to use the 'vars' option of matlabFunction to bundle all of the variables together as a single argument. Use a cell array containing a vector of the variables. For example,
vars = symvar(jac);
J = matlabFunction(jac, 'vars', {vars});
But I would recommend instead using an explicit list of variables as otherwise the ordering of initial conditions might be wrong.