Matlab Tutorial Lesson 1
Matlab Tutorial Lesson 1
Goal
The goal with this worksheet is to give a brief introduction to the mathematical software Matlab. After completing the worksheet you should know how to start Matlab, how to use the elementary functions in Matlab and how to use Matlab to plot functions.
What is Matlab?
Matlab is widely used in all areas of applied mathematics in education and research at universities and in the industry. Matlab stands for MATrix LABoratory and the software is built up around vectors and matrices. This makes the software particularly useful for linear algebra but Matlab is also a great tool for solving algebraic and differential equations and for numerical integration. Matlab has powerful graphic tools and can produce nice pictures in both 2D and 3D. It is also a programming language (similar to C) and is one of the easiest programming languages for writing mathematical programs. Matlab also has some tool boxes useful for signal processing, image processing, etc.
in Intro to programming). In the command window you will see a prompt, on Macs and PCs this prompt is >> . You type your commands immediately after this prompt. Once you have typed the command you wish Matlab to perform, press <enter>. If you want to interupt a command that Matlab is running, type <ctrl> + <c>.
at the prompt. Be careful with parantheses and don't forget to type * whenever you multiply! Note that Matlab is case sensitive. This means that Matlab knows a difference between letters written as lower and upper case letters. For example, Matlab will understand sin(2) but will not understand Sin(2). Here is a table of useful operations, functions and constants in Matlab. Operation, function or constant + (addition)
-
Matlab command
+ * / abs(x) sqrt(x) exp(x) log(x) log10(x) sin(x) cos(x) tan(x) cot(x)
(subtraction)
(multiplication) / (division) |x| (absolute value of x) square root of x ex ln x (natural log) log10 x (base 10 log) sin x cos x tan x cot x
pi
Exercises
Compute the following expressions using Matlab:
3cos(pi)
1+1+1/2+1/6+1/24-e ln (1000+2pi-2) e i pi The number of combinations in which 12 persons can stand in line. (Hint: Use factorials.)
Variables in Matlab
We can easily define our own variables in Matlab. Let's say we need to use the value of 3.5sin(2.9) repeatedly. Instead of typing 3.5*sin(2.9)over and over again, we can denote this variable as x by typing the following:
x=3.5*sin(2.9)
(Please try this in Matlab.) Now type
x+1
and observe what happens. If we do not want to see the result of a calculation we can use semi-colon to surpress the print-out on the screen even though Matlab still performs the command in "the background". If you defined x as above, now type
y=2*x; y
and observe what happened. In many cases we want to know what variables we have declared. We can do this by
typing whos. If you want to erase all variables from the Matlab memory, type clear. To erase a specific variable, say x, type clear x. To clear two specific variables, say x and y, type clear x y, that is separate the different variables with a space.
is created by typing
A=[1 2 3 ; 4 5 6 ; 7 8 9 ],
i.e., rows are separated with semi-colons. If we want to use a specific element in a vector or a matrix, study the following example: Example:
Note that all text must be put within ' '. The last two commands (hold on and hold off) are best explained by trying them next time you plot.
Exercises
Plot sin(x) on the interval [-pi,pi] using spacing 0.5, 0.1 and 0.01 between the points where you will sample the function. (This will change the resolution). Experiment with the hold on command. Attach labels to the axis of the previous plot and give a title to the graph.
Plot 5 cos(x2+1) on [-2pi,2pi]. Note that the squaring operation will require you to use the dot . in order for the squaring operation to act on each element individually. However, the addition operation (+) automatically acts on elements individually.