What is MATLAB ?
Other scientific softwares
There are many softwares that have similar features to MATLAB. Examples of commercial softwares are Mathematica and Maple. Free softwares include Scilab, Octave and Sage. The latter is developed in Python-based language. All of these software are available in Windows, Mac OS X, and Linux platforms.
three commonly used panels: command window, current folder, and command history
Window | Purpose |
---|---|
Command window | main window, enter variables, run programs |
Figure window | contains output from graphic commands |
Editor window | creates and debugs script and function files |
Help window | provides help information |
Command history | window & logs commands entered in the command window |
Workspace window | provides information about the variables that are used |
Current Folder | shows the files in the current directory |
A figure window contains graphs created by putting commands in the command window. This graph is generated by the following command:
>> t=linspace(0,10,500);
>> y = exp(-t).*sin(10*t);
>> plot(t,y)
First we can try simple arithmetic operations with scalars. MATLAB executes the calculations according the order of precedence just like what we have in most calculators.
Let’s compare the command:
>> 2^6/3+4
ans =
25.3333
and the following command:
>> 2^(6/3)+4
ans =
8
The first example computed 2^6 first and then the result is divided by 3, and then was added by 4. The latter computed 2^2 (from 6 divided by 3) which gives us 4 and then it was added by 4.
Use a comma (,) to seperate several commands:
>> 3+2, 5*6
ans =
5
ans =
30
How about using the semicolon (;)?
>> 3+2; 5*6
ans =
30
The first command is executed but the output is not displayed.
When a command is too long, we can use ... to break the expression:
>> 0.56699999-0.222477777411/0.147778522699^4.3555555 ...
+1.428877/8.667774
ans =
-919.9098
MATLAB allows you to assign a value to a variable. For example,
>> x = 3
We should read as ‘x is assigned a value of 3’. Then we can use this variable is subsequent calculations:
>> x=4*x-10
x =
2
Now a new value is assigned to x. The new value is 4 times the previous value of x minus 10.
To define a variable:
There are 17 words, called keywords that are reserved by MATLAB for various purposes, and cannot be used as variable names. These words are:
break case catch continue else elseif end for function
global if otherwise persistent return switch try while
Let’s see one more example:
>> x=13;
>> y=5;
>> z=(x-y)+20-x*y;
>> z
z =
-37
First the variables x,y,z are defined but they are not displayed since a semicolon is typed at the end of each statement. So when we typed the name of the variable (here z), the value of z is displayed.
Command | Outcome |
---|---|
clear | removes all variables from the memory |
clear x y | removes only variables x,y from the memory |
who | displays a list of variables currently in the memory |
whos | displays a list of variables and their size together with information about their bytes and class |
There are frequently used variables that are already defined when MATLAB is started.
Variables | Description |
---|---|
ans | a variable that has the value of the last expression |
pi | the number of |
eps | the smallest difference between two numbers and is equal to 2^(-52) |
inf | used for infinity |
i | defined as |
j | same as i |
NaN | stands for Not-a-Number |
The command format controls how the result of computations is displayed in MATLAB. The default is format short which shows at most four decimals, with long format we can view all 16 figures:
>> format long
>> 2*3^5/7
ans =
69.428571428571431
The result can be rounded to a fixed number of decimals with the function round:
>> format short
>> round(ans*1000)/1000
ans =
69.4290