# Class 18: Intro to MATLAB

MATLAB = MATrix LABoratory

How do I access MATLAB? Two options (plus one):

• Go to any of the campus computing sites where it will be installed and ready to go. This includes McDowell, Pearson, Smith, Purnell, others I don't know. This is the easiest option!
• Purchase a student copy fro MathWorks. It will cost \$99 and be good throughout your undergraduate career. It is a memory hog.
• (Semi-option) You can use http://octave-online.net/ which runs Octave a MATLAB open source copy. It is not exactly MATLAB and it has extra commands, so focus more on learning how to use MATLAB rather than Octave. For today's mini-tasks please use Octave.

Why are we learning this?

• MATLAB is THE LANGUAGE or engineers. Almost all of your future mentors will have MATLAB scripts they have collected over the years. They will expect that you can hack together a model of some system in MATLAB, with a nice pretty plot.
• By seeing the same ideas in a slightly different format you will recognize the difference between learning to program and learning a particular language.
• MATLAB is made for scientific computing, in particular numerical linear algebra. Almost everything in the world is just applied linear algebra so it's nice to play with it.
• Seeing two different languages that have different strengths gives you some perspective as you move out of the nest.

I don't like learning this way, what can I do? Here are other resources for learning MATLAB:

OK, let's do this.

## Hello World

Hello World Task 1: Head over to http://octave-online.net/ and in the Command Prompt run the command: disp('Hello world!')

Saved Script Task 2: If you register using gmail (your udel account) then you can save a script. In MATLAB scripts are saved as *.m files. So save a file names hello.m which displays your name.

In my opinion, the number one indicator of your success is the extent to which you demonstrate self-determinism. As young coders this means that you must always begin a new language by learning how to access documentation.

We get help with MATLAB (and Octet) by using help * and doc *

Get Help Task 3: Type help disp. Then type doc disp. Based on your findings what is another function which displays output to the screen?

The Internet is our friend Task 4: Use the internet to figure out how to "roll a die" in MATLAB (that is generate a uniform random integer from 1 to 6 inclusive).

Tab completion also works!

## Comparing with Python

Here is a comparison of Python and MATLAB (matlab-python-cheatsheet.pdf borrowed from Andy Roosen, thanks):

Attempt Task 5: Use MATLAB (Octave) to tell me the terms in the sequence that starts at 3 ends at 30 and takes steps of size 7.

Translator Challenge Task 6: Write a program that rolls a die 100 times and displays the number of times each number was rolled. Hint: work with the tools you have and blah += 1 works the same way it did in Python.

## Everything is a matrix

If you've never dealt with matrices before you can think of them as "lists containing fixed-length lists". The term vector is used for a single row or a single column (called a row vector or column vector, respectively).

In MATLAB everything is a matrix, so to use it well you have to think that way.

One of the most important things to understand is the indexing:

Play Task 7: Try the following operation: a = 1:10. Now run b = a + 10. Now c = a + b. Now execute a * c' and a' * c. What do you think happened?

Predict Task 8: Now predict what will happen when you perform a*3.

Mind Blowing Task 9: Now execute surf(a' * c).

Super Mind Blown Task 10: Now execute surf(sin(a)' * cos(c))!

Play Task 11: Now try plot(1:0.1:1.9, a).

Infer Task 11: Try to make a nice sine wave plot.

Let's chat about all that happened there.

## Daily Challenge

Figure out how to plot the bell curve (a normal distribution), and make a nice plot for me.