fsolve is not returning the right solution - matlab
Show older comments
I'm trying to find the vector x=[x1 ; x2 ; x3 ; x4 ; x5] by solving:
- Valorantigo(1,1)=3441/(1-0.00302+x1) + 3441/(1-0.00261+x1)^2 + 301720.5/(1-0.00208+x1)^3
- Valorantigo(2,1)=68750/(1-0.00302+x2) + 1068750/(1-0.00261+x2)^2 + 0/(1-0.00208+x2)^3
- Valorantigo(3,1)=170040/(1-0.00302+x3) + 13085020/(1-0.00261+x3)^2 + 0/(1-0.00208+x3)^3
- Valorantigo(4,1)=229350/(1-0.00302+x4) + 229350/(1-0.00261+x4)^2 + 5729350/(1-0.00208+x4)^3
- Valorantigo(5,1)=34194000/(1-0.00302+x5) + 0/(1-0.00261+x5)^2 + 0/(1-0.00208+x5)^3
My code is:
function main
x0=ones(5,1);
sol=fsolve(@myfun,x0)
function F=myfun(x)
A=[99.23;100.05;91;107.71;104.1];
B=[3441 3441 301720.5;68750 1068750 0;170040 13085020 0;229350 229350 5729350;34194000 0 0];
N=[300000;1000000;13000000;5500000;32800000];
E=[-0.00302;-0.00261;-0.00208];
[c3,r3]=size(A);
[c4,r4]=size(B);
Valorantigo(1:c3)=A(1:c3).*N(1:c3)./100;
for i=1:c3
Valor(i)=0.0;
for j=1:r4
Valor(i)=Valor(i)+B(i,j)/(1+E(j)+x(i)^j);
end
F(i)=Valor(i)-Valorantigo(i);
end
end
end
But the solution is not right, I'm getting sol=[0.3285 0.3576 0.3474 0.3289 0.004] and it should be [1.4299 7.1098 ...].
This was my first code (but didnt get any solution):
A=[99.23;100.05;91;107.71;104.1];
B=[3441 3441 301720.5;68750 1068750 0;170040 13085020 0;229350 229350 5729350;34194000 0 0];
N=[300000;1000000;13000000;5500000;32800000];
E=[-0.00302;-0.00261;-0.00208];
[c3,r3]=size(A);
[c4,r4]=size(B);
Valorantigo(1:c3,1)=A(1:c3,1).* N(1:c3,1) ./100;
x=sym ('x',[1 c3]);
x=transpose(x);
for i=1:c3
Valor(i,1)=symfun(0,x);
for j=1:r4
Valor(i,1)=symfun((Valor(i,1)/(1+E(j,1)+x(i,1))^j)+((B(i,j)/((1+E(j,1)+x(i,1))^j))),x);
end
end
eqn=Valor(1:c3,1)==Valorantigo(1:c3,1);
[x1,x2,x3,x4, x5, param, cond] = solve(eqn, x, 'ReturnConditions', true);
Thanks
Answers (1)
Your equation contains e.g.
0/(1-0.00208+x5)^3
but the code
B(i,j)/(1+E(j)+x(i)^j)
The parenthesis differ. Try
B(i,j)/(1+E(j)+x(i)) ^ j
I do not see where Valorantigo(1:c3)=A(1:c3).*N(1:c3)./100; appears in the equation.
2 Comments
Mariana Ferreira
on 19 Jul 2017
Jan
on 19 Jul 2017
@Mariana: "Didn't work" is not helpful. This was a typo, wasn't it? Therefore changing the parenthesis such that the code equals the equation should "work", at least better than before. Then please do not let the readers guess, what the next problem is.
You have posted the equations in your question above. They do not contain something like "eqn=Valor(1:c3,1)==Valorantigo(1:c3,1)". Only the code using the symbolic approach contains this line. But again: we do not see how the equation is defined. Your code contains bugs, such that the result differ from your expectations. But how could we solve your problem if you do not post the original equations?
You have opened several threads concerning this problem now. This makes it much more diffucult to post further answers, because the level of confusion is very high now. Therefore I leave the discussion. Good luck!
Categories
Find more on Common Operations in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!