You are on page 1of 70

Lecture 6

• More of MatLab functions


• Rem(x,y) means the remainder of x
divided by y.
• Example
• >> rem(10,3)
• ans = 1
• >> rem(100,3);x
• Undefined function or variable 'x'.
• >> rem(100,3)
• ans = 1
• You can also assign a variable to the
evaluation.
• x=rem(100,3)
• x= 1
• To find the factors of a number, use
‘factor’ e.g.
• factor(36)
• ans = 2 2 3 3
• >> factor(100)
• ans = 2 2 5 5
• > x = factor(24)
• x= 2 2 2 3
• Format: The Way in Which Numbers
• Appear
• Consider the following codes
• s = [1/2 1/3 pi sqrt(2)];
• format short; s
• format long; s
• format rat; s
• format ; s
• >> s = [1/2 1/3 pi sqrt(2)];
• >> format long, s
• s = 0.500000000000000,
0.333333333333333
3.141592653589793
1.414213562373095
• >> format rat, s
• s= 1/2 1/3 355/113
1393/985
• >> format; s
• s = 0.5000 0.3333 3.1416 1.4142
• Some MatLab specific command.
3 2
• Evaluate the cubic y =𝑥 + 3𝑥 − 𝑥 −
1 at the point x = [1 2 3 4 5 6].
• We find the solution to this code as a
commented code.
• >> %firstly set up the points at which
the polynomial will be evaluated
• >> x = 1:6;
• >> % enter the coefficients of the cubic
equation and note that
• >> % these are entered starting with
the coefficients of the
• >> % of the highest power first
• >> c = [1 3 -1 -1];
• >> % now perform the evalution using
polyval
• >> y = polyval(c,x)
• y = 2 17 50 107 194 317
• It is always a good practice to provide
brief, but meaningful, comments at
important points within your code
• Plot the polynomial y = 𝑥 4 + 𝑥 2 − 1
between x = −2 and x = 2 (using fifty
points).
• >> x = linspace(-2,2,50);
• >> c = [1 0 1 0 -1];
• >> y = polyval(c,x);
• >> plot(x,y)
• Find the roots of the polynomial 𝑦 =
𝑥 3 − 3𝑥 2 + 2𝑥 using the command
roots
• c = [1 -3 2 0];
• r = roots(c)
• r =0
• 2
• 1
• The solve function is used for solving
algebraic equations.
• Solve for x in the equation x-5 =0
• y = solve(‘x-5=0’)
• MATLAB will execute some statements
and return
• y=5
• The solve function takes the equation
enclosed in quotes as an argument
• If the equation involves multiple
symbols, then MATLAB by default
assumes that you are solving for
• x, except you specify the one you are
solving.
• Example
• solve(equation, variable)
• solve for v in the equation v – u – 3t2 =
0,
• >> >> solve('v-u-3*t^2=0','v')
• ans = 3*t^2 + u
• Solve for t.
• >> solve('v-u-3*t^2=0','t')
• -(3^(1/2)*(v - u)^(1/2))/3
• (3^(1/2)*(v - u)^(1/2))/3
• We can still solve the equations in
octave using the roots function.
• >> y= roots([1,-5])
• y=
• 5
• >> c =[1 -5];
• >> roots(c)
• ans =
• 5
• solve the fourth order equation x4 − 7x3
+ 3x2 − 5x + 9 = 0.
• >> c=[1 -7 3 -5 9];
• >> s=roots(c)
• s=
• 6.6304 + 0.0000i
• 1.0598 + 0.0000i
• -0.3451 + 1.0778i
• -0.3451 - 1.0778i
• >> eq = 'x^4 - 7*x^3 + 3*x^2 -5*x +9 =
0';
• >> r = solve(eq)
• r=
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 1)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 2)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 3)
• root(z^4 - 7*z^3 + 3*z^2 - 5*z + 9, z, 4)
• Matrices and Arrays.
• MATLAB is an abbreviation for "matrix
laboratory.“
• Other programming languages mostly work with
numbers one at a time but MATLAB is designed
to operate primarily on whole matrices and
arrays.
• MATLAB variables are multidimensional arrays,
no matter what type of data.
• A matrix is a two-dimensional array often used
for linear algebra.
• Array Creation
• To create an array with four elements in a single
row, separate the elements with either
• a comma (,) or a space. e.g.
• b=[1 2 3 4 5 6]
• b=
• 1 2 3 4 5 6
• or b=[1,2,3,4,5,6]
• b=
• 1 2 3 4 5 6
• This type of array is a row vector.
• To create a matrix that has multiple rows,
separate the rows with semicolons. It also
automatically arranges them in rows and
columns
• a = [1 2 3; 4 5 6; 7 8 10]
• a=
• 123
• 456
• 7 8 10
• Another way to create a matrix is to use a
function, such as ones, zeros, or rand. For
• example, create a 5-by-1 column vector of zeros.
• z = zeros(5,1)
• z=
• 0
• 0
• 0
• 0
• 0
• >> ones(5,1)
• ans =
• 1
• 1
• 1
• 1
• 1
• > ones(1,5)
• ans =
• 1 1 1 1 1
• >> rand(2)
• ans =
• 0.8147 0.1270
• 0.9058 0.9134
• >> rand(1)
• ans =
• 0.6324
• >> rand(2,3)
• ans =
• 0.0975 0.5469 0.9649
• 0.2785 0.9575 0.1576
• MATLAB allows you to process all of the values in a
matrix using a single arithmetic operator or
function. e.g.
• >> A=[1,2,3;4,5,6;7,8,10]
• A=
• 1 2 3
• 4 5 6
• 7 8 10
• >> C=A+10
• C=
• 11 12 13
• 14 15 16
• 17 18 20
• D=sin(A)
• D=
• 0.8415 0.9093 0.1411
• -0.7568 -0.9589 -0.2794
• 0.6570 0.9894 -0.5440
• To transpose a matrix, use a single quote ('): e.g.
• >> C'
• ans =
• 11 14 17
• 12 15 18
• 13 16 20
• D=sin(A)
• D=
• 0.8415 0.9093 0.1411
• -0.7568 -0.9589 -0.2794
• 0.6570 0.9894 -0.5440
• To transpose a matrix, use a single quote ('): e.g.
• >> C'
• ans =
• 11 14 17
• 12 15 18
• 13 16 20
• You can perform standard matrix multiplication,
which computes the inner products between
rows and columns, using the * operator. e.g.
• >> A*inv(A)
• ans =
• 1.0000 0 -0.0000
• 0 1.0000 0
• 0 0 1.0000
• (The identity matrix).
• >> format long
• >> Z=A*inv(A)
• Z=
• 1.000000000000000 0 -0.000000000000000
• 0 1.000000000000000 0
• 0 0 0.999999999999998
• >> format short
• >> Z=A*inv(A)
• Z=
• 1.0000 0 -0.0000
• 0 1.0000 0
• 0 0 1.0000
• To perform element-wise multiplication rather
than matrix multiplication, use the .* operator:
• e.g.
• >> Z=A.*A
• Z=
• 1 4 9
• 16 25 36
• 49 64 100
• The matrix operators for multiplication, division, and
power each have a corresponding array operator
that operates element-wise. e.g.
• >> A.^2
• ans =
• 1 4 9
• 16 25 36
• 49 64 100
• >> A./2
• ans =
• 0.5000 1.0000 1.5000
• 2.0000 2.5000 3.0000
• 3.5000 4.0000 5.0000
• Internalize the differences between the
point-wise and regular versions of the
operators by examining the results of the
following expressions that use the variables
A=[1 2; 3 4], B=[1 0; 0 2], and C=[3;4]. Note:
some commands may result in an error
message. Understand what the error is and
why it was given
• When the variables are written on the
command prompt, the following results are
given
• >> A=([1 2; 3 4]),B=([1 0; 0 2]),C=([3;4])
• A=
• 1 2
• 3 4
• B=
• 1 0
• 0 2
• C=
• 3
• 4
• A*B vs. A.*B vs. B.*A vs. B*A
• 2*A vs. 2.*A
• A^2 vs. A*A vs. A.*A vs. A.^2 vs. 2.^A
vs. A^A vs. 2^A. The last one here might
be difficult to understand…it is matrix
exponentiation.
• A/B vs. A\B vs. A./B vs. A.\B
• A*C vs. A*C' vs. C*A vs. C'*A
• A\C vs. A\C' vs. C/A vs. C'/A
• >> A*B
• ans =
• 1 4
• 3 8
• >> A.*B
• ans =
• 1 0
• 0 8
• >> B.*A
• ans =
• 1 0
• 0 8
• >> B*A
• ans =
• 1 2
• 6 8
• 2*A
• ans =
• 2 4
• 6 8
• 2.*A
• ans =
• 2 4
• 6 8
• A^2
• ans =
• 7 10
• 15 22
• >> A*A
• ans =
• 7 10
• 15 22
• A.*A
• ans =
• 1 4
• 9 16
• >> A.^2
• ans =
• 1 4
• 9 16
• >> 2.^A
• ans =
• 2 4
• 8 16
• >> A^A
• ??? Error using ==> mpower
• At least one operand must be scalar.
• >> 2^A (it is matrix exponentiation)
• ans =
• 10.4827 14.1519
• 21.2278 31.7106
• >> A/B
• ans =
• 1 1
• 3 2
• >> A\B
• ans =
• -2.0000 2.0000
• 1.5000 -1.0000
• When creating a matrix, a space or a
comma (,) are the separator between
columns, while a semicolon (;) separate
between rows. Figure out how to create
the following matrices: (1 4 2 5 3 6 ) ,
(1 0 0 1 1 0 )
• You can nest matrix construction so
that [ 6 (1:5) 7 ] makes sense (what
does it result in?)
• >> [6 (1:5) 7]
• ans =
• 6 1 2 3 4 5 7
• Concatenation
• This is the process of joining matrices to make
larger ones. The pair of square brackets [] is the
concatenation operator. e.g.
• >> G=[A,A]
• G=
• 1 2 3 1 2 3
• 4 5 6 4 5 6
• 7 8 10 7 8 10
• This is a 3X6 matrix
• >> D=[A;A]
• D=
• 1 2 3
• 4 5 6
• 7 8 10
• 1 2 3
• 4 5 6
• 7 8 10
• This is a 6X3 matrix.
• Complex Numbers
• Complex numbers have both real and imaginary
parts, where the imaginary unit is the
• square root of -1.
• >> sqrt(-1)
• ans =
• 0 + 1.0000i
• To represent the imaginary part of complex
numbers, use either i or j . e.g.
• >> c = [3+4i, 4+3j; -i, 10j]
• c=
• 3.0000 + 4.0000i 4.0000 + 3.0000i
• 0 - 1.0000i 0 +10.0000i
• Array Indexing
• Every variable in MATLAB is an array that can hold
many numbers. When you want to
• access selected elements of an array, use indexing.
e.g.
• consider the 4-by-4 magic square A:
• A = magic(4)
• A=
• 16 2 3 13
• 5 11 10 8
• 9 7 6 12
• 4 14 15 1
• There are two ways to refer to a particular element
in an array. The most common way is
• to specify row and column subscripts, such as:
• A(4,2)
• ans =
• 14
• Less common, but sometimes useful, is to use a
single subscript that traverses down each
• column in order:
• A(8)
• ans =
• 14
• Using a single subscript to refer to a particular
element in an array is called linear
• indexing.
• If you try to refer to elements outside an array on
the right side of an assignment statement,
MATLAB throws an error. E.g
• test = A(4,5)
• Index exceeds matrix dimensions.
• However, on the left side of an assignment
statement, you can specify elements outside
• the current dimensions. The size of the array
increases to accommodate the newcomers. e.g.
• A(4,5) = 17
• A=
• 16 2 3 13 0
• 5 11 10 8 0
• 9 7 6 12 0
• 4 14 15 1 17
• >> A=magic(4)
• A=
• 16 2 3 13
• 5 11 10 8
• 9 7 6 12
• 4 14 15 1
• >> A(1:4,3) This refers to all the 4 elements in the
3rd column.
• ans =
• 3
• 10
• 6
• 15
• The colon alone, without start or end values, specifies all of the
elements in that dimension. >> A(:)
• ans =
• 16
• 5
• 9
• 4
• 2
• 11
• 7
• 14
• 3
• 10
• 6
• 15
• 13
• 8
• 12
• 1
• Or you could select all the columns in the third
row of A:
• >> A(3, :)
• ans =
• 9 7 6 12
• The colon operator also allows you to create an
equally spaced vector of values using the more
general form start:step:end.
• Example
• B = 0:10:100
• B=
• 0 10 20 30 40 50 60 70 80 90 100
• If you omit the middle step, as in start:end,
MATLAB uses the default step value of 1.
• Workspace Variables
• The workspace contains variables that you create
within or import into MATLAB from data files or
other programs. For example, these statements
create variables A in the workspace.
• >> A=magic(4);
• You can view the contents of the workspace
using the command.
• >> whos
• Name Size Bytes Class Attributes
• A 4x4 128 double
• Solve the following system of equations
• x + 2y + 3z = 1
• 3x + 3y + 4z = 1
• 2x + 3y + 3z = 2
• Solution
• >> A = [1 2 3;3 3 4;2 3 3];
• >> b = [1;1;2];
• >> X = A\b
• X = -0.5000
• 1.5000
• -0.5000
Solve the equations
• 2x + 3y = 7,
• x − y = 1,
• and
• 2x + 3y = −2,
• x − y = 8.
The equations can be written as
2 3 𝑥 7 −2
=
1 −1 𝑦 1 8
• >> A = [2 3;1 -1];
• >> b = [7 -2;1 8];
• >> X = A\b
• X=
• 2.0000 4.4000
• 1.0000 -3.6000
• The Solution to the 1st equation is (2, 1)
and the 2nd is (4.4, -3.6)
Solve the following simultaneous equations
(a) y = x+1 and x+y =3
(b) y = x+1 and x+y = 5
(c) y = x2 and y = x+2
(d) Y =x2 -3x + 4 and y – x = 1
>> x = -4:0.1:4;
>> y1 = x + 1;
>> y2 = 3-x;
>> plot(x,y1,x,y2)
>> xlabel('-4\leqx\leq4')
>> ylabel('Y')
>> grid
>> title('graphical solution of simultaneous
equation')
>> legend('y1=x+1','y2=x-3')
Under edit
Click on figure properties
Click on the each of the lines and effect what ever
changes you desire

You might also like