Upload
optimal87
View
128
Download
0
Tags:
Embed Size (px)
Citation preview
% Inputs:
% Coefficient matrix A
%
% Outputs:
% an lower and upper triangular matrix;
% Usage:
% [l, u] = LUdecompCrout(a);
% Notes:
%
% ************************************************************************@
[R,C]=size(a);
% starting the Gauss elimination procedure
for i=1:R
L(i,1) = a(i,1);
U(i,i) = 1;
end
for j = 2:R
U(1,j) = a(1,j)/L(1,1);
end
for i = 2:R
for j = 2:i
L(i,j) = a(i,j) - L(i,1:j-1)*U(1:j-1,j);
end
for j = i + 1:R
U(i,j) = (a(i,j) - L(i,1:i-1)*U(1:i-1,j))/L(i,i);
end
end
/////////////////////////////////////////////////////////////////////////////
function [x, iter]=GaussJacobiIte(a,b,err,x_1)
%Definition: GaussJacobiIte(a,b,err,x_1)
%*********************************************
%Inputs:
% Coefficient matrix a
% right side constant array b
% initial value: x_1
% error: err,
% limit of iteration:
%Outputs:
% Solution of linear system,x
% No. of iteration, iter
%Usage:
% [x,iter]=GaussJacobiIte(a,b,err,x_1);
%notes:
%
%**************
%The function solves a system of linear equations[a]{x}=[b] using
%Jacobi iterative method.
R=length(b);
%x=zeros(1,R);
DX=max(abs(x_1));
iter=1;
x=x_1;
while DX>err
iter=iter+1;
for i=1:R
sumax=0;
for j=1:R
if j ~=i
sumax=sumax+a(i,j)*x_1(j);
end
end
x(i)=1/a(i,i)*(b(i)-sumax);
end
% DX=max(abs((x-x_1)))
DX=norm(abs(x-x_1));%, pause
x_1 = x;
%if iter=5
% break %this part is used in lect_9
%end
%x_1 = lambda*x+(1-lambda)*x_1,iter
end
///////////////////////////////////////////////////////////////
A=[1 2 3; 7 3 1; 1 6 2]
A =
1 2 3
7 3 1
1 6 2
B=[20 13 0]'
B =
20
13
0
A1=[B,A(2,:),A(3,:)]
??? Error using ==> horzcat
CAT arguments dimensions are not consistent.
A1=[B,A(2,:)',A(3,:)']
A1 =
20 7 1
13 3 6
0 1 2
A1=[B,A(:,2),A(:.3)]
??? A1=[B,A(:,2),A(:.3)]
|
Error: Unexpected MATLAB expression.
A1=[B,A(:,2),A(:3)]
??? A1=[B,A(:,2),A(:3)]
|
Error: Unexpected MATLAB expression.
A1=[B,A(:,2),A(:,3)]
A1 =
20 2 3
13 3 1
0 6 2
A2=[A(:,1),B,A(:,3)]
A2 =
1 20 3
7 13 1
1 0 2
A3=[A(:,1),A(:,2),B]
A3 =
1 2 20
7 3 13
1 6 0
det(A1)
ans =
182.0000
det(A2)
ans =
-273
det(A3)
ans =
728.0000
det(A)
ans =
91
D1=det(A1)
D1 =
182.0000
D1=det(A1)
D1 =
182.0000
D2=det(A2)
D2 =
-273
D3=det(A3)
D3 =
728.0000
D=det(A)
D =
91
x1=D1/D
x1 =
2.0000
x2=D2/D
x2 =
-3
x3=D3/D
x3 =
8.0000
/////////////////////////////////////////////////
clear
A=[-2 2 1;2 1 6;-1 -2 0]
A =
-2 2 1
2 1 6
-1 -2 0
[V,D]=eig(A)
V =
-0.8944 0.2942 - 0.1698i 0.2942 + 0.1698i
0.4472 0.7845 0.7845
-0.0000 -0.0981 + 0.5095i -0.0981 - 0.5095i
D =
-3.0000 0 0
0 1.0000 + 3.4641i 0
0 0 1.0000 - 3.4641i
A=[-2 2 1;2 1 -6;-1 -2 0]
A =
-2 2 1
2 1 -6
-1 -2 0
[V,D]=eig(A)
V =
-0.9451 0.2032 -0.8944
0.2924 0.8758 0.4472
-0.1462 -0.4379 -0.0000
D =
-2.4641 0 0
0 4.4641 0
0 0 -3.0000
A=[-2 2 -3;2 1 -6;-1 -2 0]
A =
-2 2 -3
2 1 -6
-1 -2 0
[V,D]=eig(A)
V =
-0.9526 0.4082 0.0516
0.2722 0.8165 0.8229
-0.1361 -0.4082 0.5658
D =
-3.0000 0 0
0 5.0000 0
0 0 -3.0000
/////////////////////////////////////////////////////
function y = GaussElim(a,b)
%Definition: GaussElim
%*********************************************************************
%Inputs:
%Coefficient matrix A
% right side constant array B
%Outputs:
%solution to the linear system;
%Usage:
%y = GaussElim_(a, b)
%Notes:
%The function solves a system of linear equations
% using Gauss elimination method
%**********************************************************************
%This function determines the upper triangle of an input matrix using Gauss
%elimination method
ab= [a,b];
[R,C] = size(ab);
%call the user defined function to perform row elementary operation
ab_upper = GaussElim_f(ab);
y = zeros(R,1);
y(R) = ab_upper(R,C)/ab_upper(R,R);
%start the back substitution
for i = R-1:-1:1
y(i) = (ab_upper(i,C) - ab_upper(i,i+1:R)*y(i+1:R))/ab_upper(i,i)
end
///////////////////////////////////
function x = partic(A, b)
% partic Particular solution of Ax=b.
%
% x = partic(A, b) returns a particular solution to Ax=b.
% This particular solution has all free variables set to zero.
% An empty vector is returned if Ax=b is not solvable.
%
% See also slash as in A\b .
[m, n] = size(A);
[Rd, pivcol] = rref([A b]);
r = length(pivcol);
%
% If the last column of the augmented matrix [A b]
% is a pivot column, then Ax=b has no solution.
%
if max(pivcol) == n+1
x = [];
else
%
% The values of the pivot variables are in the
% last column (which is called d) of Rd.
% The free variables are zero in this particular solution.
%
x = zeros(n, 1);
d = Rd(:, n+1);
x(pivcol) = d(1:r);
end
%copy paste :x = partic(A, b)???
/////////////////////////////////////////////////
function x = GaussPivot(a,b)
%Definition: GaussPivot(a,b)
%*********************************************************************
%Inputs:
% Coefficient matrix a
% and right side constant array b
%Outputs:
% Solution of linear system;
%Usage:
% s = GaussPivot(a,b);
%Notes:
%The function solves a system of linear equations [a]{x}=[b] using
%elimination method with pivoting.
%***********************************************************
ab=[a,b];
[R,C]=size(ab);
%starting the Gauss elimination procedure
for j=1:R-1
%Pivoting section starts
if ab(j,j) ==0 % < 10^-12
for k = j + 1:R
if ab(k,j) ~= 0
abTemp = ab(j,:);
ab(j,:) = ab(k,:);
ab(k,:) = abTemp;
break
end
end
end
%Pivoting section ends
%Elimination procedure starts
for i = j+1:R
ab(i,j:C) = ab(i,j:C)-ab(i,j)/ab(j,j)*ab(j,j:C);
end
%Gauss elimination procedure ends
end
x = zeros(R,1);
x(R) = ab(R,C)/ab(R,R);
%start the back substitution
for i = R-1:-1:1
x(i) = (ab(i,C) - ab(i,i+1:R)*x(i+1:R))/ab(i,i);
end
%end the back substitution
////////////////////////////////////////
function [x,err]=GaussJordanElim(A,b)
% Definition: [x,err]=GaussJordanElim(A,b)
% ************************************************************************@
% Inputs:
% Coefficient matrix A
% and right side constant array b
% Outputs:
% Solution of linear system; and an intermediate housekeeping variable
% error
% Usage:
% [x,err]=GaussJordanElim(A,b);
% Notes:
% There are also two specific user defined function inside this function
% ************************************************************************@
% The function solves a system of linear equations [a]{x}=[b] using Gauss
% elimination method.
[n,m]=size(A); % size of matrix A
err =0;
x=zeros(n,1);
if n ~= m
disp('error: n~=m');
err = 1;
end
if length(b) ~= n
disp('error: wrong size of b');
err = 2;
else
if size(b,2) ~= 1
b=b';
end
if size(b,2) ~= 1
disp('error: b is a matrix');
err = 3;
end
end
if err == 0
Aa=[A,b];
for i=1:n
[Aa(i:n,i:n+1),err]=gauss_pivot(Aa(i:n,i:n+1));
if err == 0
Aa(1:n,i:n+1)=gauss_jordan_step(Aa(1:n,i:n+1),i); %, pause
end
end
x=Aa(:,n+1);
end
A=0;
%----------------------------------------------------
function A1=gauss_jordan_step(A,i)
[n,m]=size(A); % size of matrix A
A1=A;
s=A1(i,1);
A1(i,:) = A(i,:)/s;
k=[[1:i-1],[i+1:n]];
for j=k
s=A1(j,1);
A1(j,:)=A1(j,:)-A1(i,:)*s;
end
%----------------------------------------------------------
function [A1,err]=gauss_pivot(A)
[n,m]=size(A); % size of matrix A
A1=A;
err = 0; % error flag
if A1(1,1) == 0
check = logical(1); % logical(1) - TRUE
i = 1;
while check
i = i + 1;
if i > n
disp('error: matrix is singular');
err = 1;
check = logical(0);
else
if A(i,1) ~= 0 & check
check = logical(0);
b=A1(i,:); % next three operations exchange rows 1 and i
A1(i,:)=A1(1,:);
A1(1,:)=b;
end
end
end
end
/////////////////////////////////////////////////
function y = GaussElim_f(a)
%Definition: GaussElim_f
%*********************************************************************
%Inputs:
%Coefficient matrix A
%Outputs:
%upper triangular matrix;
%Usage:
%x = GaussElim_f(a)
%Notes:
%
%**********************************************************************
%This function determines the upper triangle of an input matrix using Gauss
%elimination method
[R,C] = size(a); %determines the dimension of a matrix starting the Gauss
%elimination procedure
for j = 1:R-1
for i = j + 1:R
a(i,j:C) = a(i,j:C)-a(i,j)/a(j,j)*a(j,j:C)
end
end
y=a;
////////////////////////////////////////////////////////////////////////
A=[3 -1 2;1 2 3;2 -2 -1]
A =
3 -1 2
1 2 3
2 -2 -1
B=[12 11 2]'
B =
12
11
2
[L, U] = LUdecompCrout(A)
??? Undefined function or method 'LUdecompCrout' for input arguments of type
'double'.
[L, U] = LUdecompCrout(A)
L =
3.0000 0 0
1.0000 2.3333 0
2.0000 -1.3333 -1.0000
U =
1.0000 -0.3333 0.6667
0 1.0000 1.0000
0 0 1.0000
a=[2 0 1;0 2 -1;1 -1 4]
a =
2 0 1
0 2 -1
1 -1 4
b=[1 2 -1]
b =
1 2 -1
V=diag(a)
V =
2
2
4
D=diag(V)
D =
2 0 0
0 2 0
0 0 4
A_off=a-D
A_off =
0 0 1
0 0 -1
1 -1 0
inv(D0
??? inv(D0
|
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
inv(D)
ans =
0.5000 0 0
0 0.5000 0
0 0 0.2500
[x, iter]=GaussJacobiIte(a,b,err,x_1)
??? Undefined function or variable 'err'.
[x, iter]=GaussJacobiIte(a,b,err,x_1)
??? Undefined function or variable 'err'.
[x, iter]=GaussJacobiIte(a,b,10^-3,x_1)
??? Undefined function or variable 'x_1'.
x_1=[0 0 0]
x_1 =
0 0 0
[x, iter]=GaussJacobiIte(a,b,10^-3,x_1)
x =
0 0 0
iter =
1
x_1=[0 0 1]
x_1 =
0 0 1
[x, iter]=GaussJacobiIte(a,b,10^-3,x_1)
x =
0 0 1
iter =
2
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0 0 1
iter =
2
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5000 1.0000 -0.2500
iter =
3
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5834 0.9166 -0.1664
iter =
13
x_1=[0 1 0]
x_1 =
0 1 0
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5830 0.9170 -0.1665
iter =
11
x_1=[1 0 0]
x_1 =
1 0 0
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5835 0.9165 -0.1666
iter =
13
x_1=[1 1 1]
x_1 =
1 1 1
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5834 0.9166 -0.1664
iter =
13
x_1=[1 1 0]
x_1 =
1 1 0
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5835 0.9165 -0.1665
iter =
11
x_1=[0 1 1]
x_1 =
0 1 1
[x, iter]=GaussJacobiIte(a,b,0.001,x_1)
x =
0.5833 0.9167 -0.1664
iter =
13
A = [4 -2 -3 6; -6 7 6.5 -6; 1 7.5 6.25 5.5; -12 22 15.5 -1]
A =
4.0000 -2.0000 -3.0000 6.0000
-6.0000 7.0000 6.5000 -6.0000
1.0000 7.5000 6.2500 5.5000
-12.0000 22.0000 15.5000 -1.0000
B=[12 -6.5 16 17]'
B =
12.0000
-6.5000
16.0000
17.0000
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
??? Attempted to access x_1(4); index out of bounds because numel(x_1)=3.
Error in ==> GaussJacobiIte at 30
sumax=sumax+a(i,j)*x_1(j);
x_1=[0 0 0]
x_1 =
0 0 0
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
0 0 0
iter =
1
A
A =
4.0000 -2.0000 -3.0000 6.0000
-6.0000 7.0000 6.5000 -6.0000
1.0000 7.5000 6.2500 5.5000
-12.0000 22.0000 15.5000 -1.0000
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
0 0 0
iter =
1
A\B
ans =
2.0000
4.0000
-3.0000
0.5000
x_1=[0 0 0 0]
x_1 =
0 0 0 0
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
0 0 0 0
iter =
1
x_1=[0 0 0 1]
x_1 =
0 0 0 1
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
-Inf Inf NaN -Inf
iter =
426
x_1=[0 0 1 1]
x_1 =
0 0 1 1
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
NaN NaN NaN NaN
iter =
427
A\B
ans =
2.0000
4.0000
-3.0000
0.5000
x_1=[0 0 1 0]
x_1 =
0 0 1 0
[x, iter]=GaussJacobiIte(A,B,0.001,x_1)
x =
NaN NaN NaN NaN
iter =
427
x_1=[2 1 1 0]
x_1 =
2 1 1 0
[x, iter]=GaussJacobiIte(A,B,0.0001,x_1)
x =
NaN NaN NaN NaN
iter =
427
x_1=[2 1 -2 3]
x_1 =
2 1 -2 3
[x, iter]=GaussJacobiIte(A,B,0.0001,x_1)
x =
Inf -Inf NaN Inf
iter =
425
A=[9 -4 -2 0;-4 17 -6 -3;-2 -6 14 -6;0 -3 -6 11]
A =
9 -4 -2 0
-4 17 -6 -3
-2 -6 14 -6
0 -3 -6 11
B=[24;-16;0;18]
B =
24
-16
0
18
[x, iter]=GaussJacobiIte(A,B,0.1,x_1)
x =
3.8284 1.4148 2.5474 3.3691
iter =
13
A\B
ans =
4.0343
1.6545
2.8452
3.6395
A
A =
9 -4 -2 0
-4 17 -6 -3
-2 -6 14 -6
0 -3 -6 11
x_1
x_1 =
2 1 -2 3
x_1=[0 0 0 0]
x_1 =
0 0 0 0
[x, iter]=GaussJacobiIte(A,B,0.1,x_1)
x =
0 0 0 0
iter =
1
A\B
ans =
4.0343
1.6545
2.8452
3.6395
x_1=[0 0 0 1]
x_1 =
0 0 0 1
[x, iter]=GaussJacobiIte(A,B,0.1,x_1)
x =
3.8286 1.4149 2.5490 3.3687
iter =
14
x_1=[0 0 0 1]
x_1 =
0 0 0 1
[x, iter]=GaussJacobiIte(A,B,0.0001,x_1)
x =
4.0341 1.6543 2.8449 3.6393
iter =
53
A\B
ans =
4.0343
1.6545
2.8452
3.6395
3/13Gauss Elim code notes>> help size SIZE Size of array. D = SIZE(X), for M-by-N matrix X, returns the two-element row vector D = [M,N] containing the number of rows and columns in the matrix. For N-D arrays, SIZE(X) returns a 1-by-N vector of dimension lengths. Trailing singleton dimensions are ignored. [M,N] = SIZE(X) for matrix X, returns the number of rows and columns in X as separate output variables. [M1,M2,M3,...,MN] = SIZE(X) for N>1 returns the sizes of the first N dimensions of the array X. If the number of output arguments N does not equal NDIMS(X), then for: N > NDIMS(X), SIZE returns ones in the "extra" variables, i.e., outputs NDIMS(X)+1 through N. N < NDIMS(X), MN contains the product of the sizes of dimensions N through NDIMS(X). M = SIZE(X,DIM) returns the length of the dimension specified by the scalar DIM. For example, SIZE(X,1) returns the number of rows. If DIM > NDIMS(X), M will be 1. When SIZE is applied to a Java array, the number of rows returned is the length of the Java array and the number of columns is always 1. When SIZE is applied to a Java array of arrays, the
result describes only the top level array in the array of arrays. Example: If X = rand(2,3,4); then d = size(X) returns d = [2 3 4] [m1,m2,m3,m4] = size(X) returns m1 = 2, m2 = 3, m3 = 4, m4 = 1 [m,n] = size(X) returns m = 2, n = 12 m2 = size(X,2) returns m2 = 3 See also length, ndims, numel.
>> A
A =
5 1 2 1 4 -2 2 3 8
>> [R,C] = size(A)
R =
3
C =
3////////////////////////////////////////>> help zero --- help for DynamicSystem/zero ---
ZERO Computes the transmission zeros of linear systems. Z = ZERO(SYS) returns the transmission zeros of the dynamic system SYS. Zero values are expressed in the reciprocal of the time units of SYS (for example, 1/minute if SYS.TimeUnit = 'minutes'). [Z,GAIN] = ZERO(SYS) also returns the transfer function gain (in the zero-pole-gain sense) for SISO models SYS. [Z,...] = ZERO(SYS,J1,...,JN) computes the transmission zeros of the model with subscripts (J1,...,JN) in the model array SYS.
See also damp, pole, pzmap, iopzmap, zpk, lti, DynamicSystem.
>> y = zeros(R,1)
y =
0 0 0
>> y = zeros(R,2)
y =
0 0 0 0 0 0
>> y = zeros(3,2)
y =
0 0 0 0 0 0
>> y = zeros(5,2)
y =
0 0 0 0 0 0 0 0 0 0
//////////////////////>> [A,B]
ans =
5 1 2 19 1 4 -2 -2 2 3 8 39
///////////////////////////////////>> R-1:-1:1
ans =
2 1
>> [10:-1:3]
ans =
10 9 8 7 6 5 4 3
>> [10:1:30]
ans =
Columns 1 through 12
10 11 12 13 14 15 16 17 18 19 20 21
Columns 13 through 21
22 23 24 25 26 27 28 29 30
//////////////////////////////////////For>> help for FOR Repeat statements a specific number of times. The general form of a FOR statement is: FOR variable = expr, statement, ..., statement END The columns of the expression are stored one at a time in the variable and then the following statements, up to the END, are executed. The expression is often of the form X:Y, in which case its columns are simply scalars. Some examples (assume N has already been assigned a value).
for R = 1:N for C = 1:N A(R,C) = 1/(R+C-1); end end Step S with increments of -0.1 for S = 1.0: -0.1: 0.0, do_some_task(S), end Set E to the unit N-vectors for E = eye(N), do_some_task(E), end Long loops are more memory efficient when the colon expression appears in the FOR statement since the index vector is never created. The BREAK statement can be used to terminate the loop prematurely. See also parfor, if, while, switch, break, continue, end, colon.
Reference page in Help browser doc for
>> for R = 1:5 for C = 1:5 A(R,C) = 1/(R+C-1); end end>> A(R,C)
ans =
0.1111
>> (1/(1+1-1))
ans =
1
>> (2/(2+2-1))
ans =
0.6667