Friday, October 30, 2009

Matlab polynomial basics: roots, derivative of polynomial, evaluate polynomial



%Get roots of function

octave-3.2.3:13> A = [1 -3 3 -1]
A =

1 -3 3 -1

octave-3.2.3:14> roots(A)
ans =

1.00001 + 0.00000i
1.00000 + 0.00001i
1.00000 - 0.00001i

%get derivative of polynomial

octave-3.2.3:18> A
A =

1 -2 1

octave-3.2.3:19> polyder(A)
ans =

2 -2


%evaluate polynomial using matrix notation

polyval(A,1)
ans = 0
octave-3.2.3:21> polyval(A,2)
ans = 1

Sunday, October 11, 2009

matlab : example program to find extreme points of lp problem



format compact;
A=[2 1 1 1 0 0; 1 -2 1 0 1 0; 1 1 0 0 0 1];

b = [ 10; 8; 3];
c= [-1 1 1 0 0 0];

n = 3;
m = 3;

i = 1;
while i <= n+1
j = i+1;
while j < n+m
k=j+1;
while k <=n+m
B=[A(:,[i j k]) b];
B=rref(B);
temp = [ 0 ; 0 ; 0; 0; 0; 0];
x = B(:,[n+1]);
fprintf(1,'***Is feasible __ ***');
fprintf(1,'i=%d,j=%d,k=%d \n',i,j,k);
temp(i) = x(1);
temp(j) = x(2);
temp(k) = x(3);
temp
c*temp
B=0;
x=0;
k=k+1;

end
j=j+1;
end
i=i+1;
end

matlab basics : matrix norms, condition number




A =

1 0 -2
2 2 2
2 2 4

>> norm(A)

ans =

6.0225

%infinity norm

>> norm(A,'inf')

ans =

8


%condition numbers
>> cond(A)

ans =

19.5212

>> cond(A,'inf')

ans =

36

>> norm(A)*norm(inv(A))

ans =

19.5212

>> norm(A,'inf')*norm(inv(A),'inf')

ans =

36


matlab basics : matrix num elements, row/column operations, lu decomposition




%num of matrix elements
numel(A)

%example of row/col operations for complete pivoting

B =

1 0 -2 1
2 2 2 0
2 2 4 0

%swop rows 1 and 3
>> B([1 3],:) = B([3 1],:)


B =

2 2 4 0
2 2 2 0
1 0 -2 1

%swop columns 1 and 3
>> B( :,[1 3]) = B(:,[3 1])

B =

4 2 2 0
2 2 2 0
-2 0 1 1

% it is now z y x

>> B(2,:) = (-1/2)*B(1,:) + B(2,:)

B =

4 2 2 0
0 1 1 0
-2 0 1 1

>> B(3,:) = (1/2)*B(1,:) + B(3,:)

B =

4 2 2 0
0 1 1 0
0 1 2 1


>> B([2 3],:) = B([3 2],:)

B =

4 2 2 0
0 1 2 1
0 1 1 0

>> B( :,[2 3]) = B(:,[3 2])

B =

4 2 2 0
0 2 1 1
0 1 1 0

%it is now z x y

>> B(3,:) = (-1/2) * B(2,:) + B(3,:)

B =

4.0000 2.0000 2.0000 0
0 2.0000 1.0000 1.0000
0 0 0.5000 -0.5000

>> rref(B)

ans =

1 0 0 0
0 1 0 1
0 0 1 -1

%x = 1
%y = -1
%z = 0



%lu decomposition

[L,U]=lu(some_matrix)

Saturday, October 10, 2009

matlab : sample Gauss Elimination


A = [ 2 1 -1 8; -3 -1 2 -11; -2 1 2 -3]

m = 3
n = 4

i = 1

adds=0 %just counting the additions
mults=0 % just counting the multiplications

while i < m
j = i+1
while j <= m
temp = -(A(j,i)/A(i,i))
k = i
while k <= n
A(j,k) = A(j,k) + A(i,k)*temp
adds=adds+1
mults=mults+1
k=k+1
end
j=j+1
end
i=i+1
end
A

matlab : sample cholesky program


A = [2 4 2; 4 11 4; 2 4 6]

j = 1
n = 3

while j <= n
k = 1
while k < j
i = j
while i <= n
A(i,j) = A(i,j) - A(i,k)*A(j,k)
i=i+1
end
k = k+1
end
A(j,j) = (A(j,j)).^(1/2)
k = j+1
while k <= n
A(k,j) = (A(k,j))/(A(j,j))
k = k+1
end
j = j+1
end
A

matlab basics : inverse matrices, transpose

matlab basics : inverse matrices, transpose



>> B = [ 2 0 ; 3 1]

B =

2 0
3 1

>> inv(B)

ans =

0.5000 0
-1.5000 1.0000


transpose

>> B = [2 0; 3 1]

B =

2 0
3 1

>> B'

ans =

2 3
0 1

Tuesday, October 6, 2009

matlab basics : matrices

% 3 by 3 matrix

A = [ 2 1 1
1 -2 1
1 1 0]

A =

2 1 1
1 -2 1
1 1 0

% column vector

b = [10; 8; 3]

b =

10
8
3

% matrix multiplication

A * [1; 0; 0]

ans =

2
1
1


% index operations

A(1:3)

ans =

2 1 1

>> A(1:4)

ans =

2 1 1 1


% adding 3 columns to the A matrix and assigning result to B
B = [A(1:3) 1 0 0; A(4:6) 0 1 0; A(7:9) 0 0 1]

B =

2 1 1 1 0 0
1 -2 1 0 1 0
1 1 0 0 0 1



% the rref function for solving a linear system

rref(A)

Saturday, October 3, 2009

matlab basics : scripts, functions, precision

running scripts
===============

matlab -r myscript


creating and using a function
=============================

g = inline('9*(s.^6) + 6*(s.^5) - 11*(s.^4) - 4*(s.^3) - 5*(s.^2) + 12*s - 4')

g(1)

precision display
=================

format long e %15 digits with exponent