Error using==> nargin

8 次查看(过去 30 天)
Raphel
Raphel 2011-10-31
Hi,
I am a new user to mathlab, and trying to solve 11 simultaneous 1st order differential equation:
Here is the M-file :
funtion dxdt=anu(t,x)
%x(1)=xA, x(2)=xB, x(3)=xC, x(4)=xD, x(5)=xE, x(6)=xF, x(7)=xG, x(8)=xH,
%x(9)=xI, x(10)=xJ, x(11)=xK
global kg ka kb kc kd Z
dxdt=[-kg/Z*(x(2)+x(4)+x(7)+x(10)+x(11)); 1/Z*(kg*x(1)-ka*x(2)); 1/Z*ka*x(2); 1/Z*(kg*x(1)-kb*x(4)); 1/Z*(kb*x(4)-2*kd*x(5)); 1/Z*kd*(x(5)+x(8));1/Z*(kg*x(1)-kc*x(7)); 1/Z*(kc*x(7)-2*kd*x(8)); 1/Z*kd*(x(5)+x(8)); 1/Z*kg*x(1); 1/Z*kg*x(1)];
This is the call function:
>> clf
>> global kg ka kb kc kd Z
>> kg=0.03118;
>> ka=0.0042;
>> kb=0.000083;
>> kc=0;
>> kd=0.00025;
>> Z=4.056*10^-4;
>> tspan=[0:10:240];
>> x0=[1 0 0 0 0 0 0 0 0 0 0];
>> [t,x]=ode45('anu',tspan,x0);
Here is the error i got:
??? Error using ==> nargin
anu is a script.
Error in ==> funfun\private\odearguments at 80
if (nargin(ode) == 2)
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, ...
Somebody please help, i am new to mathlab so please simply.. Thanks

回答(6 个)

Walter Roberson
Walter Roberson 2011-10-31
Your function anu starts with the word
funtion
which is missing the 'c'.
  1 个评论
Jan
Jan 2011-10-31
After eating a soup with noodles shaped like characters ("Buchstabennudeln"), a friend of mine told me: "Jan, da liegt ein C auf dem Tisch" (there lies a C on the table). It was distusting, because I understood: "da liegt ein Zeh auf dem Tisch" (the lies a toe on the table).
I wish you and all readers a scary halloween.

请先登录,再进行评论。


Walter Roberson
Walter Roberson 2011-10-31
Earlier in your path, you must have another anu.m file that is a script (no "function" line)
Try
which -all anu
and compare that to the anu that you are expecting to be invoked.
Note: if the anu function that you show above is stored in a file other than anu.m then MATLAB will not be able to find the right file when you pass 'anu' as a quoted string to ode45: passing as a quoted string always forces MATLAB to feval() the name in the base workspace. Using function handles such as @anu is more likely to work in such a situation.

Raphel
Raphel 2011-10-31
Thanks for the quick response, anu was new m-file, i didnt have it earlier. Its stored in anu.m. can you explain how to use @anu??
  3 个评论
Walter Roberson
Walter Roberson 2011-10-31
Note: if there is *any* executable line in anu.m before the "function" line, then the file would be considered a script rather than a function. You need it to be a function.
Dennis
Dennis 2011-10-31
Walter i got the error after following up on your recomendations:
Warning: feval on script names will not work, or may work differently,
in a future version of MATLAB. To make your code insensitive to any change
and to suppress this warning message:
- Either change the script to a function.
- Or use eval instead of feval.
The script file in question is anu.
> In funfun\private\odearguments at 110
In ode45 at 173
??? Attempt to execute SCRIPT anu as a function.
Error in ==> funfun\private\odearguments at 110
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173
[neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, ...

请先登录,再进行评论。


Dennis
Dennis 2011-10-31
Hi Walter, When did: [t,x]=ode45(@anu,tspan,x0); i got error: Warning: feval on script names will not work, or may work differently, in a future version of MATLAB. To make your code insensitive to any change and to suppress this warning message: - Either change the script to a function. - Or use eval instead of feval. The script file in question is anu. > In funfun\private\odearguments at 110 In ode45 at 173 ??? Attempt to execute SCRIPT anu as a function.
Error in ==> funfun\private\odearguments at 110 f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ==> ode45 at 173 [neq, tspan, ntspan, next, t0, tfinal, tdir, y0, f0, odeArgs, ... Please help!

Dennis
Dennis 2011-10-31
Walter, Thank you soo much.. Dennis K
  1 个评论
pavitra ramdas
pavitra ramdas 2015-2-12
编辑:pavitra ramdas 2015-2-12
Did it work? I have the same problem. I executed it as a function and I am still getting the same error

请先登录,再进行评论。


pavitra ramdas
pavitra ramdas 2015-2-12
编辑:pavitra ramdas 2015-2-12
I have the same problem. I executed it as a function and I am still getting the same error

类别

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

标签

Community Treasure Hunt

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

Start Hunting!

Translated by