# MATLAB help! defining functions

• MATLAB

## Main Question or Discussion Point

been looking online and messing around with different codes for almost a couple hours now...

but my assignment is:

" Define a function named projectile_position that computes and returns [x,y] where x is the distance a projectile has traveled along the ground from its position at time 0 seconds and y is the height of the projectile in meters. Your function must compute these values based on the initial conditions at release and the current time in seconds (t). This function must also work for vectors of time values. "

this is what my .m file looks like

========================================================

function [x, y] = projectile_position( vi, h, time, angle )
%
% vi = initial velocity of projectile
% h = initial height of projectile
% time = time it takes projectile to cease motion after it's trajectory
% angle = angle at which projectile is launched
%
x = vi*cos(angle)*time;
y = h + vi*time*sin(angle) + 0.5*9.81*time^2;

========================================================

problem is when i input some values after defining this function, for example:

projectile_position( 20, 5, 10, 30)

it only returns one value, but i need it to return 2!! any help is greatly appreciated.

Related MATLAB, Maple, Mathematica, LaTeX News on Phys.org
If you want more than one result value, you have to do it like this:

[a, b]=projectile_position( 20, 5, 10, 30)

The variable a will hold the return value x, and b the value y.

a and b are just examples. You can use any variable names you want, including x and y.

thanks! that solved the problem

been looking online and messing around with different codes for almost a couple hours now...

but my assignment is:

" Define a function named projectile_position that computes and returns [x,y] where x is the distance a projectile has traveled along the ground from its position at time 0 seconds and y is the height of the projectile in meters. Your function must compute these values based on the initial conditions at release and the current time in seconds (t). This function must also work for vectors of time values. "

this is what my .m file looks like

========================================================

function [x, y] = projectile_position( vi, h, time, angle )
%
% vi = initial velocity of projectile
% h = initial height of projectile
% time = time it takes projectile to cease motion after it's trajectory
% angle = angle at which projectile is launched
%
x = vi*cos(angle)*time;
y = h + vi*time*sin(angle) + 0.5*9.81*time^2;

========================================================

problem is when i input some values after defining this function, for example:

projectile_position( 20, 5, 10, 30)

it only returns one value, but i need it to return 2!! any help is greatly appreciated.
------------------------
you can use one of the flow Control statements
it is the (for ) statement

the for loop executes a statement or group statements a predetermined number of times.
its syntax is :
for index=start:increment:end
statements
end
you can specify any increment including a negative one
for example
for i=3:6
x(i)=5*x;
end

also you can use multiple
for i=1:n
for j=1:m
A(i,j)=4*(i+j);
end
end

best wishes