error using fsolve 'Index exceeds matrix dimensions.' and ' Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.'
Afficher commentaires plus anciens
hello all.
I am trying to solve a system of nonlinear equations in a symbolic form using matlabFunction and fsolve but I have an error. Here is the code
eqns=vpa([ eqn_17 ; eqn_18]); %eqn_17 has 5 equations,eqn_18 has 5 equations
t=symvar(eqns); %10 symbolic variables used in eqns
F1=matlabFunction(eqns,'vars',{t}); %return function handle of the 10 eqns
x0=ones(10,1);
[soln,fval,exitflag]=fsolve(F1,x0)
at the last line I have the following error
Index exceeds matrix dimensions.
Error in F:\MATLAB_setup\toolbox\symbolic\symbolic\symengine.p
Error in fsolve (line 241) fuser = feval(funfcn{3},x,varargin{:});
Caused by: Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.
I do not understand that. Any help would be appreciated.
Thanks
2 commentaires
Star Strider
le 16 Sep 2012
I suggest doing simplify on your eqns variable. It might make subsequent calculations easier.
Look carefully at your t and F1 variables to be sure they make sense. I suspect F1 is not what you believe it to be.
Note that matlabFunction considers subscripted variables in the equations given to it as input arguments to be functions. So instead of treating x as a vector and its elements as discrete variables, it assumes x(1) is a function x() with an argument of 1.
I suggest you experiment with vectorize on your eqns variable, then create your own anonymous function or functions out of them. The behavior of matlabFunction can be different from what you expect.
annona
le 17 Sep 2012
Réponse acceptée
Plus de réponses (1)
Walter Roberson
le 16 Sep 2012
0 votes
Your F1 will take 10 different variables, but you are trying to pass in a single variable that is a scalar of length 10.
7 commentaires
annona
le 16 Sep 2012
Walter Roberson
le 16 Sep 2012
Sorry I should have said a vector of length 10 rather than a scalar of length 10.
[soln,fval,exitflag]=fsolve(@(x) F1(x(1),x(2),x(3),x(4),x(5),x(6),x(7),x(8),x(9),x(10)), x0)
Walter Roberson
le 16 Sep 2012
Hmmm, okay instead of what I showed, try using your original code but setting x0 = ones(1,10) as it appears your symbolic function expects a row vector.
Walter Roberson
le 17 Sep 2012
Which of the two errors? Index out of range or two many input arguments?
To confirm, you are now testing with
x0=ones(1,10);
[soln,fval,exitflag]=fsolve(F1,x0)
annona
le 17 Sep 2012
Catégories
En savoir plus sur Operations on Strings dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!