Professional Documents
Culture Documents
2 1
A= 5 4
7 9
Example: g=9.81
Indexing (or Subscripting)
A(i,j) in MATLAB refers to the element aij of matrix A, i.e. the element in the ith row and jth
column.
A(m:n,k:l) specifies rows m to n and columns k to l of matrix A.
A(:,5:20) refers to the elements in column 5 through 20 of all the rows of the matrix A.
Example:
>> A=[1 2 3;4 5 6;7 8 9]
A=
1 2 3
4 5 6
7 8 9
>> A(2,3)
ans =
6
>> A(3,3)=10
A=
1 2 3
4 5 6
7 8 10
>> B=A(2:3,1:3)
B=
4 5 6
7 8 10
>> B=A(2:3,:)
B=
4 5 6
7 8 10
>> B(:,2)=[]
B=
4 6
7 10
Transpose
The transpose of a matrix A is obtained by typing A.
>> A'
ans =
1 4
2 5
3 6
7
8
10
>> u=1:2:9
% This tell MATLAB to create a vector of values running from 1 to 9 in
increments of 2.
u=
1 3 5 7 9
>> v=u(2:4)'
v=
3
5
7
Inverse
The inverse of a square matrix C is obtained by typing inv(C) in the command window.
>> C=[2 4 6;10 15 21;0 3 1];
>> D=inv(C)
D=
-1.0909 0.3182 -0.1364
-0.2273 0.0455 0.4091
0.6818 -0.1364 -0.2273
>> I=C*D
I=
1.0000 0.0000
0
-0.0000 1.0000 -0.0000
0 0.0000 1.0000
Concatenation
Concatenation is the process of joining small matrices to make bigger ones.
>> (x+y)*(x+y)^2
ans =
(x+y)^3
>> expand(ans)
ans =
x^3+3*x^2*y+3*x*y^2+y^3
>> factor(ans)
ans =
(x+y)^3
>> simplify((x^2-y^2)/(x-y))
ans =
x+y
We will try later solve and dsolve math tools for symbolic solutions of algebraic and
differential equations.
GRAPHING WITH MATLAB
>> help plot % read the documentation
graph the function y = x3 over the x domain -1 to 1. The first step is to generate the data to
plot.
>> x=-1:.1:1; % Define the range of x