MATLAB help defining functions

In summary, the conversation discusses the implementation of a function named projectile_position that calculates and returns the distance and height of a projectile based on initial conditions and time. The conversation also mentions the use of flow control statements, specifically the 'for' loop, to execute statements a predetermined number of times. The example given is using the 'for' loop to calculate the values of a matrix.
  • #1
physizl
9
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
  • #2
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.
 
  • #3
thanks! that solved the problem
 
  • #4
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
 
  • #5


I understand your frustration with trying to define a function in MATLAB. It can be a complex and time-consuming process. However, it is important to carefully review the instructions for your assignment and make sure you are meeting all the requirements.

In this case, it seems like you have successfully defined a function that calculates the position of a projectile at a given time. However, the problem you are facing is that your function is only returning one value instead of two. This could be due to a few different reasons.

First, make sure that you are correctly defining the outputs of your function. In this case, you have specified that your function should return [x,y] as the outputs. However, in your code, you are only assigning values to x and y separately. To fix this, you can simply change the last line of your code to:

[x, y] = [vi*cos(angle)*time; h + vi*time*sin(angle) + 0.5*9.81*time^2];

This will ensure that your function returns both x and y values.

Another possible issue could be with the inputs of your function. Make sure that you are inputting the correct values and in the correct order. In your example, you have inputted the values (20,5,10,30) which correspond to vi, h, time, and angle respectively. However, if you are expecting [x,y] as the output, the order of the inputs should be (vi, h, angle, time) according to your function definition.

I hope this helps you solve the issue and complete your assignment successfully. Remember to always carefully read and follow the instructions, and don't hesitate to seek help from your instructor or classmates if you are still having trouble. Good luck!
 

Related to MATLAB help defining functions

1. What is a function in MATLAB?

A function in MATLAB is a reusable block of code that performs a specific task. It takes input arguments, performs operations, and returns an output value.

2. How do I define a function in MATLAB?

To define a function in MATLAB, use the syntax "function [output] = functionName(input1, input2, ...)" followed by the code to be executed within the function. Make sure to include a "end" statement at the end of the function.

3. Can a function have multiple output values?

Yes, a function in MATLAB can have multiple output values. Simply list the output variables within square brackets after the function name, separated by commas.

4. How do I call a function in MATLAB?

To call a function in MATLAB, use the function name followed by the input arguments within parentheses. The output values will be returned and can be stored in variables or used in other calculations.

5. Can I use built-in functions within a user-defined function in MATLAB?

Yes, you can use built-in functions within a user-defined function in MATLAB. Simply call the built-in function within the function code, and make sure to include any necessary input arguments.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
18
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
5K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
8
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
2K
Replies
4
Views
1K
Back
Top