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

  • Context: MATLAB 
  • Thread starter Thread starter physizl
  • Start date Start date
  • Tags Tags
    Functions Matlab
Click For Summary

Discussion Overview

The discussion revolves around a MATLAB function designed to compute and return the position of a projectile in terms of horizontal distance and height based on given initial conditions and time. Participants explore issues related to the function's return values and provide suggestions for resolving the problem.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • One participant describes their function and the issue of it returning only one value instead of two when called with specific parameters.
  • Another participant suggests that to receive multiple return values, the function should be called with two output variables, explaining that the first variable will hold the first return value and the second variable will hold the second return value.
  • A later reply expresses gratitude, indicating that the suggestion resolved the initial problem.
  • Another participant reiterates the original problem and provides a general explanation of using a for loop in MATLAB, though it is unclear how this relates to the issue of returning multiple values.

Areas of Agreement / Disagreement

Participants generally agree on the solution to the problem of returning multiple values from the function. However, the relevance of the for loop explanation remains unclear, and it does not directly address the initial issue.

Contextual Notes

The discussion does not explore the implications of using vectors for time values or the specific behavior of the function when handling such inputs.

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 ·
Replies
4
Views
2K
  • · Replies 16 ·
Replies
16
Views
15K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 12 ·
Replies
12
Views
4K
Replies
4
Views
2K
  • · Replies 6 ·
Replies
6
Views
6K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
3K
  • · Replies 2 ·
Replies
2
Views
5K