hi guys, I'm trying to solve a 3 equation non-linear system with fsolve, but it always give me an error when i run it. I have F cell of 3 function handle, and that's my input with fsolve. The functions work properly (I tryed to give a random vector of solutions and they gave me a result).
sol0=[0.5 0.3 0.3]
f1=@(sol) exp((P./(R.*T)).*(B(1,1)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
f2=@(sol) exp((P./(R.*T)).*(B(2,2)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
f3=@(sol) exp((P./(R.*T)).*(B(3,3)+0.5.*(sol(2).^2+sol(3).^2+(1-sol(2)-sol(3)).^2+2.*sol(2).*sol(3)+2.*sol(2)*(1-sol(2)-sol(3))+2.*sol(3).*(1-sol(2)-sol(3)))));
g1=@(sol) exp((1./(R.*T)).*(a./(1+(a.*sol(1)./(b.*(1-sol(1))))).^2));
g2=@(sol) exp((1./(R.*T)).*(b./(1+(b.*(1-sol(1))./(a.*sol(1)))).^2));
fun1=@(sol) (g1(sol).*sol(1).*Ps1)-(f1(sol).*sol(2).*P);
fun2=@(sol) (g2(sol).*(1-sol(1)).*Ps2)-(f2(sol).*sol(3).*P);
fun3=@(sol) (K.*f3(sol).*f2(sol))-f1(sol);
sol=[0 0 0]
F={fun1 fun2 fun3}
sol=fsolve(F,sol0)

 Respuesta aceptada

Star Strider
Star Strider el 5 de Jun. de 2019

0 votos

After I created values for all the constants (it would have been nice to have had those), this ran for me without error:
F = @(sol) [fun1(sol), fun2(sol), fun3(sol)]
sol=fsolve(F,sol0)
Defining ‘F’ as a cell array is a problem, because then the elements are elements of a cell array, and fsolve objects to that. Defining ‘F’ as a matrix and as its own anonymous function seems to work.

4 comentarios

Mattia Galanti
Mattia Galanti el 6 de Jun. de 2019
thank you for the answer! I tryed using your definition of F but it's not working. I tryed on a my friend's Mac and it works. Do you have a Mac? If you want I let you the constats I used:
P=40;
R=8.314472;
T=180.2+273.15;
T0=298.15;
a=3280;
b=2400;
B(1,1)=-3.0242
B(2,2)=-4.6483
B(3,3)=--0.4959
Star Strider
Star Strider el 6 de Jun. de 2019
My pleasure!
What does ‘not working’ mean?
That my code worked on your friend’s computer, means that the problem is not with my code. It worked correctly for me as well (Windows 10, R2019a). The problem is obviously with your implementation of my code, and not my code itself.
That said, you still didn’t supply ‘Ps1’, ‘Ps2’, and ‘K’. When I create values for those variables, my code works correctly.
Mattia Galanti
Mattia Galanti el 6 de Jun. de 2019
I think is a Computer problem,,my friend managed to run it without modifing the code i wrote, I still don't. Do you know if there's some options on my pc that could be the proeblem?
Star Strider
Star Strider el 6 de Jun. de 2019
I know nothing at all about your PC!
However you have now verified that my code works and gives the correct result. Please click on Accept this Answer to accept it.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Mathematics en Centro de ayuda y File Exchange.

Productos

Etiquetas

Preguntada:

el 5 de Jun. de 2019

Comentada:

el 6 de Jun. de 2019

Community Treasure Hunt

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

Start Hunting!

Translated by