Professional Documents
Culture Documents
Linear Algebra
Module code: 501032
1 Objective
In this Lab you are going to study about:
• Variables
• 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.
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.
• 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.
a =
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
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
4
Ton Duc Thang University
Faculty of Information Technology
ans =
0.6667
ans =
1.0000 + 3.0000 i
2. Exponentiation (ˆ)
>> 4ˆ2
ans =
16
>> (3+4∗5 i ) ˆ 2
ans =
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
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
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
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
11