Undefined function or variable 'x'.

2 ビュー (過去 30 日間)
sabarish
sabarish 2014 年 7 月 25 日
コメント済み: Yahia Mounir 2015 年 9 月 15 日
similar to the question am also having the same doubt
function F = myfun(x)
x=[x(0),x(1),x(2),x(3),x(4),x(5)];
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
h=str2func( ['@myfun' F] );
fsolve(h,x1);
this is an example problem from matlab.am just trying to solve this.but am getting error as i mentioned in question!!!!!
how can eliminate this error
my equation is consists of 5 variable and lengthy equation for example my first equation is * *
((((-x(1)*x(2)*x(4)) + (x(1)*x(3)*x(5))*0.15335) + (0.0680154*(-x(1)*x(2)*x(4)) + (x(3)*x(1)*x(5))) + (x(1) (x(1)*x(2)) + (x(2)*x(3)))*(-0.15335))*(1 + (x(1)+(x(1)*x(2)) + (x(2)*x(3)))*0.15335) - ((0.1067*(x(1)*x(2)*x(4)) + (x(1)*x(3)*x(5))) + (x(1) + (x(1)*x(2)) + (x(2)*x(3))*0.0340077)))]
so i need to solve my equations
help to solve the problem

回答 (4 件)

Steven Lord
Steven Lord 2015 年 9 月 14 日
Do NOT try to call FSOLVE with a function handle to myfun from within myfun itself. Doing so is likely to lead to a recursion error. Either create two separate function files, one named myfun.m that evaluate the system of equations for which you want to find a solution and another that contains your FSOLVE call. The solveit.m file should contain only:
function y = solveit(x0)
% Contains the FSOLVE call ONLY
y = fsolve(@myfun,x0);
and the myfun.m file should contain only:
function F = myfun(x)
% Contains the system to be solved
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
Alternately, if the only place you use myfun is in this solveit call, you could create it as a subfunction in solveit.m to avoid creating two separate files. In this scenario, solveit.m contains:
function y = solveit(x0)
y = fsolve(@myfun,x0);
function F = myfun(x)
F = [2*x(1) - x(2) - exp(-x(1));
-x(1) + 2*x(2) - exp(-x(2))];
In either of these cases, you'd call solveit with your initial guess at a solution vector as the only input.
  1 件のコメント
Yahia Mounir
Yahia Mounir 2015 年 9 月 15 日
Thanks Steven for your explanation can you have a look for attached file and equation and try fix the m.file I will be grateful for your help

サインインしてコメントする。


Julia
Julia 2014 年 7 月 25 日
Is it "Undefined function or variable 'x'" or "Undefined function or variable 'x1'"?
fsolve(h, x1);
I cannot find x1 in your code.
  1 件のコメント
sabarish
sabarish 2014 年 7 月 26 日
編集済み: Walter Roberson 2015 年 9 月 14 日
i need to solve the another set of equation like i mentioned above passage
its showing error
could u please hep to rectify
its showing error like this
tail11 at 2
F=[((x(1)*(-x(2)*x(4))+(x(3)*x(5))*0.15335)+(x(1)*(1+x(2)+x(3))))/((1+(x(1)*(1+x(2)+x(3))*0.1535)^2));
Error in ==> fsolve at 248
fuser = feval(funfcn{3},x,varargin{:});
Caused by:
Failure in initial user-supplied objective function evaluation. FSOLVE cannot continue.

サインインしてコメントする。


Image Analyst
Image Analyst 2014 年 7 月 25 日
You did not define x1. Also you can't start with and index of zero like you did. To crop/extract only the first 6 elements of x, you'd do this
x = x(1:6);
  2 件のコメント
sabarish
sabarish 2014 年 7 月 26 日
its showing again error like this
Input argument "x" is undefined.
Error in ==> tail11 at 2 x = x(1:6);
Image Analyst
Image Analyst 2014 年 7 月 26 日
Attach your m-file so we can fix it.

サインインしてコメントする。


Yahia Mounir
Yahia Mounir 2015 年 9 月 14 日
Hello I am trying to run the following file however it give me error undefined variable X, I attached the my file can you help with that ,
Please note if i call the function at command window with the following steps @myfun t x0=[0 0] x=(@myfun,xo) it is working and giving me the answer i need only to use the mfile to get the answer
  1 件のコメント
Walter Roberson
Walter Roberson 2015 年 9 月 14 日
The file did not get attached.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeAgriculture についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by