MATLAB Introduction
MATH2070: Numerical Methods in Scientific Computing I
Location: http://people.sc.fsu.edu/∼jburkardt/classes/math2070 2019/matlab intro/matlab intro.pdf
Hailstone numbers cycle for a while and then drop unpredictably!
MATLAB Intro
Let’s model a particular problem, and see how MATLAB can help us.
1 The HUMP function
Suppose we are interested in the function:
y =
1
(x − 0.3)
2
+ 0.01
+
1
(x − 0.9)
2
+ 0.04
− 6.0
We can create a MATLAB function hump.m, so that we only have to type this formula correctly once. Note
that division is done with a period-slash, squaring is done with a “period-caret”, and we have to be careful
about the parentheses:
1 function y = hump ( x )
2 y = 1 . 0 . / ( ( x − 0 . 3 ) . ˆ 2 + 0 . 01 ) . . .
3 + 1 . 0 . / ( ( x − 0 . 9 ) . ˆ 2 + 0 . 04 ) . . .
4 − 6 . 0 ;
5 return
6 end
Then we can ask for the value at 2.0 by writing y=hump(2.0), or the value at a variable x by writing
y=hump(x) and so on.
Suppose we wanted to know the value of the function at 6 equally spaced points in the interval [0, 1]? We
could type the command 6 times, but a better option would be to somehow get all the answers at once. We
can create a list of the x values and get our answers back like this:
1