在Matlab中对球进行动画处理 [英] Animating a ball in Matlab

查看:201
本文介绍了在Matlab中对球进行动画处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用以下两个方程式表示球: x(t)= v0 * cos(α)* t和 y(t)= h + v0 * sin(α)* t− 1/2 g t ^ 2,其中t∈[0,t final]是时间变量,h是高度, v0是初始速度,α是v0与水平方向的夹角,g = 9.8 m/s ^ 2.底边是y(x)= 0.

I have a ball with these two equations: x(t) = v0*cos(α)*t and y(t) = h + v0*sin(α)*t− 1/2 gt^2 , where t ∈ [0,t final] is the time variable, h is the height, v0 is the initial speed, α is the angle made by v0 with the horizontal, g = 9.8 m/s^2. The floor is at y(x) = 0.

我需要绘制一个小动画,说明在情节中移动的球.我现在应该使用,绘图,绘制,但是我不知道该怎么做.

I need to draw a small animation of the ball moving on the plot. I now I should use for, plot, drawnow, but I don't know how to do it.

你能告诉我如何产生这个动画吗?

Can you tell me how to obain this animation?

推荐答案

首先,以下是一些测试变量,包括重力引起的加速度:

First, here are some test variables to start with, including the acceleration due to gravity:

g = 9.8; %// m/s^2
v0 = 2; %// m/s
alpha = pi/6; %// Radians
h = 30; %// Start at 30 metres
t_final = 4.5; %// Seconds
t_vector = 0 : 0.01 : t_final;

代码的最后一行中的

t_vector创建一个点向量,该点从t = 0的初始时间一直到我们的结束时间(步长为0.01).定义好这些后,我们的工作就是遍历向量中的每个点并绘制球.接下来,让我们为每个xy创建匿名函数,以使绘制更加容易:

t_vector in the last line of code creates a vector of points from the initial time of t = 0 up until our ending time in steps of 0.01. With these defined, our job is to go through each of these points in our vector and plot our ball. Next, let's create anonymous functions for each x and y to make our plotting easier:

x = @(t) v0*cos(alpha)*t;
y = @(t) h + v0*sin(alpha)*t - 0.5*g*t.^2;

接下来可以做的是使用for循环,遍历t_vector的每个值,直到t_final并绘制各个点.您可能应该指出一点,以便我们可以实际看到球的形状:

What you can do next is use a for loop and go through each value of t_vector up until t_final and plot the individual point. You should probably make the point big so we can actually see what the ball looks like:

close all;
figure; hold on;
for t = t_vector
    plot(x(t), y(t), 'b.', 'MarkerSize', 16);
    axis([0 t_final 0 h]);
    pause(0.01);
end

上面的代码将首先关闭我们打开的所有图形,生成一个新的figure并使用hold on,以便我们可以多次调用plot并将点附加到图形上,而无需每次删除.然后,对于每个时间点,我们将图形上的位置绘制为蓝色点,然后确定点16的大小.我们还确保通过强制x值来确保轴不会自动调整自身限制在t = 0t = t_final之间.我们还将y的值限制为从y = 0到初始起始高度,在我的示例中为30.在每个点处,我们都会暂停0.01毫秒,以便您可以看到这些点的图形.

The above code will first close any figures we have open, spawn a new figure and use hold on so that we can call plot multiple times and append points to the graph without it erasing each time. Then, for each point in time, we plot the location on the graph as a blue dot, then make the size of the dot 16. We also make sure that the axis doesn't automatically adjust itself by enforcing that the x values are restricted between t = 0 up to t = t_final. We also restrict the y values from y = 0 up to the initial starting height, which is 30 in my example. At each point, we pause by 0.01 ms so you can see the drawing of the points.

作为奖励,这是动画gif中人物的形像:

As a bonus, this is what the figure looks like as an animated gif:

这篇关于在Matlab中对球进行动画处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆