error using fsolve 'Index exceeds matrix dimensions.' and ' Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.'
이전 댓글 표시
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
Star Strider
2012년 9월 16일
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
2012년 9월 17일
채택된 답변
추가 답변 (1개)
Walter Roberson
2012년 9월 16일
0 개 추천
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
annona
2012년 9월 16일
Walter Roberson
2012년 9월 16일
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
2012년 9월 16일
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
2012년 9월 17일
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
2012년 9월 17일
카테고리
도움말 센터 및 File Exchange에서 Creating and Concatenating Matrices에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!