How can I output a 2 column matrix from a function?

I have a position expression and a velocity expression. I want to output a 2 column matrix with the data from t=0 to t=15. My t variable is t = linspace(0,15,100)' . Here is my code :
--------------------------
function [ x,xdot ] = finalproject( t )
% x represents position and xdot represents velocity
x = exp(-t./2).*(20.*cos(1.3229.*t)+22.6775.*sin(1.3229.*t));
xdot = exp(-t./2).*(20.*(-1./2.*cos(1.3229.*t) - 1.3229.*sin(1.3229.*t)) + 22.6775.*(-1./2.*sin(1.3229.*t) + 1.3229.*cos(1.3229.*t)));
end
------------------
In the Command Window, I know i need to have [.....] = finalproject(t) I can't seem to get it to work after trying various combinations of function output arguments and variables in the command window bracket.

 Accepted Answer

Zachary - your function is outputting two columns of 100 elements each (given your example t). So you would call this function (from the command line) as
>> [x,xdot] = finalproject(t);
You can then combine them into a two column matrix as
>> data = [x xdot];
If you want your function to output a two column matrix, then you will need to change its signature and body to
function [ result ] = finalproject( t )
% x represents position and xdot represents velocity
x = exp(-t./2).*(20.*cos(1.3229.*t)+22.6775.*sin(1.3229.*t));
xdot = exp(-t./2).*(20.*(-1./2.*cos(1.3229.*t) - 1.3229.*sin(1.3229.*t)) + 22.6775.*(-1./2.*sin(1.3229.*t) + 1.3229.*cos(1.3229.*t)));
result = [x xdot];
end

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!