Functions

advertisement
Functions
http://www2.warwick.ac.uk/fac/sci/moac/degrees/modules/tutorial/matlab
Review
• Script files
– Run a list of commands
• Flow Control
– If, for, while, etc
Functions
– All variables are local variables
Example
Fibonacci Sequence
• Write a function that calculates the nth
number in the Fibonacci Sequence
f1  0
f2  1
f n  f n 1  f n  2
My solution
or
or
or
Multiple inputs and outputs
• Comments + good layout make things easier
to read, correct, use and re-use.
Solving Equations
• Write a function that solves
ax  bx  c  0
2
• For real roots, otherwise displays an error.
• Also creates a plot of this function.
• And displays the real roots.
My Solution
What is the difference between a
script and a m-file?
Pendulum Simulation

g




sin

2
t
l
2
• Inputs:
– Length of pendulum
– Starting pendulum angle
• Hints:
– To start just draw the pendulum in its starting
position
– Use Euler rule
Download