Matlab WS1
Matlab WS1
A. Installation of MATLAB
(i) Go to https://www.mathworks.com/mwaccount/register.
(ii) Create the account using your NUS email address (e.g., [email protected])
(iii) Fill in the rest of the information and follow the instructions to complete the
registration process.
2. Connecting to nVPN
(i) If you are NOT using NUS network, you are required to connect to nVPN at
the URL: https://webvpn.nus.edu.sg/
(ii) Sign in with your NUSNET ID in the format: nusstu\nusnetid and pass-
word.
(iii) If you are using NUS network, you may ignore this and proceed to download
MATLAB.
3. Download MATLAB
(i) As you are allowed to download MATLAB on only one device, you are advised
to do so on the PC that you are likely to use during the exam.
(ii) Go to
https://nusit.nus.edu.sg/services/software_and_os/software/software-student
and follow the instructions to download MATLAB.
(iii) It is required to sign in with your NUSNET ID in the format: nusstu\nusnetid
and password.
(iv) Choose an installation folder in your PC.
(v) Select products to install, include MATLAB and Symbolic Math Toolbox.
(vi) If you encounter any installation problem, contact NUS IT Care at [email protected]
or 65162080.
1
B. MATLAB Commands
MATLAB environment behaves like a super-complex calculator. You can type the
commands at the >> command prompt. The answer appears by pressing Enter.
(i) You can type any valid expression at the command prompt. The expression
can be a number, a matrix, an operation and so on.
>> 5
ans = 5
>> 3 + 5
ans = 8
>> [1 2 3; 4 5 6]
ans = 1 2 3
ans = 4 5 6
( )
1 2 3
representing the matrix .
4 5 6
⋆ Note that, to input a matrix, use square brackets [ ] instead of round
brackets. Within the square brackets, type the entries of the matrix row by row,
separating each entry on the same row by a space, and separating every row by
a semicolon ; .
2
(iii) You may add a semicolon ; at the end of a command. Then MATLAB will
hide the output. For example,
>> a = 4; b = 7;
>> a + b
ans = 11
>> c = a * b
c = 28
C. Output Formats
(iii) MATLAB can also display the answers as rational number (approximation):
>> format rat
>> sqrt(2)
ans = 1393/985
>> a
a = 1/2
⋆ MATLAB will approximate irrational numbers with rational numbers when
you use format rat . Sometimes, this may cause unexpected results. Occa-
sionally, an asterisk ∗ may appear when you expect the quantity to be 0.
(iv) To change the display to four decimal digits, you can type format short or
simply format :
>> format short
>> sqrt(2)
ans = 1.4142
>> a
a = 0.5000
3
D. Solving Linear System
1 −5 −7 6 2 −7
4
of the reduced row-echelon form are pivot columns. Set x3 = s and x4 = t to be
arbitrary parameters, and solve the other variables:
x1 = 2s − t + 1,
x2 = −s + t + 2, x5 = 1.
x1 2s − t + 1
x2 −s + t + 2
This gives us the general solution
x3 = s .
x4 t
x5 1
4 5 6 7 8
>> A = [1 2 3 4 5; 2 3 4 5 6; 3 4 5 6 7; 4 5 6 7 8]
ans = 1 2 3 4 5
ans = 2 3 4 5 6
ans = 3 4 5 6 7
ans = 4 5 6 7 8
The ith row of A can be extracted using the command A(i,:) . For example, to
get the 4th row of A:
>> A(4,:)
ans = 4 5 6 7 8
5
A = 3 4 5 6 7
A = 2 3 4 5 6
A = 4 5 6 7 8
Note that MATLAB returns directly the matrix A with its 2nd and 3rd rows
interchanged under this elementary row operation.
(iii) Adding c times of the j th row to the ith row: A(i,:) = A(i,:) + c*A(j,:) .
>> A(4,:) = A(4,:) + 2*A(1,:)
A = -2 -4 -6 -8 -10
A = 3 4 5 6 7
A = 2 3 4 5 6
A = 40 -3 -6 -9 -12
Note that MATLAB returns directly the matrix A with its 4th row changed under
this elementary row operation.
F. Practices
1. Enter the following commands in MATLAB window and observe the outputs.
Describe what MATLAB has done.
>> x = [1 2 3]
>> y = [1; 2; 3]
>> A = [1 2 pi; 0.1 5 6; 7 8 1/4]
>> format rat
>> A
>> format short
>> A
>> format long
>> A
>> format
>> A
>> [A x]
>> [A y]
2. For each of the linear systems in Question 1.16 in the textbook Exercise 1, Use
rref to find its general solution (if any).