modelClassicModel"This is the typical equation-oriented model"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";parameterRealx0=10"Start value of prey population";parameterRealy0=10"Start value of predator population";Realx(start=x0)"Prey population";Realy(start=y0)"Predator population";equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endClassicModel;
此时,我们还没有讨论完的一件事就是 x 和 y 上的“start”属性的存在情况。正如我们在上一节“获取物理信息”中提到的牛顿冷却示例中所看到的那样,变量具有各种我们可以指定的属性(有关可用属性的详细讨论,请参阅即将出现的关于内置类型的章节)。我们之前讨论过单位属性,但这是我们第一次见到“start”属性。
细心的读者可能会注意到存在 x0 和 y0 这两个参数变量,并且它们代表的是初始数量。根据之前的例子,人们可能会预期这些初始条件能在模型中这样被体现出来:
modelClassicModelInitialEquations"This is the typical equation-oriented model"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";parameterRealx0=10"Initial prey population";parameterRealy0=10"Initial predator population";Realx(start=x0)"Prey population";Realy(start=y0)"Predator population";initialequationx=x0;y=y0;equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endClassicModelInitialEquations;
modelQuiescentModel"Find steady state solutions to LotkaVolterra equations"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";Realx"Prey population";Realy"Predator population";initialequationder(x)=0;der(y)=0;equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endQuiescentModel;
与我们之前的模型相比,此模型的主要区别在于存在那些突出显示的初始方程。观察这个模型时,您可能会好奇那些初始方程到底意味着什么。毕竟,我们需要求解的是 x 和 y。但这些变量甚至没有出现在我们的初始方程中。那么,它们是如何求解出来的呢?
请回想一下,“start”属性是具有多重含义的。在我们讨论经典洛卡-沃尔特模型时,曾指出“start”属性的一个作用是,在选择具有“start”属性的变量作为迭代变量时,它能提供一个初始猜测值。那么,我们的“静止模型”恰好就是一个这样的情况,其中 x 和 y 实际上是迭代变量,因为它们必须通过一组非线性方程来求解。这意味着,如果我们想要避免平凡解,就需要为 x 和 y 的“start”属性指定“远离”我们试图避免的平凡解的值(或者至少更接近我们所寻求的非平凡解)。例如:
modelQuiescentModelUsingStart"Find steady state solutions to LotkaVolterra equations"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";Realx(start=10)"Prey population";Realy(start=10)"Predator population";initialequationder(x)=0;der(y)=0;equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endQuiescentModelUsingStart;
modelClassicModel"This is the typical equation-oriented model"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";parameterRealx0=10"Start value of prey population";parameterRealy0=10"Start value of predator population";Realx(start=x0)"Prey population";Realy(start=y0)"Predator population";equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endClassicModel;
modelQuiescentModelUsingStart"Find steady state solutions to LotkaVolterra equations"parameterRealalpha=0.1"Reproduction rate of prey";parameterRealbeta=0.02"Mortality rate of predator per prey";parameterRealgamma=0.4"Mortality rate of predator";parameterRealdelta=0.02"Reproduction rate of predator per prey";Realx(start=10)"Prey population";Realy(start=10)"Predator population";initialequationder(x)=0;der(y)=0;equationder(x)=x*(alpha-beta*y);der(y)=y*(delta*x-gamma);endQuiescentModelUsingStart;
换句话说,唯一的实质性区别在于新增了初始方程部分(原来的经典模型已经为我们的两个变量 x 和 y 设定了非零的初始值)。理想情况下,我们可以通过仅根据该模型与另一个模型之间的差异来定义模型,从而避免任何冗余代码。事实证明,这正是 extends 关键字所允许我们做到的。以下是一个替代 QuiescentModelUsingStart 模型的示例:
modelQuiescentModelWithInheritance"Steady state model with inheritance"extendsClassicModel;initialequationder(x)=0;der(y)=0;endQuiescentModelWithInheritance;
modelQuiescentModelWithModifications"Steady state model with modifications"extendsQuiescentModelWithInheritance(gamma=0.3,delta=0.01);endQuiescentModelWithModifications;