Using polyfit to predict where an object will fall?

I have information about an object thrown onto a parabolic pattern. A total of 30 pictures taken at regular intervals from the start position to the end.

Now I was able to extract the x, y coordinates of an object that was loaded in all 30 images ... I think using a polyfit (or maybe a polyval?) Can help me predict where the object will fall after the first 15 images ...

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

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

Any ideas?

Thanks!

EDIT

This is my current code:

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

      

+2


a source to share


1 answer


Here's one way to animate it, using the POLYFIT function to match the parabola, x

and y

the POLYVAL function to evaluate your polynomial in a set of values x

, and SET to change the plot objects instead of reassembling 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

      


A note on MATLAB graphics ...



Anytime a plot command is issued (eg PLOT ), then one or more machined graphic objects are created in the current axes. These objects have a "handle" or numeric identifier that acts as a reference to the plot object and which can be used to access and change the object's properties. The GET and SET commands can be used to access and change, respectively, the properties of graphical objects using their handles, which are usually returned as output arguments from graph commands.

Each type of graphic object descriptor has a set of properties. The PLOT command creates an object lineseries

with a number of properties that can be found here . For example, a property retains the x values ​​of plotted points, and a property retains the y values. You can change the x and y positions on the charts by changing these object properties . 'XData'

'YData'

lineseries

When animating graphics in MATLAB, it is generally more efficient to create an object first and update its properties during animation, rather than create, delete, and then recreate the object during animation. In the above code, the plot object for the individual points is created before the animation loop, and the handle to that object is stored in a variable hPoints

. The plot object for the parabolic line is also created before the animation loop, and its descriptor is stored in hFitLine

. The SET command is then used in the loop to change these two plot objects.

Since the parabolic line must be invisible at first, setting the initial y values ​​to NaN causes the line not to be displayed (although the object still exists). You can also make the line invisible by setting its 'Visible'

property
to 'off'

.

+3


a source







All Articles