I can not solve non-linear equation using m.file only
이전 댓글 표시
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
채택된 답변
추가 답변 (1개)
Yahia Mounir
2015년 9월 15일
0 개 추천
댓글 수: 3
Yahia Mounir
2015년 9월 15일
Walter Roberson
2015년 9월 15일
Which workspace are they in?
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.
카테고리
도움말 센터 및 File Exchange에서 Whos에 대해 자세히 알아보기
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!