1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Homework Help: Program to plot 3D trajectory?

  1. Oct 12, 2012 #1
    Howdy folks,

    I have been given the opportunity to do some extra credit for my mechanics class. As an assignment, my prof would like to see a 3D trajectory plotted via a program (like Python).

    My basic assignment is to plot the trajectory of a soccer ball after it has been kicked. I don't need help with the physics equations, but I need to know what programs I can use and if one is better than the other. No worries learning the coding language, I have a lot of experience with Java, vb script, etc...

    Ideally, I'd like to use a program that has a simple 3D plot of trajectory, and it would be even better if I could save the results as an independent program. If not, no big deal. Also, it would be superb if I could put in switches in the graphical output for things like turning wind on and off, air resistance, etc... If not, I'm sure I can just write multiple programs for each instance.

  2. jcsd
  3. Oct 13, 2012 #2


    User Avatar
    Science Advisor
    Homework Helper
    Gold Member

    I would use Maple (since that's the one I have experience with) or something equivalent to it.
  4. Oct 13, 2012 #3


    User Avatar
    Gold Member

    MATLAB is generally the standard in the dynamics community. FreeMat and Octave are open source clones of MATLAB.

    There is a plot3 command in MATLAB that will do what you want.

    Here's an example MATLAB simulation of a mass attached to a spring that behaves as a pendulum; its equations of motion are:
    \ddot{r} &= r\dot{\theta}^2 - \frac{k}{m}(r-l) \\
    \ddot{\theta} &= -\frac{2\dot{r}\dot{\theta}}{r}
    Code (Text):

    [color=#408080][i]%% Example simulation of a dynamical system.[/i][/color]
    r0 = 1;
    l = 1;
    rdot0 = [color=#008000]rand[/color](1,1);
    theta0 = [color=#008000]rand[/color](1,1);
    thetadot0 = [color=#008000]rand[/color](1,1);
    x0 = [r0 theta0 rdot0 thetadot0][color=#666666]'[/color];

    [t x] = ode45(@pendulum, [0 100], x0);

    r = x(:, 1);
    theta = x(:, 2);

    x = r [color=#666666].*[/color] [color=#008000]sin[/color](theta);
    y = r [color=#666666].*[/color] [color=#008000]cos[/color](theta);

    plot(x, y, x(1), y(1), [color=#BA2121]'+g'[/color], x([color=#008000][b]end[/b][/color]), y([color=#008000][b]end[/b][/color]), [color=#BA2121]'xr'[/color], [color=#BA2121]'MarkerFaceColor'[/color], [color=#BA2121]'r'[/color], [color=#BA2121]'linewidth'[/color], 3);
        tl = title([color=#BA2121]'X-Y Position of Spring-Mass Pendulum'[/color]);
        xl = xlabel([color=#BA2121]'x'[/color]);
        yl = ylabel([color=#BA2121]'y'[/color]);
        set([tl xl yl], [color=#BA2121]'fontsize'[/color], 16);
        legend([color=#BA2121]'Path'[/color], [color=#BA2121]'Start'[/color], [color=#BA2121]'End'[/color], [color=#BA2121]'Location'[/color], [color=#BA2121]'NorthEastOutside'[/color]);
    Code (Text):

    [color=#008000][b]function[/b][/color][color=#bbbbbb] [/color] xdot =[color=#bbbbbb] [/color][color=#0000FF] pendulum[/color](t, x)[color=#bbbbbb]
        [/color]m = 1;
        k = 1;
        l = 1;

        r = x(1); theta = x(2); v = x(3); w = x(4);
        xdot = [v;
                r [color=#666666].*[/color] w[color=#666666].^[/color]2 [color=#666666]-[/color] k[color=#666666]/[/color]m [color=#666666]*[/color] (r [color=#666666]-[/color] l);
                [color=#666666]-[/color]2 [color=#666666]*[/color] v [color=#666666]*[/color] w [color=#666666]/[/color] r;
    What you want to do will be much simpler than this, but this example should give you a taste of MATLAB.
    Last edited: Oct 13, 2012
Share this great discussion with others via Reddit, Google+, Twitter, or Facebook