问题描述
我仍在学习Scilab(5.5.2),因此我正在编写和运行测试代码以熟悉该软件.
I am still learning Scilab (5.5.2), so I am writing and running test codes to familiarize myself with the software.
要测试数值微分方程求解器,我从方程 dy/dx = A 轻松开始,该方程具有 y = Ax + c (线性方程)
To test the numerical differential equation solver, I started easy from the equation dy/dx = A, which has as solution y = Ax+c (line equation).
这是我写的代码:
// Function y = A*x+1
function ydot=fn(x, A)
ydot=A
endfunction
A=2;
//Initial conditions
x0=0;
y0=A*x0+1;
//Numerical Solution
x=[0:5];
y= ode(y0,x0,x,fn);
//Analytical solution
y2 = A*x+1;
clf(); plot(x, y); plot(x, y2, '-k');
//End
这些是意想不到的结果:
And these are the unexpected results:
y2 = 1. 3. 5. 7. 9. 11.
y2 = 1. 3. 5. 7. 9. 11.
看来 y = e ^ x .有人可以解释出什么问题了或我做错了什么吗?
It appears that y = e^x. Can someone explain what is going wrong, or what I did wrong?
推荐答案
仅重命名变量并不会更改ODE求解器在内部使用变量的方式.由于该求解程序需要一个带有参数time,state
的函数,因此它将以这种方式解释所提供的函数.
Just renaming the variables does not change how they are used internally by the ODE solver. Since that solver expects a function with arguments time,state
it will interpret the provided function that way.
重命名变量,您所编程的内容等同于
Renaming the variables back, what you programmed is equivalent to
function ydot=fn(t,y)
ydot = y
endfunction
它确实具有指数函数作为解决方案.
which indeed has the exponential function as solution.
在手册中,您可以看到包含参数的方法是将功能作为列表传递,
From the manual you can see that the way to include parameters is to pass the function as a list,
function ydot=fn(t,y,A)
ydot = A
endfunction
y= ode(y0,x0,x,list(fn,A));
这篇关于Scilab中ODE的奇数行为:方程dy/dx = A未正确求解的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!