I can not solve non-linear equation using m.file only
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
Yahia Mounir
am 14 Sep. 2015
Kommentiert: Hamoon
am 15 Sep. 2015
Hello I am trying to run the following file attached however it give me error undefined variable X, I attached the m.file can you help with that ,
Please note if i call the function at command window with the following steps @myfun x0=[0 0] x=fsolve(@myfun,xo) it is working and giving me the answer however i need only to use the mfile to get the answer
0 Kommentare
Akzeptierte Antwort
Hamoon
am 14 Sep. 2015
Cut this part of the code from the mfile you atteched:
x0=[0 0];
options = optimoptions('fsolve','Display','iter'); % Option to display output
x = fsolve(@myfun,x0,options);
then copy it to another script, and run that script. Make sure that two scripts are in a same path.
2 Kommentare
Hamoon
am 15 Sep. 2015
first you should put your variables inside the function block when you are defining a function, so myfun should be like this:
function F = myfun(x)
K1=1.912284191525973e+08;
K2=1.003211416173691;
K3=0.003211416173691;
K4=43.627688688552304;
F = [(x(1)^2*K1*x(2))-K4;
K2*(1-K3*log((1+K2*x(1))^(1/x(1))))-x(2)];
end
You've got another problem in your code too, through F evaluation you have this term:
1/x(1)
and when algorithm tries to get the value of F for example for x=[0,0], the second term of F will be NaN (Not a Number) because 1/x(1) when x(1)=0 is undefined and you'll get an error. please accept the answer if you are happy with it and it's the answer of your question.
Weitere Antworten (1)
Yahia Mounir
am 15 Sep. 2015
3 Kommentare
Hamoon
am 15 Sep. 2015
It would be better if you asked this question as a new question, first because other people can help you on this and nobody open an answered question if they want to answer a question, they don't know there is an another question here (I didn't know too, I just saw this by accident) and also it can be someone else's question too, so they will find the answer easier if you ask another question as a new one.
For what you asked, there is several ways to do that.
1) You can pass the constants to your evaluation function like this:
function F = myfun(x,K1,K2,K3,K4)
F = [(x(1)^2*K1*x(2))-K4;
K2*(1-K3)-x(2)];
end
and then in the solver script you can use this code:
x0=[0 0];
K1=1.912284191525973e+08;
K2=1.003211416173691;
K3=0.003211416173691;
K4=43.627688688552304;
options = optimoptions('fsolve','Display','iter'); % Options
x = fsolve(@(x) myfun(x,K1,K2,K3,K4),x0,options);
-----------------------------------------------------------------------
2) you can define global variables:
so your function script can be:
function F = myfun(x)
global K1 K2 K3 K4
F = [(x(1)^2*K1*x(2))-K4;
K2*(1-K3)-x(2)];
end
and your solver script:
x0=[0 0];
global K1 K2 K3 K4
K1=1.912284191525973e+08;
K2=1.003211416173691;
K3=0.003211416173691;
K4=43.627688688552304;
options = optimoptions('fsolve','Display','iter'); % Options
fsolve(@myfun,x0,options);
I hope this helps you.
Siehe auch
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!