Fsolve Error while solving a system of non-linear equations.
조회 수: 1 (최근 30일)
이전 댓글 표시
I am trying to solve this system of non linear equations using fsolve:
function F = root2d(q)
M = 2;
z_alice = 1;
gammabar = 1;
F(1) = gamma(M) - gammainc(M*q(1)/gammabar, M ...
,'upper') - gammainc(M*(q(3)+z_alice)/gammabar, M,'upper');
F(2) = gamma(M) - gammainc(M*q(1)/gammabar, M,'upper') - (M/gammabar)^M...
(-q(2)^M*vpa(expint(1-M,sym(M)*q(2)/gammabar)) + (q(1) + z_alice)^M * ...
vpa(expint(1-M,sym(M)*q(1)+z_alice)/gammabar));
F(3) = q(2)^M * vpa(expint(1-M, sym(M)*q(2)/gammabar))...
-((q(1)+z_zlice)^M)*vpa(expint(1-M, sym(M)*(q(1)+z_alice)/gammabar))...
-q(3)^M * vpa(expint(1-M, sym(M)*q(3)/gammabar))+...
((q(2)+z_zlice)^M)*vpa(expint(1-M, sym(M)*(q(2)+z_alice)/gammabar));
when I run this:
close all;
clear all;
clc;
fun = @root2d;
q0 = [0,0 0];
q = fsolve(fun,q0)
I get the following error:
Failure in initial objective function evaluation. FSOLVE cannot continue.
Any help will be apprecated.
Thanks
댓글 수: 0
답변 (1개)
Walter Roberson
2020년 3월 15일
F(2) = gamma(M) - gammainc(M*q(1)/gammabar, M,'upper') - (M/gammabar)^M...
(-q(2)^M*vpa(expint(1-M,sym(M)*q(2)/gammabar)) + (q(1) + z_alice)^M * ...
vpa(expint(1-M,sym(M)*q(1)+z_alice)/gammabar));
Notice that at the end of the first line, you have ^M... with no operation after the ^M . Notice that the next line starts with ( with no operation there.
When you use ... then the current line ends immediately and the next line is effectively put into position, with no implied whitespace or operation. Your code is thus equivalent to
F(2) = gamma(M) - gammainc(M*q(1)/gammabar, M,'upper') - (M/gammabar)^M (-q(2)^M*vpa(expint(1-M,sym(M)*q(2)/gammabar)) + (q(1) + z_alice)^M * vpa(expint(1-M,sym(M)*q(1)+z_alice)/gammabar));
At the end of the second line you have the * before the ... so that is a plain multiplication between the two sides, but at the end of the first line you have the M and then ( on the next line, so what you have is M(-q(2)etc) which is an indexing request into M.
Be sure to put the appropriate mathematical operation before the ... on that first line.
댓글 수: 3
Walter Roberson
2020년 3월 22일
Your code defines z_alice and uses that variable, but it also uses z_zlice twice. Is that a different variable, or is that a typing mistake?
Walter Roberson
2020년 3월 22일
Your initial values, q0, are [0 0 0] . However, the second and third inputs must be non-zero and not the negative of z_alice (so, not -1) or else the expint() returns inf and you end up getting NaN created.
참고 항목
카테고리
Help Center 및 File Exchange에서 Systems of Nonlinear Equations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!