有一个在数学中求解微分方程的例子。关于数学。js主页,但它相当复杂,并没有为我个人提供足够的信息来应用数学。其他类似问题。所以,我要做的是解决
LotkaâVolterra equations for predator-prey simulation
. 系统中有两个方程:
dx/dt = ax - bxy
dy/dt = cxy - y
这在数学中再次出现。js,我得到了
math.import({ndsolve:ndsolve});
const sim2 = math.parser();
sim2.eval("dxdt(x, y) = x - x * y");
sim2.eval("dydt(x, y) = x * y - y");
sim2.eval("dt = 1.0 s"); // Simulation timestep
sim2.eval("x0 = 0");
sim2.eval("y0 = 0");
sim2.eval("tfinal = 100 s"); // Simulation duration
sim2.eval("result_stage1 = ndsolve([dxdt, dydt], [x0, y0], dt, tfinal)");
哪里
ndsolve
来自火箭轨迹示例:
http://mathjs.org/examples/browser/rocket_trajectory_optimization.html.html
function ndsolve(f, x0, dt, tmax) {
var n = f.size()[0]; // Number of variables
var x = x0.clone(); // Current values of variables
var dxdt = []; // Temporary variable to hold time-derivatives
var result = []; // Contains entire solution
var nsteps = math.divide(tmax, dt); // Number of time steps
for(var i=0; i<nsteps; i++) {
// Compute derivatives
for(var j=0; j<n; j++) {
dxdt[j] = f.get([j]).apply(null, x.toArray());
}
// Euler method to compute next time step
for(var j=0; j<n; j++) {
console.log(x.get([j]));
console.log(dt);
x.set([j], math.add(x.get([j]), math.multiply(dxdt[j], dt)));
}
result.push(x.clone());
}
return math.matrix(result);
}
然而,运行这段代码我得到了错误
函数add中的参数类型意外(应为:数字或复数或大数字或分数,实际为:单位,索引为1)
这个错误的来源是什么?我是否缺少正确的单位?
感谢您的帮助。