前几天科代表说要做一个物理模型,看完题意之后发现就是一个“大摆锤”。有现成的表达式,就不用慢慢去推导了,直接照抄,瞬间完成。效果如下图~
问题与原理
啊……这两天传图一直卡死,是我电脑坏了,还是CSDN坏了。
你们先直接运行代码看效果吧,或者上B站看演示视频,见文末
代码
不废话,直接粘走吧
% 大摆球demo
clear; close all; clc
% {模型参数设置}
dt = 5e-4; % -迭代时间间隔
maxstep = 1e7; % -最大迭代次数
Vmin = 1e-4; % -迭代终止速度
Hmin = 1e-3; % -迭代终止高度
g = 9.81; % -重力加速度
mu = 5e-3; % -阻力系数
% {迭代初始化}
iter = 0; % -迭代次数
x = 0; % -初始位置
y = (x-5)^2; % -初始位置
vx = 0; % -初始速度
vy = 0; % - 初始速度
v = sqrt(vx^2+vy^2); % -初始速率
% {开始迭代绘制}
xarc = 0:0.1:10;
yarc = (xarc-5).^2;
figure('Position',[10,10,500,900])
while iter<maxstep
% [实时参数计算]
sigma = sign(vx);
theta = atan(2*(x-5));
% [更新加速度]
ax = -(g+2*vx^2)*(sin(theta)*cos(theta)+mu*sigma*v*cos(theta)^2);
ay = -g+(g+2*vx^2)*(cos(theta)^2-mu*sigma*v*sin(theta)*cos(theta));
% [更新速度]
vx = vx+dt*ax;
vy = vy+dt*ay;
v = sqrt(vx^2+vy^2);
% [更新位置]
x = x+vx*dt;
y = y+vy*dt;
% [绘制] (绘制的越多越慢哦)
if rem(iter,100)==0
plot(xarc,yarc,'--','Color',[0.8,0.8,0.8],'LineWidth',1),hold on % - 绘制轨道
plot(x,y,'r.','MarkerSize',30) % -绘制小球
quiver(x,y,vx/2,vy/2,'Color','b') % -绘制速度方向
quiver(x,y,ax/5,ay/5,'Color','g') % -绘制加速度方向
axis equal, axis([-5,15,-10,30])
text(0,-5,{['x = ',num2str(round(x,4))],['y = ',num2str(round(y,4))],...
['vx = ',num2str(round(vx,4))],['vy = ',num2str(round(vy,4))],...
['ax = ',num2str(round(ax,4))],['ay = ',num2str(round(ay,4))]}) % -写文字
hold off
drawnow
end
% [停止条件]
iter = iter+1;
if v<Vmin && y<Hmin
break
end
end
科代表你还满意吗
其他
- 哔哩哔哩视频演示与讲解:【有问不答】大摆球物理模型(MATLAB)
- 有更多问题欢迎留言