简体   繁体   English

微分方程 八度中的功能

[英]Differential equation; function in Octave

I try to solve a differential equation in Octave. 我尝试在Octave中求解微分方程。 In a first attempt, all my independent variables are set constant (n,y,w). 第一次尝试,将我所有的自变量设置为常数(n,y,w)。 This is my code: 这是我的代码:

function xdot= f(x,t);

  % xdot=zeros(1,1);

  X=1.44221E+12;
  IO=5.318E+11;
  GO=6.81E+11;
  beta=0;
  gamma=0;
  y=58.5021088;
  w= 31.29;
  n=1363.5;
  tw=0.4;
  tp=0.3;
  sw=0.07;
  sp=0.25;
  mw=0.593941689;
  mp=0.593941689;

  % aw=(1-tw)(sw+mw)
  % ap=(1-tp)(sp+mp) 

  xdot=-(X+IO*(1+gamma*(diff(n)/n+diff(y)/y))+GO*beta(tw(diff(n)/n+diff(w)/w)+tp(diff(y)/y-diff(w)/w)-(x*n)/((y-w)((1-tp)(sp+mp)+tp)+((1-tw)(sw+mw)+tw)*w))*x)/(IO*gamma+GO*beta*tw);


 endfunction

When I add 当我添加

 t = [0:(1/360):10]

 x = lsode ("f", 39290000, t);

to solve the equation in the command line, I get this error: 在命令行中求解方程式,出现以下错误:

error: index (0.843942): subscripts must be either integers 1 to (2^31)-1 or logicals
error: lsode: evaluation of user-supplied function failed
error: called from
f at line 23 column 7

It seems to me, in some way I misunderstood how to make the function. 在我看来,我在某种程度上误解了如何制作该功能。 Any help? 有什么帮助吗?

EDIT: 编辑:

This is my new code: 这是我的新代码:

function xdot= f(x,t);

 X=1.44221E+12;
 IO=5.318E+11;
 GO=6.81E+11;
 beta=0;
 gamma=0;
 y=58.5021088;
 w= 31.29;
 n=1363.5;
 tw=0.4;
 tp=0.3;
 sw=0.07;
 sp=0.25;
 mw=0.593941689;
 mp=0.593941689;

 xdot=-X+IO-(x*n)/((y-w)*((1-tp)*(sp+mp)+tp)+w*(tw+(1-tp)*(sp+mp)))

endfunction

It does work if I copy it into the command line but if I start it as programm (fm) and solve it then I get this error: 如果我将其复制到命令行中,它确实可以工作,但是如果我以programm(fm)的形式启动并解决它,那么我会收到此错误:

error: 'x' undefined near line 25 column 15
error: called from
f at line 25 column 7

From the documentation of lsode : lsode文档中

Syntax: [x, istate, msg] = lsode (fcn, x_0, t) 语法: [x, istate, msg] = lsode (fcn, x_0, t)
... ...

The first argument, fcn , is a string, inline, or function handle that names the function f to call to compute the vector of right hand sides for the set of equations. 第一个参数fcn是一个字符串,内联函数或函数句柄,该函数句柄将函数f命名为要调用的函数,以计算方程组的右侧向量。 The function must have the form 该函数必须具有以下形式

 xdot = f (x, t) 

in which xdot and x are vectors and t is a scalar. 其中xdot和x是向量,t是标量。

So firstly, your f function is of the wrong form to the one expected by lsode , since it takes 5 arguments as opposed to 2. 因此,首先,您的f函数形式与lsode期望的形式错误,因为它需要5个参数而不是2个。

Secondly, inside your f function, the variables y , w , and n are overwritten, regardless of whether they were supplied, whereas t and x need to be supplied by the user. 其次, 您的f函数内部 ,变量ywn被覆盖,无论是否提供了变量,而tx需要由用户提供。

When lsode calls the handle f , it calls it with two arguments as per its specification, which means it instantiates t and y , and calls the function with the remaining input arguments undefined. lsode调用句柄f ,它将按照其规范使用两个参数来调用它,这意味着它将实例化ty ,并使用未定义的其余输入参数来调用该函数。 Of the remaining arguments, w and n are given values inside the function, as we pointed out above, but x remains undefined. 如上文所述,在其余参数中, wn被赋予了函数内部的值,但x仍未定义。 So when you run the final calculation which relies on x , you get an error saying it has not yet been defined during the call to this function. 因此,当您运行依赖x的最终计算时,会收到一条错误消息,指出尚未在调用此函数期间对其进行定义。

So, to fix this issue, rewrite your function such that its signature is f(x, t) , which you can do so easily since y , w , and n are defined inside your function anyway, so they don't really need to be given as inputs. 因此,要解决此问题,请重写函数,使其签名为f(x, t) ,因为ywn始终在函数内部定义,因此您可以轻松地做到这一点,因此它们实际上并不需要作为输入给出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM