You are on page 1of 1

Functions are better than scripts.

A MATLAB function is a special tryp of M-file that runs in its own independent workspace. It receives
input data through an input argument list, and returns results...
Function (has code associated with it) performs some task.
(Don't see or need to be concerned with) Inputs go in Outputs come out.
M-file, declare function
give it a name, right the function code.
End with (return or end)
In the main workspace, you call the function ans inputs/outputs.
All the code Function Workspace: Calculations, intermediate variables, return variables, and then the
workspace should be deleted.
Make sure your names and the dummy variables line up, matlab won't understand if you switch the
order, term will come out in the same order regardless if the names are correct.
CAN'T JUST CALL THE FUNCTION, specify the outputs
[xs,f_xs] = function name
a,b,c are inputs () to the function
function [xs, f_xs] = guad_opt2(a,b,c)
entered by user.

<-inputs to the function, read in by the input function not

On hmwk, on 1-5 you will be writing functions and testing it with given values. Show function and
command window where you ran the function, and answer
6. Functions should be general things! Given these intial values, calculate trajectory (answers will be
arrays). Write a script that calls the function and then display and plot from the script.

You might also like