gpt4 book ai didi

physics - 使用 Scilab 求解微分方程组并绘制解

转载 作者:行者123 更新时间:2023-12-03 07:57:13 44 4
gpt4 key购买 nike

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

system

它描述了弹珠在平面上滑动的运动。我希望我的 Scilab 程序能够绘制弹珠的轨迹,即 X 轴上的 rcos(theta) 和 Y 轴上的 rsin(theta)。

我对 Scilab 完全陌生,我还没有上过任何关于如何使用它的类(class)。我根据老师给出的一些例子编写了以下代码。所以请随意解释,就像我 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

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

最佳答案

您的 ODE 格式不正确。您还需要一个变量 v = r_dot 来跟踪 r 的速度。

现在你的 ODE 应该是这样的

fig1

并更新dxdy以匹配上面的3×1向量。

关于physics - 使用 Scilab 求解微分方程组并绘制解,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/75745579/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com