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

3 views (last 30 days)
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
options=optimoptions(@fsolve,'Algorithm','trust-region','SpecifyObjectiveGradient',true);
[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.
  3 Comments
Walter Roberson
Walter Roberson on 22 Mar 2019
Perhaps that is what should be passed in the 'vars' parameter of matlabFunction
matlabFunction(fun, 'vars', {q})

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson 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.

Community Treasure Hunt

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

Start Hunting!