如何求解Scilab上的二阶微分方程?

问题描述 投票:0回答:1

我需要在Scilab上使用Runge-Kytta 4(5)来解决这个微分方程:

enter image description here

初始条件如上。间隔和h步骤是:

enter image description here

我不需要实施Runge-Kutta。我只需要解决这个问题并将结果绘制在飞机上:

enter image description here

我试图在官方的“Scilab帮助”中遵循这些说明:

https://x-engineer.org/graduate-engineering/programming-languages/scilab/solve-second-order-ordinary-differential-equation-ode-scilab/

建议的代码是:

// Import the diagram and set the ending time
loadScicos();
loadXcosLibs();
importXcosDiagram("SCI/modules/xcos/examples/solvers/ODE_Example.zcos");
scs_m.props.tf = 5000;

// Select the solver Runge-Kutta and set the precision
scs_m.props.tol(6) = 6;
scs_m.props.tol(7) = 10^-2;

// Start the timer, launch the simulation and display time
tic();
try xcos_simulate(scs_m, 4); catch disp(lasterror()); end
t = toc();
disp(t, "Time for Runge-Kutta:");

但是,我不清楚如何根据上面显示的特定微分方程改变这一点。我对Scilab有非常基本的了解。

最终的情节应该如下图所示,椭圆形:

enter image description here

只是为了提供一些数学上下文,这是描述钟摆问题的微分方程。

有人可以帮帮我吗?

=========

UPDATE

基于@luizpauloml评论,我正在更新这篇文章。我需要将二阶ODE转换为一阶ODE系统,然后我需要编写一个函数来表示这样的系统。

所以,我知道如何在笔和纸上做到这一点。因此,使用z作为变量:

enter image description here

好的,但我该如何编写普通脚本?

Xcos是一次性的。我只保留它,因为我试图模仿官方Scilab页面上的例子。

numerical-methods differential-equations scilab numerical-integration runge-kutta
1个回答
3
投票

要解决这个问题,你需要使用ode(),它可以使用很多方法,包括Runge-Kutta。首先,您需要定义一个函数来表示ODE系统,您提供的链接中的第1步显示了您要执行的操作:

function z = f(t,y)
    //f(t,z) represents the sysmte of ODEs:
    //    -the first argument should always be the independe variable
    //    -the second argument should always be the dependent variables
    //    -it may have more than two arguments
    //    -y is a vector 2x1: y(1) = theta, y(2) = theta'
    //    -z is a vector 2x1: z(1) = z    , z(2) = z'

    z(1) = y(2)         //first equation:  z  = theta'
    z(2) = 10*sin(y(1)) //second equation: z' = 10*sin(theta)
endfunction

请注意,即使t(自变量)未明确出现在您的ODE系统中,它仍然需要是f()的参数。现在你只需使用ode(),设置标志'rk''rkf'使用任何一种可用的Runge-Kutta方法:

ts = linspace(0,3,200);
theta0  = %pi/4;
dtheta0 = 0;
y0 = [theta0; dtheta0];
t0 = 0;
thetas = ode('rk',y0, t0, ts, f); //the output have the same order
                                  //as the argument `y` of f()

scf(1); clf();
plot2d(thetas(2,:),thetas(1,:),-5);
xtitle('Phase portrait', 'theta''(t)','theta(t)');
xgrid();

输出:

Phase portrait of a gravity pendulum

© www.soinside.com 2019 - 2024. All rights reserved.