Fsolve Error while solving a system of non-linear equations.

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

답변 (1개)

Walter Roberson
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

Thanks brother ... sorry about this syntax error but there is another problem now ..... this code works after that correction you are suggesting ..... the new problem is that at times i get negative values in answer which i want to avoid .... Thanks if there is any hep plz
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?
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.

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

카테고리

도움말 센터File Exchange에서 Mathematics에 대해 자세히 알아보기

태그

질문:

2020년 3월 15일

댓글:

2020년 3월 22일

Community Treasure Hunt

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

Start Hunting!

Translated by