Vous êtes sur la page 1sur 31

INTRODUCTION TO MATLAB

MATLAB
MATrix LABoratory ; an interactive matrix-based macro computer codes for scientific and engineering calculations. Licensed by The Math Works, Inc. Numerical methods with MATLAB; Implementation and Applications by G.W.Recktenwald published by Prentice Hall,NJ,2002. www.eece.maine.edu/mm/matweb.html www.indiana.edu/`statmath/math/matlab www.math.ucsd.edu/`driver/21d-s99/matlab-primer.html

MATLAB WINDOWS
Command Window Activated by MATLAB Each command line will be operated and its output is printed Quit to get out of the Command Window Clear to clear up current activities Editor Window Click on File in the command window File will be saved with . m extension Help icon is in the Editor Window Files can be printed out in MS-Word as a .txt file Figure Window Display a figure Select copy figure, paste it to an MS-Word file and print it

RUNNING MATLAB
Interactive Mode
Run in the command window Save the output by >> diary fn.txt . . . >> diary off Any output between the dairy lines will be saved in fn.txt in the same folder of MATLAB. contd

..contd
Batch Mode
Create the MATLAB file in Editor Window File is saved with a .m extension In the command window, locate the .m file and then type file name to run it. >> cd A : >> try 2 >> quit

DATA ENTERING
MATLAB is an Expression language
Variable symbol = Expression (of variable symbols) Matrix Operations + Addition - Substration * Multiplication ^ Power Transpose \ Left division / Right division Print (after each command line ) suppressed by a semicolon at the end of the command line.

MATRIX FUNCTIONS
eig svd Inv lu det rank

Filename: example1.m
diary ff.txt % Example of Data Entering % Scalar s=3 s=3; % Vector s= [1 2 3 4] s=[1,2,3,4] s=[1:1:4] % Matrix s=[1 2 3; 4 5 6; 7 8 9] s=[1,2,3;4,5,6;7,8,9] %Transpose st=s' rt=st(2,3) % Column vector r=s(:,3) % Row vector t=s(3,:) rr=s(:,1:2) tt=s([1 2],:) ...contd

contd
% Matrix Product h=s*r S=s % S*y=r y=S\r % S*x= e*y, eigenvalue problem e=eig(S) nn=rank(S) format long e=eig(S) [U,D]=eig(S) % Creat a new S S(3,3)= S(3,3)+3 % S*y=r y=S\r % S*x= e*y, eigenvalue problem e=eig(S) nn=rank(S) format long e=eig(S) [U,D]=eig(S)

...contd

.contd
% If statement; for statement n=3 for i=1:3 for j=1:3 a(i,j)=i*j; end end disp('a') disp([a]) for i=1:3 for j=1:3 if a(i,j) < n a(i,j)=0; end end end disp('a') disp([a]) xx= -4:0.01:4; yy=sin(xx); plot(xx,yy) diary off

Filename : ff.txt
s = 3 s = 1 s = 1 s = 1 s = 1 4 7 s = 1 4 7 st = 1 2 3 rt = 8 r = 3 6 9 4 5 6 7 8 9 2 5 8 3 6 9 2 5 8 3 6 9 2 3 4 2 3 4 2 3 4

t =
7 8 9

..contd

contd
rr = 1 2 4 5 7 8 tt = 1 2 3 4 5 6 h = 42 96 150 S = 1 2 3 4 5 6 7 8 9 Warning: Matrix is close to singular or badly scaled. Results may be inaccurate. RCOND = 2.055969e-018. > In A:\example1.m at line 26 y = 0 0 1 e = 16.1168 -1.1168 -0.0000 nn = 2 e = 16.11684396980706 -1.11684396980705 -0.00000000000000 ...contd

contd

U =0.23197068724629 0.78583023874207 0.40824829046386 0.52532209330123 0.08675133925663 -0.81649658092773 0.81867349935618 -0.61232756022881 0.40824829046386 D =16.11684396980706 0 0 0 -1.11684396980705 0 0 0 -0.00000000000000 S = 1 2 3 4 5 6 7 8 12 y =-1.00000000000000 2.00000000000000 -0.00000000000000 e =17.97213602218604 -0.69385985881828 0.72172383663223 nn = 3 e = 17.97213602218604 -0.69385985881828 0.72172383663223 U = 0.20689469056452 0.89969557489374 0.06732014112204 0.46255040691720 -0.32521336270486 -0.83303356099588 0.86211467223157 -0.29117716468641 0.54911117713431 D = 17.97213602218604 0 0 0 -0.69385985881828 0 0 0 0.72172383663223

contd

contd
n = 3 a 1 2 3 a 0 0 3 0 4 6 3 6 9 2 4 6 3 6 9

Filename : displace.m
%************************************************ % Direct Input %************************************************ %The line ended with ";" will not %be printed in MATLAB command Window k1=5; k2=10; k3=20; force=100; %Zero the 3*3 K matrix(stiffness matrix) K=zeros(3,3); %Function Diary to save the printout %in the txt file, out_disp3.txt %The txt file will be saved in %the folder where the **.m %file is excuted diary out_disp3.txt %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %Define each term in the upper %triangular K matrix

...contd

contd
K(1,1)=k1+2*k2; K(1,2)=-2*k2; K(2,2)=2*k2+k3; K(2,3)=-k3; K(3,3)=k3; %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %use do loop to duplicate the lower %triangular K matrix %note that K is symmetric %command line, 'for - end' is for looping for i=1:3 for j=i:3 %Note the exchange of indices, (i,j) K(j,i)=K(i,j); end end %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %Zero the 3*1 Force Vector matrix %(Force matrix) F=zeros(3,1)

...contd

contd
%Define each term in the F matrix F(3,1)=force; %Print 'F matrix' disp('F matrix') %Print the 3*1 F matrix disp(F) %Caculate Displacement q=inv(K)*F; %Print 'q matrix' disp('Displacement q') %Print the 3*1 q matrix disp(q) %Function "Diary off" to end the printout %in the txt file, out_disp.txt diary off

Filename : displace1.m
%************************************************ % Input from MATLAB Command Window %************************************************ %The line ended with ";" will not %be printed in MATLAB command Window k1=input('k1=?'); k2=input('k2=?'); k3=input('k3=?'); force=input('force=?'); %Zero the 3*3 K matrix(stiffness matrix) K=zeros(3,3); %Function Diary to save the printout %in the txt file, out_disp3.txt %The txt file will be saved in %the folder where the **.m %file is excuted diary out_disp3.txt %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %Define each term in the upper %triangular K matrix contd

contd
K(1,1)=k1+2*k2; K(1,2)=-2*k2; K(2,2)=2*k2+k3; K(2,3)=-k3; K(3,3)=k3; %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %use do loop to duplicate the lower %triangular K matrix %note that K is symmetric %command line, 'for - end' is for looping for i=1:3 for j=i:3 %Note the exchange of indices, (i,j) K(j,i)=K(i,j); end end %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %Zero the 3*1 Force Vector matrix %(Force matrix) F=zeros(3,1)

...contd

contd
%Define each term in the F matrix F(3,1)=force; %Print 'F matrix' disp('F matrix') %Print the 3*1 F matrix disp(F) %Caculate Displacement q=inv(K)*F; %Print 'q matrix' disp('Displacement q') %Print the 3*1 q matrix disp(q) %Function "Diary off" to end the printout %in the txt file, out_disp.txt diary off

eig
Find eigenvalues and eigenvectors

Syntax
[V,D] = eig(A,B) Description The generalized eigenvalue problem is to determine the nontrivial solutions of the equation where both A and B are n-by-n matrices and is a scalar. The values of that satisfy the equation are the generalized eigenvalues and the corresponding values of x are the generalized right eigenvectors. If B is nonsingular, the problem could be solved by reducing it to a standard eigenvalue problem d = eig(A,B) returns a vector containing the generalized eigenvalues, if A and B are square matrices. [V,D] = eig(A,B) produces a diagonal matrix D of generalized eigenvalues and a full matrix V whose columns are the corresponding eigenvectors so that A*V = B*V*D. The eigenvectors are scaled so that the norm of each is 1.0.

File name eigen1.m


File name eigen1.m; K x = r M x %************************************************ % Direct Input %************************************************ %The line ended with ";" will not %be printed in MATLAB command Window k1=5; k2=10; k3=20; m1=2; m2=1; m3=3; force=100; %Zero the 3*3 K matrix(stiffness matrix) K=zeros(3,3); %Function Diary to save the printout %in the txt file, out_disp3.txt %The txt file will be saved in %the folder where the **.m %file is excuted %*******************diary out_disp3.txt %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K)

...contd
Define each term in the upper %triangular K matrix K(1,1)=k1+2*k2; K(1,2)=-2*k2; K(2,2)=2*k2+k3; K(2,3)=-k3; K(3,3)=k3; %Print 'K matrix' disp('K matrix') %Print the 3*3 K matrix disp(K) %use do loop to duplicate the lower %triangular K matrix %note that K is symmetric %command line, 'for - end' is for looping for i=1:3 for j=i:3 %Note the exchange of indices, (i,j) K(j,i)=K(i,j); end end %Print 'K matrix' disp('K matrix')

...contd
Print the 3*3 K matrix disp(K) %Zero the 3*3 M matrix(Mass matrix) zeros(3,3); %Mass Matrix M(1,1)=2*m1+2*2*m2; M(1,2)=-2*2*m2; M(2,2)=2*2*m2+2*m3; M(2,3)=-2*m3; M(3,3)=2*m3; %note that M is symmetric %command line, 'for - end' is for looping for i=1:3 for j=i:3 %Note the exchange of indices, (i,j) M(j,i)=M(i,j); end end %Print 'M matrix' disp('M matrix') %Print the 3*3 M matrix disp(M)

...contd
Find eigenvalue(V) & eigenfactor(D) [V,D]=eig(K,M); %Print 'eigenvalue' disp('eigenvalue') %Print eigenvalue disp([D]) %Print 'eigenvector' disp('eigenfactor') %Print eigenfactor disp([V]) %Zero the 3*1 Force Vector matrix %(Force matrix) F=zeros(3,1) %Define each term in the F matrix F(3,1)=force; %Print 'F matrix' disp('F matrix') %Print the 3*1 F matrix disp(F) %Caculate Displacement q=inv(K)*F; %Print 'q matrix' disp('Displacement q')

...contd
%Print the 3*1 q matrix disp(q) %Function "Diary off" to end the printout %in the txt file, out_disp.txt %*********************diary off

Ode45, ode23, ode113 Ode15s, ode23s, Ode23t, ode23tb


Solve differential equations
Syntax [T,Y] = solver('F',tspan,y0) Arguments F Name of the ODE file, a MATLAB function of t and y returning a column vector. All solvers can solve systems of equations in the form . ode15s, ode23s, ode23t, and ode23tb can solve equations of the form . Of these four solvers all but ode23s can solve equations in the form For information about ODE file syntax, see the odefile reference page.

tspan A vector specifying the interval of integration [t0 tfinal]. To obtain solutions at specific times (all increasing or all decreasing), use tspan = [t0,t1, ..., tfinal]. Y0 A vector of initial conditions.

%Solve T'=(Q-kT)/c, the right hand side %is defined by the file vdo1.m [t,T]=ode45('vdp1',[0,20],[0]); plot(t,T) %defined the right-hand side of the ODE; %File name, vdp1.m function out1=vdp1(t,T); k=5; c=0.1; out1=[(sin(t)-k*T)/c];

quad, quad8
Numerical evaluation of integrals Syntax q = quad('fun',a,b) Description Quadrature is a numerical method of finding the area under the graph of a function, that is, computing a definite integral.

q = quad('fun',a,b) returns the result of numerically integrating 'fun' between the limits a and b. 'fun' must return a vector of output values when given a vector of input values.

%Integration the function defined in file shapefun.m %from t1 to t2 disp('Integrate from') t1=input('t1') disp('Integrate to') t2=input('t2') a=quad('shapefun',t1,t2)

%Defined function for integration. File name,shapefun.m function y=shapefunc(t) y=(1/2)*((5*t.^3)-(3*t));

Vous aimerez peut-être aussi