Call Now: (800) 537-1660  
The Algebra Buster
The Algebra Buster


May 24th









May 24th

INTRODUCTION TO MATLAB

Chapter 6
Vector Products, Dot and Cross

Matlab will do dot and cross products for you with the commands dot and cross, like this:
a=[1,2,3],
b=[3,2,1],
dot(a,b)
cross(a,b)

(Cross products only work for three-dimensional vectors but dot products can be used with
vectors of any length.)

Chapter 7
Linear Algebra

Almost anything you learned about in your linear algebra class Matlab has a command to
do. Here is a brief summary of the most useful ones for physics .

7.1 Solve a Linear System
Matlab will solve the matrix equation Ax = b, where A is a square matrix, where b is a
known column vector, and where x is an unknown column vector. For instance, the system
of equations

which is solved by (x, y, z) = (1, 2, 3), is handled in Matlab by defining a matrix A corresponding
to the coefficients on the left side of this equation and a column vector b corresponding
to the coefficients on the right (see the example below.) By use of the simple
symbol \ (sort of "backwards divide") Matlab will use Gaussian elimination to solve this
system of equations, like this:

% here are A and b corresponding to the equations above

A=[ 1, 0,1
-1, 1,1
1,-1,1 ],
b=[4
4
2],

% now solve for x and see if we obtain [1,2,3], like we should

x=A\b

7.2 Max and Min
The commands max and min return the maximum and minimum values of an array . And
with a slight change of syntax they will also return the indices in the array at which the
maximum and minimum occur.

Example 7.2a (ch7ex2a.m)

% Example 7.2a (Physics 330)
clear, close all,

x=0:.01:5,
y=x.*exp(-x.^2),

% take a look at the function so we know what it looks like
plot(x,y)
% find the max and min

ymin=min(y)
ymax=max(y)

% find the max and min along with the array indices imax and imin
% where they occur

[ymin,imin]=min(y)
[ymax,imax]=max(y)

7.3 Matrix Inverse
The inv command will compute the inverse of a square matrix. For instance, using the
matrix
A=[1,0,-1,-1,1,1,1,-1,1]

we find
% load C with the inverse of A
C=inv(A)
% verify by matrix multiplication that A*C is the identity matrix
A*C

7.4 Transpose and Hermitian Conjugate
To find the transpose of the matrix A just use a single quote with a period, like this
A.'

To find the Hermitian conjugate of the matrix A (transpose of A with all elements replaced
with their complex conjugates) type
A'

(notice that there isn't a period). If your matrices are real, then there is no difference
between these two commands and you might as well just use A'. Notice that if a is a row
vector then a' is a column vector. You will use the transpose operator to switch between
row and column vectors a lot in Matlab, like this
[1,2,3]
[1,2,3]'
[4,5,6]
[4,5,6]'

7.5 Special Matrices
Matlab will let you load several special matrices. The most useful of these are given here.

% eye:
% load I with the 4x4 identity matrix (the programmer who invented this
% syntax must have been drunk)

I=eye(4,4)

% zeros:
% load Z with a 5x5 matrix full of zeros

Z=zeros(5,5)

% ones:
% load X with a 3x3 matrix full of ones

X=ones(3,3)

% rand:
% load Y with a 4x6 matrix full of random numbers between 0 and 1
% The random numbers are uniformly distributed on [0,1]

Y=rand(4,6)
% And to load a single random number just use
r=rand

% randn:
% load Y with a 4x6 matrix full of random numbers with a Gaussian
% distribution with zero mean and a variance of 1

Y=randn(4,6)

7.6 Determinant
Find the de terminant of a square matrix this way
det(A)

7.7 Norm of Vector (Magnitude)
Matlab will compute the magnitude of a vector a (the square root of the sum of the squares
of its components) with the norm command
a=[1,2,3]
norm(a)

7.8 Sum the Elements
For arrays the command sum adds up the elements of the array. For instance, the following
commands calculate the sum of the squares of the reciprocals of the integers from 1 to
10,000.
n=1:10000,
sum(1./n.^2)

You can compare this answer with the sum to infinity, which is π2/6, by typing
ans-pi^2/6

For matrices the sum command produces a row vector which is made up of the sum of
the columns of the matrix.
A=[1,2,3,4,5,6,7,8,9]
sum(A)

7.9 Selecting Rows and Columns
Sometimes you will want to select a row or a column of a matrix and load it into an array.
This is done with Matlab's all-purpose colon (:) command.

To load a column vector b with the contents of the third column of the matrix A use:
b=A(:,3)

Recall that the first index of a matrix is the row index, so this command tells Matlab to
select all of the rows of A in column 3.

To load a row vector c with the contents of the second row of the matrix A use:
c=A(2,:)

You can also select just part of row or column like this:
c=A(2,1:2)

which takes only the first two elements of the second row.

7.10 Eigenvalues and Eigenvectors
To build a column vector containing the eigenvalues of the matrix A in the previous section
use
E=eig(A)

To build a matrix V whose columns are the eigenvectors of the matrix A and another matrix
D whose diagonal elements are the eigenvalues corresponding to the eigenvectors in V use
[V,D]=eig(A)

To select the 3rd eigenvector and load it into a column vector use
v3=V(:,3) % i.e., select all of the rows (:) in column 3

7.11 Fancy Stu
Matlab also knows how to do singular value decomposition, QR factorization , LU factorization,
and conversion to reduced row-echelon form. And the commands rcond and cond will
give you the condition number of a matrix. To learn about these ideas, consult a textbook
on linear algebra. To learn how they are used in Matlab use the commands,
help svd
help QR
help LU
help rref
help rcond
help cond

Chapter 8
Polynomials

Polynomials are used so commonly in computation that Matlab has special commands to
deal with them. The polynomial x4+2x3-13x2-14x+24 is represented in Matlab by the
array [1,2,-13,-14,24], i.e., by the coefficients of the polynomial starting with the highest
power and ending with the constant term. If any power is missing from the polynomial its
coefficient must appear in the array as a zero. Here are some of the things Matlab can do
with polynomials. Try each piece of code in Matlab and see what it does.

8.1 Roots of a Polynomial
The following command will find the roots of a polynomial:
p=[1,2,-13,-14,24],
r=roots(p)

8.2 Find the polynomial from the roots
If you know that the roots of a polynomial are 1, 2, and 3, then you can find the polynomial
in Matlab's array form this way
r=[1,2,3],
p=poly(r)

8.3 Multiply Polynomials
The command conv returns the coefficient array of the product of two polynomials.
a=[1,0,1],
b=[1,0,-1],
c=conv(a,b)

Stare at this result and make sure that it is correct.

8.4 Divide Polynomials
Remember synthetic division? Matlab can do it with the command deconv, giving you the
quotient and the remainder.
a=[1,1,1], % a=x^2+x+1
b=[1,1], % b=x+1

% now divide b into a finding the quotient and remainder

[q,r]=deconv(a,b)

After you do this Matlab will give you q=[1,0] and r=[0,0,1]. This means that q =
x + 0 = x and r = 0x2 + 0x + 1 = 1, so

8.5 First Derivative
Matlab can take a polynomial array and return the polynomial array of its derivative:
a=[1,1,1,1]
ap=polyder(a)

8.6 Evaluate a Polynomial
If you have an array of x-values and you want to evaluate a polynomial at each one, do this:
% define the polynomial
a=[1,2,-13,-14,24],
% load the x-values
x=-5:.01:5,
% evaluate the polynomial
y=polyval(a,x),
% plot it
plot(x,y)

8.7 Fitting Data to a Polynomial
If you have some data in the form of arrays (x, y), Matlab will do a least-squares t of a
polynomial of any order you choose to this data. In this example we will let the data be
the sine function between 0 and and we will t a polynomial of order 4 to it. Then we
will plot the two functions on the same frame to see if the t is any good. Before going on
to the next section, try fitting a polynomial of order 60 to the data to see why you need to
be careful when you do fits like this.

Example 8.7a (ch8ex7a.m)

% Example 8.7a (Physics 330)
clear, close all,
x=linspace(0,pi,50),
% make a sine function with 1% random error on it
f=sin(x)+.01*rand(1,length(x)),
% fit to the data
p=polyfit(x,f,4),
% evaluate the fit
g=polyval(p,x),
% plot fit and data together
plot(x,f,'r*',x,g,'b-')

Prev Next
 
Home    Why Algebra Buster?    Guarantee    Testimonials    Ordering    FAQ    About Us
What's new?    Resources    Animated demo    Algebra lessons    Bibliography of     textbooks
 

Copyright © 2009, algebra-online.com. All rights reserved.