Sei sulla pagina 1di 11

IM 512 3.

0 (I) Mathematical Software

Dr. TGI Fernando

September 24, 2011

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 1 / 11
Directional Exploration Working with Arrays and Matrices

Working with Arrays and Matrices

Arrays are the backbone of Matlab computation.


An array with m rows and n columns is called an m×n array and it
has a total of m·n entries.
An element of an array is recognized by its location - its row number
and column number.
These row and column identifications are called indices (singular -
index) of the matrix.
A(i,j) - element located in the ith row and jth column.

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 2 / 11
Directional Exploration Working with Arrays and Matrices

Working with Arrays and Matrices (Cont.)

Type the following commands.


A = [1 2 3; 4 5 6; 7 8 9] (Matrices are entered row-wise. Rows are
separated by semicolons and columns are separated by spaces or
commas.)
A(2,3) (Element Aij of matrix A is accessed as A(i,j))
A(2,3) = 10 (Correcting an array element)
B = A(2:3, 1:3) (Any submatrix of A is obtained by using range
specifiers for row and column indices)
B = A(2:3, :) (The colon by itself as a row or column index specifies
all rows or columns of the matrix)
B(:,2) = [] (A row or a column of a matrix is deleted by setting it to
a null vector [])

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 3 / 11
Directional Exploration Working with Arrays and Matrices

Working with Arrays and Matrices (Cont.)

Matrices are transposed using the single right-quote character (’).


A = [1 2 3; 4 5 6; 7 8 9]
B = A’
Here B is the transpose of A.
A ˆ 2 (Exponentiate a square matrix. Aˆ2 is simply A*A)
A . ˆ 2 (When a dot precedes the arithmetic operators *,ˆand /,
Matlab performs array operation (element-by-element operation).
Exercises 6

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 4 / 11
Directional Exploration Working with Anonymous Functions

Working with Anonymous Functions

An anonymous function is a function of one or more variables that you


create on the command line for subsequent evaluation.
These functions are especially useful if you need to evaluate the function
several times (with different input) during a single Matlab session.
Syntax: fn name = @(list of input variables) function expression
Here, the symbol @ assigns a function handle to the defined function. A
function handle is a name given to a function by which you can call it
whenever you need it.
fn name - name of the function handle
E.g. f = @(x) xˆ2 - 1

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 5 / 11
Directional Exploration Working with Anonymous Functions

Working with Anonymous Functions (Cont.)

Implement the following commands.


f = @(x) x ˆ 3 - 3*x ˆ 2 + x*log(x-1) + 100 (Create a function
f (x) = x 3 − 3x 2 + xlox(x − 1) − 100)
f(0) (Evaluate the function at x = 0, i.e. find f (0))
f(1) (Evaluate the function at x = 1. Note that f is singular at
x = 1).
values = [f(0) f(1) f(2) f(3)] (You can use f in an array also).
x = [0 1 2 10];
f(x) (Using an array as the input to f causes an error. This is
because the expression for f is not vectorized)
f = @(x) x . ˆ 3 - 3*x . ˆ 2 + x.*log(x-1) + 100 (Redefining f by
vectorizing the expression).
f(x) (Now use it with an array argument).

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 6 / 11
Directional Exploration Working with Anonymous Functions

Working with Anonymous Functions (Cont.)

x = linspace(-10, 10);
plot(x, f(x)) (You can also use f as input to other functions where
appropriate).
f = @(mu, x) mu*x - x . ˆ 3; (Define a function of two variables
f (µ, x) = µx − x 3 and evaluate over a range of x for different values
of µ).
x = linspace(-5, 5)’;
f many = [f(-5,x) f(0,x) f(5,x) f(45,x)]
plot(x, f many) (Output)
grid
xlabel(’x’)
ylabel(’f(\mu,x) = \mu x - x ˆ 3’)

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 7 / 11
Directional Exploration Working with Anonymous Functions

Working with Anonymous Functions (Cont.)

g = @(a,x) a(4)*x ˆ 3 + a(3)*x ˆ 2 + a(2)*x + a(1);


a = [1 2 -1 0];
g(a,5) (Define a function g(a,x) of two variables and use a, one of
the variables, as an array of parameters.)
Here a = [a1 a2 a3 a4 ] and g (x) = a4 x 3 + a3 x 2 + a2 x + a1 . This is
useful for all polynomial type of functions.
f = @(x) x . ˆ 2 - 1; (Remember to vectorize the function)
R1
f integral = quad(f, 0, 1) (Here we compute 0 (x 2 − 1)dx)
Exercises 7

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 8 / 11
Directional Exploration Symbolic Computation

Symbolic Computation

Let’s learn how to do simple symbolic algebra in Matlab. Such calculations


are done with symbolic variables without resorting to their numerical
values.
Method: Suppose you want to use x and y as symbolic variables. Then
you can declare them to be symbolic using any of the following
commands.
1 x = sym(’x’); y=sym(’y’)
2 syms x y
There are three basic skills to learn in symbolic computations:
1 How to define expressions and do simple algebra with them (multiply,
divide, expand, factorize, simplify, etc.).
2 How to do simple calculus (differentiate, integrate, etc.)
3 How to solve equations (algebraic and differential).

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 9 / 11
Directional Exploration Symbolic Computation

Symbolic Computation (Cont.)

Implement the following commands.


syms x y (Define x and y to be symbolic variables).
f = (x+y) ˆ 3 (Define a function f as f (x, y ) = (x + y )3 ).
expand(f) (Use ‘expand’ to multiply out and expand algebraic or
trigonometric expressions).
factor(ans) (Use ‘factor’ to find factors of long algebraic expressions).
z = sin(x+y) (Define a trigonometric expression)
expand(z)
subs(z, y, pi-x) (Substitute y = π − x in expression z).
∂z
diff(z,x) (Differentiate z w.r.t. x. i.e. find ∂x ).
∂2z
diff(z,x,2) (Find the second derivative of z w.r.t. x. i.e. ∂x 2
).
R π/2
int(z,x,0,pi/2) (Integrate z w.r.t. x from 0 to π/2. i.e. 0 zdx).

Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 10 / 11
Directional Exploration Symbolic Computation

Symbolic Computation (Cont.)


v = [x;y]; (Define a column vector)
inner product = v‘*v (Find the inner product.)
Here Matlab assumes x and y are complex (default).
syms x y real (now x and y are assumed to be real).
inner product = v‘*v
Solving two simultaneous equations for x and y
syms a b
exp1 = ‘a*x + b*y - 3’;
exp2 = ‘-x + 2*a*y - 5’;
[x, y ] = solve(exp1, exp2)
subs(exp1) (Substitute the values of x and y just found in ‘exp1’ to
check the result).
simplify(ans) (Simplify the answer)
pretty(subs(exp2)) (Use ‘pretty’ to get the expression in more
readable form).
Exercises 8
Dr. TGI Fernando () IM 512 3.0 (I) Mathematical Software September 24, 2011 11 / 11

Potrebbero piacerti anche