필터 지우기
필터 지우기

How to fixe this error with fsolve ?

조회 수: 1 (최근 30일)
Idossou Marius Adom
Idossou Marius Adom 2020년 10월 1일
댓글: Idossou Marius Adom 2020년 10월 2일
Hello everyone.
I am having trouble to solve a system of two nonlinear equations of two variables with the fsolve command. Here is how I proceed:
  • I define a function of 5 variables:
function y = eq2(Par,kl,kf,ml,okt,omt) % Par is structure that contains parameters (constants) of my function
... % lines of instructions
end
The function eq2 works well. For example:
eq2(Par,.5,1,0.1,0.3,0.5)
% gives
ans =
0.0011 -0.9532
  • Then I fixe the value of 3 variables and define the function of the two remaining variables:
fun = @(kl,ml) eq2(Par,kl,1,ml,0.3,0.5);
The function fun works as well. For example:
fun(0.5,0.1)
% gives
ans =
0.0011 -0.9532
% same answer as before, as expected
  • Then I want to solve the equation fun(kl,ml)=(0,0):
x0 = [0.1,0.1];
solve(fun,x0)
which does not work. I get the error message:
Error using sym.getEqnsVars>checkVariables (line 92)
Second argument must be a vector of symbolic variables.
Error in sym.getEqnsVars (line 56)
checkVariables(vars);
Error in solve>getEqns (line 429)
[eqns, vars] = sym.getEqnsVars(argv{:});
Error in solve (line 226)
[eqns,vars,options] = getEqns(varargin{:});
I don't understand the error message, and I don't know what is wrong in my code. If the problem were only that solution does not exist the message should be different.
Can someone help me please ? Thank you in advance.
  댓글 수: 4
VBBV
VBBV 2020년 10월 2일
Use fsolve as
% if true
% code
% end
options = optimoptions('fsolve','Display','iter');
[x fval] = fsolve(fun,x0,options)
Idossou Marius Adom
Idossou Marius Adom 2020년 10월 2일
Thank you very much J. Alex Lee and Vasishta Bhargava. It works now.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

Help CenterFile Exchange에서 Solver Outputs and Iterative Display에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by