MATLAB TUTORIAL - PART II
The default display format is the ’short’ where we have 4 decimal digits after the fixed point. We can always change the format by typing the format command, and for more info about parameters to use, type ‘help format’ in the command window.
Below you can find the commands that can be used, with their description and an example on each case:
- format short: fixed point with 4 decimal digits >> 280/6ans =46.6667
- format long: fixed point with 14 decimal digits>> 280/6ans =46.666666666666664
- format short e: scientific notation with 4 decimal digits>> 280/6ans =4.6667e+001
- format long e: scientific notation with 15 decimal digits>> 280/6ans =4.666666666666666e+001
- format short g: best of 5-digit fixed or floating point>> 280/6ans =46.667
- format long g: best of 15-digit fixed or floating point>> 280/6ans =46.6666666666667
- format bank: two decimal digits>> 280/6ans =46.67
- format compact: eliminates empty lines to allow more lines with info to be displayed on the screen
- format loose: adds empty lines, and this is the opposite of the previous command
Now, it is a good time to explore some of the elementary functions within MATLAB. As you may know, each function has a name and an argument in parenthesis.
- sqrt(x) : name is sqrt and the argument is x, and it calculates the square root of a number
Below are some of the formats that can used:
- >> sqrt(30) ans =5.4772
- >> sqrt(30+20/5) ans =5.8310
- >> sqrt(5*sqrt(30+20/5)) ans =5.3995
Here are some useful functions and for sure you will guess what each function does:
- exp(x)
- abs(x)
- log(x) :normal logirithm
- log10(x)
- factorial(x)
- sin(x) *x is in radians
- cos(x) *x is in radians
- tan(x) *x is in radians
- cot(x) *x is in radians
- round(x)
- fix(x)
- ceil(x)
- floor(x)
- rem(x,y)
- sign(x)
Of course, it is a great idea if you explore those functions so you learn how to use them.
Remember, when you get stuck, Consult MATLAB HELP.
A good thing in MATLAB is that you can assign a numerical value to a variable which is a name made of a combination of letters and/or digits using the = operator. Variables can be used in any MATLAB statement or command.
You can say that a variable is a memory location to store the numerical value. You can store a numerical value of a computable expression like in the example below:
x =
7
>> y=5*x+22
y =
57
>> Z=(x-y)+10/x
Z =
-48.5714
He are some useful rules to take into account when using variables names:
- name length up to 63 characters in MATLAB 7 and 31 characters in MATLAB 6.x
- contains letters, digits, underscores
- must begin with a letter
- avoid using names of built-in functions or predefined variables
And here are some predefined variables:
- pi = 3.1416….
- eps = the smallest difference between two numbers is 2^(-52) =
2.2204e-016 - inf = infinity
- i = sqrt(-1)
- j = same as i
- Nan= Not a Number whichi is used in MATLAB when it can’t define a valid numerical value, for example 0/0
Since from now and on we will be using variable a lot, it is good to learn some command to manage these variables:
- clear : removes all variables from memory
- clear x y z : removes on x, y and z from memory
- who : displays a list of of the variables currently in memory
- whos : displays a list of the variables currently in memory and their size together with the information about their size and class.
Having read all the above, I guess you have a good background to start working with Arrays and Matrices which is the most important thing in MATLAB. Click on the link below for the next tutorial.
Filed under: MATLAB

Leave a Reply
You must be logged in to post a comment.