error using fsolve 'Index exceeds matrix dimensions.' and ' Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.'
3 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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 comentarios
Star Strider
el 16 de Sept. de 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.
Respuesta aceptada
Star Strider
el 17 de Sept. de 2012
Editada: Star Strider
el 17 de Sept. de 2012
The problem I see is that in F1 you define your in1 vector as [1 x 10], then you give it your initial x0 as a [10 x 1] vector, so it threw the error you posted.
You can eliminate the column-vector requirement by changing in1(:,1) ... in1(:,10) to in1(1) ... in1(10), unless you have a specific reason for requiring in1 to be a column vector. With the single subscripts, F1 will be happy with either row or column vectors for in1.
When I run this code:
F1 = @(in1)[in1(:,1).*-5.592228285836476e11+in1(:,6).*3.34246646076234e10+in1(:,1).*in1(:,6).*4.665753353923766e11+5.338430977835152e10;in1(:,2).*-5.595115494912028e11+in1(:,7).*3.34246646076234e10+in1(:,2).*in1(:,7).*4.665753353923766e11+5.338429087540035e10;in1(:,3).*-5.598175072243341e11+in1(:,8).*3.34246646076234e10+in1(:,3).*in1(:,8).*4.665753353923766e11+5.338427320731596e10;in1(:,4).*-5.601417301942708e11+in1(:,9).*3.34246646076234e10+in1(:,4).*in1(:,9).*4.665753353923766e11+5.338425666965504e10;in1(:,5).*-5.604853082262216e11+in1(:,10).*3.34246646076234e10+in1(:,5).*in1(:,10).*4.665753353923766e11+5.338424116873857e10;in1(:,1).*1.0e12-in1(:,6).*6.791397396184978e10-in1(:,1).*in1(:,6).*9.331506707847532e11+3.232237330148703e7;in1(:,2).*1.0e12-in1(:,7).*6.793060676517363e10-in1(:,2).*in1(:,7).*9.331506707847532e11+4.063877496341306e7;in1(:,3).*1.0e12-in1(:,8).*6.794632422932858e10-in1(:,3).*in1(:,8).*9.331506707847532e11+4.849750704088881e7;in1(:,4).*1.0e12-in1(:,9).*6.795466436527876e10-in1(:,4).*in1(:,9).*9.331506707847532e11+5.266757501597958e7;in1(:,5).*1.0e12-in1(:,10).*6.795074806819398e10-in1(:,5).*in1(:,10).*9.331506707847532e11+5.070942647358813e7];
x0 = ones(1,10); % <— NOTE: this is [1 x 10], *NOT* [10 x 1]
[soln,fval,exitflag]=fsolve(F1,x0)
I get:
soln =
Columns 1 through 5
892.7806e-003 888.3838e-003 883.7788e-003 878.9839e-003 874.0104e-003
Columns 6 through 10
990.8993e-003 990.5208e-003 990.1217e-003 989.7065e-003 989.2783e-003
fval =
15.2588e-006
-38.1470e-006
-15.2588e-006
7.6294e-006
-15.2588e-006
-26.6433e-006
156.2238e-006
-126.8163e-006
110.4400e-006
77.3892e-006
exitflag =
2.0000e+000
A second minimum appears at:
soln =
Columns 1 through 5
-73.5049e-003 -73.5340e-003 -73.5614e-003 -73.5757e-003 -73.5685e-003
Columns 6 through 10
108.4937e+000 106.8749e+000 105.3948e+000 104.6480e+000 105.0634e+000
fval =
-534.0576e-006
-38.1470e-006
45.7764e-006
-236.5112e-006
-198.3643e-006
705.7786e-006
-1.4307e-003
-1.1034e-003
-621.9819e-006
-44.6811e-006
exitflag =
2.0000e+000
Is this what you want or what you were expecting?
2 comentarios
Star Strider
el 18 de Sept. de 2012
Thank you! It is always my pleasure to help!
To get the other minima, start at a different initial guess. (I usually search for several, because there are usually more than one. That explains the 100000 multiplier, since I added zeros to see if there were more minima.) I simply used:
x0 = -ones(1,10)*100000;
to get the second minimum, adding the ‘-’ to my first guess to get the second.
According to the documentation for matlabFunction, the order should be the same as {t}.
Again, my pleasure.
Más respuestas (1)
Walter Roberson
el 16 de Sept. de 2012
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 comentarios
Walter Roberson
el 17 de Sept. de 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)
Ver también
Categorías
Más información sobre Get Started with Symbolic Math Toolbox en Help Center y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!