## Lecture 1## Getting Matlab to runOn your own laptop...you're on your own... On Athena type (without the athena% prompt)
athena% add matlab It takes about 30-60 secs to start up... and you'll get a screen
Alternatively, you can type athena% add matlab
## ProgrammingThe most awful truth about computers is that they are all dumb. Everything must be
## The Command PromptIn the interactive terminal of Matlab you will see the command prompt (>>). This is Matlab's way of telling you that it is ready to receive instructions from you. To command Matlab, type your request and hit Enter. Matlab then evaluates your command and displays the output (if any).
To get help use the help, and lookfor commands. help should be used to find out information about a known command, e.g. help zeros. If you do not get the command prompt for some reason, you may have typed a syntax error, or have put Matlab into an infinite loop. Don't panic! press Ctrl-C and you'll get the prompt back. ## Exercises- Learn about the commands ones, zeros, sum, and diag.
- Learn about magic and verify what you learned with sum.
## Simple ExpressionsMatlab's most basic building blocks are expressions: 1, 1+1,4*3, 4^2, 5/6. To transpose a matrix (or vector) use the apostrophe ('): >> [1:10]' ## ExercisesTry to solve these with a single Matlab command: - Create the vector consisting of the whole even numbers between 15 and 27.
- Create the vector consisting of the whole odd numbers between 15 and 27.
- Create the vector of the previous question in decreasing order.
## VariablesAsking Matlab to calculate things for us is great, but most of the times a calculation is only one step out of many and the result of a calculation need to be saved for the (very near) future. To do this we use 'variables', which are simply a way to name a bit of Matlab data. This is best explained by example: >> a=1 puts the value 1 into a variable called a. typing
>> a results back in the value 1. We can also assign a calculation into a variable: >> b=1+1
and now b will contain the answer, 2. Variable names can (and should) be longer than one letter. A word or a couple of words strung together tends to work the best. ## Referencing matrix elementsWe can access the elements of a matrix in the following manner:
>> a=[1 4 3 5 6] >> a(4)
>> b=[1 2 ; 3 4] >> b(2,2)
Where the numbers in the parenthesis are (row, column). |