You are on page 1of 11

Ton Duc Thang University

Faculty of Information Technology

Linear Algebra
Module code: 501032

1 Objective
In this Lab you are going to study about:
• Variables

• Input and Output


• Scripts
• Operations

• Condition structures

2 MATLAB software
2.1 How to use MATLAB software.
After you installed MATLAB on your computer, you will open graphic user
interface(GUI) on menu and you can see the same figure below.

• The Command Prompt ” >> ” is displayed in the Command Win-


dow.

1
Ton Duc Thang University
Faculty of Information Technology

• The Workspace: show list all variables that you have created so far
and their values. Other informationis also displayed here, such as the
maximum and minimum values of any variables that are created.

• The Command History: records all of the commands that you have
previously typed at the Command Prompt. You can select one of these
commands using the mouse and execute it in the Command Window by
double-clicking it from within the Command History.
• Current Folder You can direct MATLAB to set up a folder of your
choice to the current folder. This will then be your working folder, where
you can save your programs.
• Comment: anything following a % is seen as a comment. You can add a
comment to MATLAB code by insert a percentage sign at the beginning
of the line.

2.2 Help/ Docs


• help is the most important function for learning MATLAB on your own.
>> h e l p round

• doc get a nicer version of help with examples and easy to read descriptions.
>> doc round

You can type the commands above and compare with the results on the screen.

2.3 Variable
MATLAB is a weakly typed language so it doesn’t need to initialize vari-
ables. MATLAB support various types, for example double(default), char,
int,etc...Beside, other types are also supported: complex, symbolic, integer,
etc...
If you want to create a x variable, you can type the command below
>> x =0.3

x =

0.3000
To create a variable you assign a value to a name. But you have to noticed
these rules.
• first character must be a LETTER
• after that, any combination of letters, numbers and

2
Ton Duc Thang University
Faculty of Information Technology

On the other hand, MATLAB built in some variable and you don’t use these
name
• i and j can be used to indicate complex numbers
• pi as the value 3.1415926. . .
• ans stores the last unassigned value (like on a calculator)
• Inf and -Inf are positive and negative infinity
• NaN represents ‘Not a Number’
In MATLAB, it provides scalars variable concept which you can understand
that a variable can be given a value explicitly or as a function of explicit values
and existing variables.

2.4 Input and Output


1. To display information on the screen.
If you want to show variable value you only type the variable name.
>> a = 9

a =

but you suppress output, end the line with a semicolon.


>> a = 9 ;

If you want to show a string, MATLAB provides some functions as disp


or fprintf.
>> disp ( ’ I am s t u d y i n g L i n e a r Algebra L a b o r a t o r y ˆˆ ’ ) ;

or
>> f p r i n t f ( ’%s \n ’ , ’ I am s t u d y i n g L i n e a r Algebra L a b o r a t o r y ’ ) ;

When you want to show variable value with the message ’I have ...’, type
the statement:
>> d i s p ( [ ’ I have a v a r i a b l e e q u a l t o ’ num2str ( a ) ] )
I have a v a r i a b l e e q u a l t o 9

2. To enter value for variable.


MATLAB provides input(promt) command For example, write a program
to enter your information and display some texts.
>> s t r=input ( ’What i s your name? ’ , ’ s ’ ) ;
disp ( [ s t r ’ must t r y hard t o improve your s e l f ! ’ ] ) ;

3
Ton Duc Thang University
Faculty of Information Technology

2.5 Scripts
Scripts are collection of commands executed in sequence and written in the
MATLAB editor. Besides you can save your commands as MATLAB files which
have .m extension.
You can create a MATLAB file from command line
>> e d i t h e l l o W o r l d .m
or click on the GUI

2.6 Basic scalars operations


1. Arithmetic operations (+, −, ∗, /)
>> 4/6

4
Ton Duc Thang University
Faculty of Information Technology

ans =

0.6667

>> (1+ i )∗(2+ i )

ans =

1.0000 + 3.0000 i

2. Exponentiation (ˆ)
>> 4ˆ2

ans =

16

>> (3+4∗5 i ) ˆ 2

ans =

−3.9100 e+02 + 1 . 2 0 0 0 e+02 i

2.7 Built in functions


MATLAB provides some math functions
Function MATLAB Example
xn xˆ2 x2 −→ x.ˆ2
en exp(x) e−5 −→ exp(-5)
lnx log(x) lnπ −→ log(pi)
sinhx, coshx sinh(x), cosh(x) sinh1−→ sinh(1)
sinx, cosx sin(x), cos(x) cosπ −→ cos(pi)
sin−1 x or arcsinx asin(x) √ 0.5 −→ asin(0.5)
arcsin

x sqrt(x) 2.5 −→ sqrt(2.5)

2.8 Logic operators


• MATLAB uses mostly standard relational operators

5
Ton Duc Thang University
Faculty of Information Technology

Description symbol
equal ==
not equal ∼=
greater than >
less than <
greater or equal >=
less or qual <=

• Logical operators

Description Elementwise Short-circuit(Scalars)


And & &&
Or | k
Not ∼
Xor xor
All true all
Any true any

2.9 Condition statements

Example Write a MATLAB function that has one input argument and returns
one value. Name this fuction chang it() . This function operates as follows:
• If the value of the input argument is greater than 0, the function returns
the value 10.

• If the value of the input argument is less than or equal to 0, the function
returns the value of the input argument.
You should solve it by the way.
function y = c h a n g e i t ( x )
i f ( x>0)
y =10;
end

6
Ton Duc Thang University
Faculty of Information Technology

i f ( x<=0)
y=x ;
end
end
or the following way
function y = c h a n g e i t ( x )
i f ( x>0)
y =10;
else
y=x ;
end
end

2.10 Loop statements


The syntax of a for statement is
f o r i t e r a t i o n V a r i a b l e= i n i t i a l v a l u e : i n c r e m e n t : f i n a l v a l u e
commands
end

Example Write a MATLAB program to calculate the summation of n integer.


The summation of n is given by the following equation:
Pn
summation = i=1 i
where n is entered by user when call function.
You should solve by typing commands.

7
Ton Duc Thang University
Faculty of Information Technology

summation = 0 ;
for i =1:1: n
summation= summation + i ;
end
or the another way:
i = 1;
summation = 0 ;
while ( i<=n )
summation = summation + i ;
i = i + 1;
end

3 Exercises
3.1 Exercise 1:
Write a MATLAB program to calculate the summation of a, b, c integer.
Where a, b, c is obtained to keyboard.

3.2 Exercise 2:
Write a MATLAB program that produces the factors of a number. Hint: You
should use factor function is built in MATLAB.

3.3 Exercise 3:
Write a MATLAB program that produces the greatest common divisor of two
numbers. Hint: You should use gcd function is built in MATLAB.

3.4 Exercise 4:
Write a MATLAB program that produces the least common multiple of two
numbers. Hint: You should use lcm function is built in MATLAB.

3.5 Exercise 5:
Write MATLAB functions to calculate r to the following equations

x + yz
r= −1 (1)
y2
ecos(2x)+1 + 2x
r= (2)
ln(2x2 + 1) + 2
1
r = tan−1 (xtan(x 3 )) (3)

8
Ton Duc Thang University
Faculty of Information Technology

cos(2x+1)+2
32 −4
r= √
2 y
(4)
sin−1 (0.1z)
n
X 1
r= 2+x
, where x, n is an integer and x 6= 0, -1 (5)
x=−10
x

Where x, y, z, n are entered on Command Window.

3.6 Exercise 6:
Write a MATLAB fuction that has four input arguments and returns the max-
imum value of its input arguments. Name this function max4a. This function
should not all any other functions.

3.7 Exercise 7:
Write a MATLAB program to change the value of the x variable
• If the value of x is greater than 1, set x to 10
• If the above condition has not been satisfied, set x to the absolute value
of x.

3.8 Exercise 8
Write a MATLAB program to convert a distance with units of either kilometers,
meters, centimeters, or milimeters into meters.

3.9 Exercise 9
Suppose the Random Bank now offers 9 percent interest on balances of less
than 5000$, 12 percent for balances of 5000$ or more but less than 10 000$, and
15 percent for balances of 10 000$. You must to write a MATLAB program
to calculate customer’s new balance after one year. According to customer’s
balance obtained to randomize: bal = 15000*rand;

3.10 Exercise 10
Write a script which inputs a number of minutes and seconds and converts it
to hours, minutes and seconds. Try out your script on 10000 seconds, which
should convert to 2 hours 46 minutes and 40 seconds.
Hint: To convert the variable mins minutes into hours and minutes you would
use fix(mins/60) to find the whole number of hours and rem(mins, 60) to find
the number of minutes left over.

9
Ton Duc Thang University
Faculty of Information Technology

3.11 Exercise 11
Write a MATLAB program to produce the numbers of a Fibonacci series whose
values are less n, where n is entered by user. By definition, the first two num-
bers in a Fibonacci series are 0 and 1, and each subsequent number is the sum
of the previous two. In methematical terms, the series Fn of Fibonacci numbers
is defined by equation

Fn = Fn−1 + Fn−2
The first seven numbers of a Fibonacci series are 0, 1, 1, 2, 3, 5, 8.

3.12 Exercise 12
Write a MATLAB program that produces the prim numbers that are less than
n. Where n is entered by user. A prime number is defined as an integer positive
number that is greater than 1 and has exactly two divisors. For example, the
number 2 has only two divisors (1, 2); hence it is a prime number. The number
4 has three divisors (1, 2, 4); hence it is not a prime number.

3.13 Exercise 13
Write a MATLAB function that calculates the least common multiple of two
numbers. This function has two scalar arguments and returns one scalar vari-
able. The operation of two numbers is similar to the existing MATLAB lcm
function in your code.

Hint: You can calculate the least common multiple of two numbers as follows:
Step 1: Find the prime factors of the first number
Step 2: Find the prime factors of the second number
Step 3: Find the common factors of the two numbers
Step 4: Find the rest of the factors of the two numbers.
Step 5: Multiply the common factors by the rest of the factors. This produces
the least common multiple.

3.14 Exercise 14
Write a MATLAB program for computing the members of the sequence xn =
an /n!. The program displays every member xn computed. Adjust it to display
only every 10th value of xn .Hint: The expression rem(n, 10) will be zero when
n is an exact multiple of 10. Use this in an if statement to display every tenth
value of xn

4 Reference
1. Linear Algebra Laboratory, National University of Singapore

10
Ton Duc Thang University
Faculty of Information Technology

2. MIT Laboratory , Danilo Šćepanovi, IPA 2010.


3. Matlab by Example: Programming Basics 1st Edition, Munther Gdeisat
and Francis Lilley, Elsevier, 2014.

4. Essential Essential MATLAB for Engineers and Scientists 3rd Edition,Brian


Hahn and Daniel Valentine, 2007.

11

You might also like