使用polyfit预测对象掉落的位置? [英] Using polyfit to predict where the object falls?

查看:101
本文介绍了使用polyfit预测对象掉落的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有抛物线状抛物的信息.从开始位置到结束,在特定时间间隔内总共拍摄了30张图像.

I have information of an object being thrown at a parabolic pattern. There are 30 images in total taken at specific intervals from the start position till the end.

现在我已经设法提取了所有30张图像中抛出的对象的x,y坐标... 我认为使用polyfit(或也许polyval?)可以帮助我预测在前15张图像后对象将掉落的位置...

Now I have managed to extract the x,y coordinates of the object being thrown in all 30 images... I think that using polyfit (or maybe polyval ? ) may help me predict where the object will fall after the first 15 images ...

我只想知道,polyfit如何与我拥有的30 x,y坐标一起使用?

I just want to know, how can polyfit be used with the 30 x,y coordinates I have ?

((我有一个循环,一次从mat文件中提取每张图像,直到30 ..然后绘制该图像..因此,在绘制之前/之后,我应该在同一循环中使用polyfit ???

( I have a loop to extract each image from a mat file 1 row at a time until 30 .. and then plot that image .. so should I use polyfit in the same loop before/after the plot ???

任何想法

谢谢!

编辑

这是我当前的代码:

load objects.mat
for G=1:30
    x=objects(G,1);
    y=objects(G,2);
    plot(x,y,'0')
    hold on
    drawnow()
end

推荐答案

这是使用功能

Here's one way you could animate this, using the function POLYFIT to fit a parabola to x and y, the function POLYVAL to evaluate your polynomial at a set of x values, and the SET command to modify your plot objects instead of having to replot them:

load objects.mat   %# Load the data
x = objects(:,1);  %# Get the x data
y = objects(:,2);  %# Get the y data
N = numel(x);      %# The number of points
hPoints = plot(x(1),y(1),'r*');       %# Plot first point as a red asterisk,
                                      %#   saving the handle
hold on;                              %# Add to the plot
hFitLine = plot(x,nan(N,1),'b-');     %# Initialize the plot for the fit line,
                                      %#   saving the handle and using NaN for
                                      %#   the y values so it doesn't appear yet
axis([min(x) max(x) min(y) max(y)]);  %# Set the axis limits
for k = 1:N
  set(hPoints,'XData',x(1:k),'YData',y(1:k));  %# Update the points
  if k >= 15                       %# Plot a fit line starting at k = 15
    p = polyfit(x(1:k),y(1:k),2);  %# Fit a parabola with points 1 through k
    yFit = polyval(p,x);           %# Evaluate the polynomial at all x
    set(hFitLine,'YData',yFit);    %# Update the fit line
  end
  drawnow();    %# Force the plot to refresh
  pause(0.25);  %# Pause for a quarter second
end


关于MATLAB图形的注释...

每当发出绘图命令时(例如 PLOT ),然后一个或多个处理图形对象在当前轴中创建.这些对象具有句柄"或数字标识符,可作为对图形对象的引用,并可用于访问和修改对象的属性. GET


A note on MATLAB graphics...

Any time a plotting command is issued (like PLOT), then one or more handle graphics objects are created in the current axes. These objects have a "handle", or a numeric identifier, that acts as a reference to the plot object and which can be used to access and modify the properties of the object. The GET and SET commands can be used to access and modify, respectively, the properties of graphics objects using their handles, which are typically returned as output arguments from the plot commands.

每种类型的手柄图形对象都有一组属性. PLOT 命令使用以下命令创建一个lineseries对象:可以在此处找到的许多属性.例如, 'XData'属性存储绘制点的x值,而 'YData'属性存储y值.您可以通过修改lineseries对象的这些属性来更改绘制点的x和y位置.

Each type of handle graphics object has a set of properties. The PLOT command creates a lineseries object with a number of properties which can be found here. For example, the 'XData' property stores the x values of the plotted points, while the 'YData' property stores the y values. You can change the x and y positions of the plotted points by modifying these properties of the lineseries object.

在MATLAB中对图形进行动画处理时,通常更有效的方法是先创建对象并在动画过程中更新其属性,而不是在动画过程中创建,删除然后重新创建对象.在上面的代码中,在动画循环之前创建了各个点的绘图对象,并将该对象的句柄存储在变量hPoints中.还在动画循环之前创建了抛物线的打印对象,其句柄存储在hFitLine中.然后,在循环中使用 SET 命令修改这两个绘图对象.

When animating graphics in MATLAB, it is generally more efficient to create the object first and update its properties during the animation instead of creating, deleting, then recreating the object during the animation. In the code above, a plot object for the individual points is created before the animation loop and the handle for that object is stored in the variable hPoints. A plot object for the parabolic line is also created before the animation loop, and its handle is stored in hFitLine. Then, the SET command is used in the loop to modify these two plot objects.

由于抛物线起初是不可见的,因此将初始y值设置为全部 属性'off'.

Since the parabolic line is intended to be invisible at first, setting the initial y values to be all NaN causes the line to not be rendered (although the object still exists). You could also make the line invisible by setting its 'Visible' property to 'off'.

这篇关于使用polyfit预测对象掉落的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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