MATLAB help! defining functions

  • MATLAB
  • Thread starter physizl
  • Start date
  • #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.
 

Answers and Replies

  • #2
caffenta
143
0
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
physizl
9
0
thanks! that solved the problem
 
  • #4
dr.zina shadi
1
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.

------------------------
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
 

Suggested for: MATLAB help! defining functions

Replies
18
Views
1K
  • Last Post
Replies
1
Views
9K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
2
Views
6K
  • Last Post
Replies
1
Views
4K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
3
Views
2K
  • Last Post
Replies
1
Views
2K
  • Last Post
Replies
1
Views
3K
  • Last Post
Replies
1
Views
3K
Top