I can not solve non-linear equation using m.file only
Show older comments
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
Accepted Answer
More Answers (1)
Yahia Mounir
on 15 Sep 2015
0 votes
3 Comments
Yahia Mounir
on 15 Sep 2015
Walter Roberson
on 15 Sep 2015
Which workspace are they in?
Hamoon
on 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.
Categories
Find more on Whos in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!