File: ef230-2023-08/www/examples/trajectory.m Download
%| Solve projectile motion trajectory equation for any variable
function [ varargout ] = trajectory( varargin )
% John Doe
% EF 230 Section T1 Final Project
% December 5, 2012
%
% Purpose:
% Solve projectile motion trajectory equation for any variable
%
% Input/Output:
%   varargout and varargin are just placeholders to allow this 'app' to
%   work with the ef230 app utility
%
% Method:
%   Display a menu that allows the user to select a variable,
%   Display a dialog box that allows the user to specify parameters
%   Use fzero to solve the equation

names = {'g','x0','y0','v0','theta','x','y'};
g=9.81;
x0=0;
y0=0;
theta=45;
v0=40;
x=100;
y=0;

runflag = 'Yes';

while strcmp(runflag,'Yes')

    % get variable to solve for
    v = menu('What variable do you want to solve for?',names);
    if ~v, return; end

    % setup prompts
    prompts={'Gravity'
        'Initial X'
        'Initial Y'
        'Initial Velocity'
        'Initial Angle (degrees)'
        'Final X'
        'Final Y'
        };

    % identify variable that is being solved for
    prompts{v} = ['=>' prompts{v} ' (initial guess)'];

    % set defaults
    defs = {num2str(g)
        num2str(x0)
        num2str(y0)
        num2str(v0)
        num2str(theta)
        num2str(x)
        num2str(y)
        };

    % get parameters
    res = inputdlg(prompts,'Trajectory Parameters',1,defs);

    if isempty(res), return; end

    % extract as numbers
    [g,x0,y0,v0,theta,x,y] = str2double(res);
    g = str2double(res{1});
    x0 = str2double(res{2});
    y0 = str2double(res{3});
    v0 = str2double(res{4});
    theta = str2double(res{5});
    x = str2double(res{6});
    y = str2double(res{7});

    % set up the general equation as a function of all parameters
    yf = @(g,x0,y0,v0,theta,x)(y0+(x-x0).*tand(theta) - g./(2.*v0.^2).*(1+tand(theta).^2).*(x-x0).^2);
    genfunc = @(g,x0,y0,v0,theta,x,y)(y-yf(g,x0,y0,v0,theta,x));

    % define specific functions for each parameter
    % these anonymous functions will inherit the parameters from above
    eval(['zfunc = @(' names{v} ')genfunc(g,x0,y0,v0,theta,x,y);']);

    % call fzero with the appropriate function and starting value
    a = fzero(zfunc,str2double(res{v}));
    eval([names{v} '=a;']);

    % display result and ask to run again
    xx = linspace(x0,x,200);
    yy = yf(g,x0,y0,v0,theta,xx);
    hp = figure;
    plot(xx,yy,'-');
    title(['Solving for ' names{v}])
    hold on;
    plot([x0 x],[y0 y],'ro');
    axis equal;

    lab = { ['Solved for ' names{v}]
            sprintf('x0=%.2f',x0')
            sprintf('y0=%.2f',y0)
            sprintf('v0=%.2f',v0)
            sprintf('theta=%.1f',theta)
            sprintf('g=%.2f',g)
            sprintf('x=%.2f',x)
            sprintf('y=%.2f',y)
            };
    text(0.5,0.1,lab,'units','normalized','horizontal','center','vertical','bottom');

    txt{1} = sprintf('The value of %s is %.2f',names{v},a);
    txt{2} = 'Run again?';
    runflag = questdlg(txt,'Trajectory Results','Yes');
    close(hp)
end

end % trajectory function