I can not solve non-linear equation using m.file only

1 次查看(过去 30 天)
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

采纳的回答

Hamoon
Hamoon 2015-9-14
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 个评论
Yahia Mounir
Yahia Mounir 2015-9-15
Thanks it is working perfectly now . However can you have a look for the attachment equation I am Trying to solve it but always giving me error ? I attached the m.file + the equation Thanks very much in advance .
Hamoon
Hamoon 2015-9-15
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.

请先登录,再进行评论。

更多回答(1 个)

Yahia Mounir
Yahia Mounir 2015-9-15
Thanks it is very helpful
  3 个评论
Hamoon
Hamoon 2015-9-15
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.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 MATLAB 的更多信息

标签

产品

Community Treasure Hunt

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

Start Hunting!

Translated by