MATLAB爱好者论坛-LabFans.com

MATLAB爱好者论坛-LabFans.com (https://www.labfans.com/bbs/index.php)
-   资料存档 (https://www.labfans.com/bbs/forumdisplay.php?f=72)
-   -   使用polyfit预测对象掉落的位置? (https://www.labfans.com/bbs/showthread.php?t=23429)

poster 2019-12-10 20:41

使用polyfit预测对象掉落的位置?
 
我有一个抛物线状抛物的信息。从开始位置到结束,在特定的时间间隔内总共拍摄了30张图像。

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

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

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

有任何想法吗 ??

谢谢 !

[B]编辑[/B]

这是我当前的代码:

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

[B]回答:[/B]

这是制作动画的一种方法,可以使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/polyfit.html"]POLYFIT[/URL]函数将抛物线拟合到x和y ,使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/polyval.html"]POLYVAL[/URL]函数在一组x值处评估多项式,并使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.html"]SET[/URL]命令修改绘图对象,而不必重新绘制它们:

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 [B]关于MATLAB图形的说明... [/B]

每次发出绘图命令(例如[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html"]PLOT[/URL] ),都会在当前轴上创建一个或多个[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/learn_matlab/f3-15974.html"]手柄图形对象[/URL] 。这些对象具有“句柄”或数字标识符,可作为对图形对象的引用,并可用于访问和修改对象的属性。 [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/get.html"]GET[/URL]和[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.html"]SET[/URL]命令可用于通过其句柄分别访问和修改图形对象的属性,这些属性通常作为绘图命令的输出参数返回。

每种类型的手柄图形对象都有一组属性。 [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/plot.html"]PLOT[/URL]命令创建一个具有一系列属性的lineseries对象,可以在[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html"]此处[/URL]找到。例如, [URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#XData"]'XData'属性[/URL]存储绘制点的x值,而[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#YData"]'YData'属性[/URL]存储y值。您可以通过修改lineseries对象的这些属性来更改绘制点的x和y位置。

在MATLAB中对图形进行动画处理时,通常更有效的方法是先创建对象并在动画过程中更新其属性,而不是在动画过程中创建,删除然后重新创建对象。在上面的代码中,在动画循环之前创建了各个点的绘图对象,并将该对象的句柄存储在变量hPoints 。还在动画循环之前创建了抛物线的hFitLine对象,其句柄存储在hFitLine 。然后,在循环中使用[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/set.html"]SET[/URL]命令修改这两个绘图对象。

由于最初打算使抛物线不可见,因此将初始y值设置为全[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/nan.html"]NaN[/URL]会导致该线无法渲染(尽管该对象仍然存在)。您还可以通过将[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#Visible"]'Visible'属性设置[/URL]为'off'来使该行不[URL="http://www.mathworks.com/access/helpdesk/help/techdoc/ref/lineseriesproperties.html#Visible"]'Visible'[/URL] 。



[url=https://stackoverflow.com/questions/2837791]更多&回答...[/url]


所有时间均为北京时间。现在的时间是 09:07

Powered by vBulletin
版权所有 ©2000 - 2025,Jelsoft Enterprises Ltd.