用 Scilab 求解微分方程组并绘制解法

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

我正在尝试用 Scilab 求解这个微分方程组:

system

描述了弹子在平面上滑动的运动。 我想让我的 Scilab 程序绘制大理石的轨迹,即 X 轴上的 rcos(theta) 和 Y 轴上的 rsin(theta)。

我是 Scilab 的新手,我还没有上过任何关于如何使用它的课程。我根据老师给的一些例子写了下面的代码。所以请随意解释,就像我 5 岁一样。

这是我的代码:

// constants definition

m1 = 1 ;
m2 = 1 ;
v0 = 1 ; 
l = 1 ; 
g = 9.81 ;

function dxdt = f(t,x)
    // returns [r ddot ; theta dot]
    r = x(1) ;
    theta = x(2) ;
    
    r_ddot = m1*(l*v0)^2/(r^3*(m1+m2)) - m2*g ;// r ddot
    theta_dot = l*v0/r^2 ;// θ dot
    
    dxdt = [r_ddot; theta_dot] ;
endfunction

// initial conditions definition

r0 = 0 ;
theta0 = 0 ;

x0 = [r0,theta0] ;

// simulation range definition

t0 = 0 ; 
tf = 20 ;
Nb_points = 500 ;

time=[t0:(Nb_points-1)/(tf-t0):tf];

// system resoution

Evo_x=ode(x0,t0,time,f);

// graph plotting

r = Evo_x(:, 1) ;
theta = Evo_x(:, 2) ;

xdata = r.*cos(theta) ;
ydata = r.*sin(theta) ;

plot(xdata, ydata);
xlabel("x") ;
ylabel("y") ;
title ("Trajectory of the M1 marble")

编译成功,但轨迹没有出现在结果图中。 empty graph

你能帮我找出问题吗? 非常感谢您。

physics scilab
2个回答
0
投票

您的 ODE 格式不正确。您还需要一个变量

v = r_dot
来跟踪
r
的速度。

现在你的 ODE 应该看起来像

并更新

dxdy
以匹配上述 3×1 向量。


0
投票

你的程序有问题。首先,右侧的

r_ddot
不正确 w.r.t 你的方程式(我在下面修正了它)。此外,您忘记了
r
的一个方程,因为该方程是二阶的。你的一阶等价方程的状态是
[r,r_dot,theta]
。最后一个问题,您不应该将
r
初始化为 0(
r
在分母中)并且您的时间向量没有正确定义。

// constants definition

m1 = 1 ;
m2 = 1 ;
v0 = 1 ; 
l = 1 ; 
g = 9.81 ;

function dxdt = f(t,x)
    // returns [r dot; r ddot ; theta dot]
    r = x(1) ;
    r_dot = x(2);
    theta = x(3) ;
    
    r_ddot = (m1*(l*v0)^2/r^3 - m2*g)/(m1+m2) ;// r ddot
    theta_dot = l*v0/r^2 ;// θ dot
    
    dxdt = [r_dot; r_ddot; theta_dot] ;
endfunction

// initial conditions definition

r0 = 1 ;
rd0 = 0;
theta0 = 0 ;

x0 = [r0;rd0;theta0] ;

// simulation range definition

t0 = 0 ; 
tf = 20 ;
Nb_points = 500 ;

time=linspace(t0,tf,Nb_points);

// system resolution

Evo_x=ode(x0,t0,time,f);

// graph plotting

r = Evo_x(1,:) ;
theta = Evo_x(3,:) ;

xdata = r.*cos(theta) ;
ydata = r.*sin(theta) ;

plot(xdata, ydata);
xlabel("x") ;
ylabel("y") ;
title ("Trajectory of the M1 marble")

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