MATLAB Why Does My MATLAB Function Only Return One Value Instead of Two?

  • Thread starter Thread starter physizl
  • Start date Start date
  • Tags Tags
    Functions Matlab
AI Thread Summary
The issue arises because the MATLAB function `projectile_position` is designed to return two values, but the user is only capturing one. To retrieve both values, the function call must be formatted correctly, using two output variables, such as `[a, b] = projectile_position(20, 5, 10, 30)`. The function computes the horizontal distance `x` and vertical height `y` based on the initial conditions and time. Additionally, the user is encouraged to explore using loops for more complex scenarios, although it is not necessary for this specific function. Properly capturing both outputs resolves the issue of receiving only one value.
physizl
Messages
9
Reaction score
0
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.
 
Physics 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
 
physizl said:
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
 

Similar threads

Replies
4
Views
1K
Replies
16
Views
14K
Replies
5
Views
1K
Replies
12
Views
3K
Replies
6
Views
5K
Replies
3
Views
3K
Replies
4
Views
2K
Replies
7
Views
3K
Replies
2
Views
4K
Back
Top