Implicit function nonlinear curve fitting error-debugging mode

4 ビュー (過去 30 日間)
Liang
Liang 2011 年 11 月 6 日
function diff=fit_pd(x,X,Y)
global X Y;
diff=(-6432)*(1-Y./1040)-1.987*Y.*log(X)-x(1)*(1-X).^2*(1-Y./x(2));
x0=[-1000,10000]';
x=lsqnonlin(@fit_pd,x0,[],[],xdata,ydata)
%xdata,ydata get from experimental value
Why debug mode? command return , it says that input argument is undefined. I use global X Y in the function definition. It does not work.
Thanks so much for answers.

回答 (3 件)

Liang
Liang 2011 年 11 月 6 日
A immediate answer is highly appreciated. Thank you. Best.
  1 件のコメント
Fangjun Jiang
Fangjun Jiang 2011 年 11 月 6 日
Add your update as a comment, not as an answer.

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


Fangjun Jiang
Fangjun Jiang 2011 年 11 月 6 日
I saw a few problems.
  1. Use of x (low case) and X (up case) as two variables is not a good idea.
  2. diff is a MATLAB function. Try not to use it as a variable name.
  3. Inside the definition of your function fit_pd(), you are using @fit_pd. Is this going to cause a chicken-and-egg problem?
  2 件のコメント
Liang
Liang 2011 年 11 月 6 日
Thanks for replying. But First two don't help. The third one. Of course i write function in m file. The bottom part in command window.
Fangjun Jiang
Fangjun Jiang 2011 年 11 月 6 日
All right, then Walter's link regarding passing extra parameters should be all you need. Using global variables works too. You just need to also declare global in MATLAB base workspace.

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


Walter Roberson
Walter Roberson 2011 年 11 月 6 日
1) Do not use the same name for a parameter and a global variable.
2) Your command
global X Y;
is the equivalent of
global('X', 'Y;')
which would attempt to declare a variable named "X" and another variable named "Y;" (Y-semicolon).
When you use command mode such as
global X Y
or
cd C:\System32\Documents And Settings\Liang\
then do not put semi-colons on the end of them, or else the semi-colon will be treated as input to the command.
3) If you are trying to execute your function fit_pd by pressing F5 or selecting Run from the menu, then it is going to be called with no arguments. You might have (perhaps) given meaning to X and Y through your global statement, but your x (lower-case) would still be undefined.
4) If we are to understand that the three lines from x0= onwards are part of a different file, and you are running that second file to invoke lsqnonlin on the function fit_pd, then if you examine the documentation for lsqnonlin, you will see that the function you designate will be called with exactly one argument. If you want to pass additional arguments then see the documentation that is referred to in the lsqnonlin reference page, Passing Extra Parameters
5) If we are to understand that the three lines from x0= onwards are part of a different file, and you are running that second file to invoke lsqnonlin on the function fit_pd, then notice that you did not define or give initial values to the global variables X and Y. In such a situation, MATLAB would initialize the variables to the empty array.
  6 件のコメント
Liang
Liang 2011 年 11 月 6 日
Does this mean that isqnonlin can not take @fun more than two arguments? Here i have three arguments x Z Y.
Walter Roberson
Walter Roberson 2011 年 11 月 6 日
It means that if you want more than one argument you need to follow the instructions in the link above "Passing Extra Parameters"

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by