Académique Documents
Professionnel Documents
Culture Documents
Prof. Dr. Y. Samim NLSOY Mech. Eng. Dept., Middle ME 513 Vehicle Dynamics East Technical 1 University, Ankara
MATLAB 7.0.1.lnk
MATLAB
Load or save files here
Double-click
INTRODUCTION TO MATLAB
Matrices In MATLAB, a matrix is a rectangular array of numbers.
Scalars are 1-by-1 matrices. Vectors are matrices with only one row or column.
MATLAB has other ways of storing both numeric and nonnumeric data; but in the beginning, it is usually best to think of everything as a matrix. Where other programming languages work with numbers one at a time, MATLAB allows you to work with entire matrices quickly and easily.
Prof. Dr. Y. Samim nlsoy ME 304 Control Systems 3
Entering a Matrix
You have only to follow a few basic conventions:
Separate the elements of a row with blanks or commas. Use a semicolon, ; ,to indicate the end of each row. Surround the entire list of elements with square brackets, [ ].
Entering A Matrix
Start by entering the matrix as a list of its elements. To enter, simply type in the Command Window
A = [16 3 2 13; 5 10 11 8; 9 6 7 12; 4 15 14 1]
Once you have entered the matrix, it is automatically remembered in the MATLAB workspace. You can refer to it simply as A. Now that you have A in the workspace.
2 13 11 8 7 12 14 1
Transpose of a Matrix
There are two possibilities to transpose a matrix. Use an apostrophe or single quote, '.
A= 16 3 5 10 9 6 4 15 2 13 11 8 7 12 14 1
9 4 6 15 7 14 12 1
Inverse of a Matrix
inv(X) is the inverse of the square matrix X.
10
C= 16 3 5 10 9 6 4 15 2 13 11 8 15 12 14 1
>> C C= 16 3 2 13 5 10 11 8 9 6 15 12 4 15 14 1 >> B=inv(C) C= 0.0760 -0.1985 0.0417 0.1005 0.0196 0.1544 -0.1250 0.0098 -0.0417 -0.1250 0.1250 0.0417 -0.0147 0.2279 -0.0417 -0.1324 >>
11
Subscripts
The element in row i and column j of A is denoted by A(i,j). For example, A(4,2) is the number in the fourth row and second column. So it is possible to compute the sum of the elements in the fourth column of A by typing
A(1,4) + A(2,4) + A(3,4) + A(4,4)
12
The expression 1:10 is a row vector containing the integers from 1 to 10 1 2 3 4 5 6 7 8 9 10 To obtain nonunit spacing, specify an increment. 100:-7:50 is 100 93 86 79 72 65 58 51 and 0:pi/4:pi is 0 0.7854 1.5708 2.3562 3.1416
Colon Operator
13
0.7854
1.5708
2.3562
3.1416
Colon Operator
Subscript expressions involving colons refer to portions of a matrix. A(1:k,j) is the first k elements of the jth column of A. So sum(A(1:4,4)) computes the sum of the fourth column. The colon by itself refers to all the elements in a row or column of a matrix and end refers to the last row or column. So sum(A(:,end)) computes the sum of the elements in the last column of A.
Prof. Dr. Y. Samim nlsoy ME 304 Control Systems 15
A= 16 3 5 10 9 6 4 15 2 13 11 8 7 12 14 1
16
A= 16 3 5 10 B= 7 12 14 1
>> A*B ans = 154 195 175 70 >> A.*B ans = 112 36 70 10 >>
18
19
A= 16 3 5 10 B= 7 12 14 1
>> B/A ans = 0.0690 1.1793 0.9310 -0.1793 >> B*inv(A) ans = 0.0690 1.1793 0.9310 -0.1793 >>
20
A= 16 3 5 10 B= 7 12 14 1
>> A./B ans = 2.2857 0.2500 0.3571 10.0000 >> A\B ans = 0.1931 0.8069 1.3034 -0.3034 >>
21
A= 16 3 5 10 9 6 4 15 2 13 11 8 15 12 14 1
23
Variables
MATLAB does not require any type declarations or dimension statements. A new variable name is automatically created and appropriate amount of storage is allocated. If the variable already exists, MATLAB changes its contents and, if necessary, allocates new storage. Variable names consist of a letter, followed by any number of letters, digits, or underscores. MATLAB uses only the first 31 characters of a variable name. MATLAB is case sensitive; it distinguishes between uppercase and lowercase letters. A and a are not the same variable. To view the matrix assigned to any variable, simply enter the variable name and press enter.
25
Functions
MATLAB provides a large number of standard elementary mathematical functions, such as
abs, sqrt, exp, and sin, cos, tan etc..
26
27
28
29
30
For example, PLOT(X,Y,'c+:') plots a cyan dotted line with a plus at each data point; PLOT(X,Y,'bd') plots blue diamond at each data point but does not draw any line.
31
32
m-files
There are two types of m-files :
m-files
Script M-Files
Do not accept input arguments or return output arguments. Operate on data in the workspace. Useful for automating a series of steps you need to perform many times.
36
Script M-Files
Consider a one degree of freedom mass-spring-damper system representing a road vehicle travelling on a rough road surface. x m c k z Obtain the motion of the vehicle in terms of the vehicle body mass displacement y and its derivatives. Assume that the road surface profile z is sinusoidal with a frequency of 5 rad/s and an amplitude of 1 cm.
37
x m c k z
Script M-Files
The equation of motion is given as :
mx + cx + kx = cz + kz
The transfer function is then given by :
38
% March 2004 % Response of a vehicle travelling on a % sinusoidal road surface profile. % ------------------------------------------------% Define Parameters and Variables % m : vehicle mass [kg]
Script M-Files
% c : total damping coefficient of suspension dampers [m/s] % k : total spring constant of suspension springs [kN/m] % z : vehicle body displacement [m] % ------------------------------------------------% Enter Data Values m=1260; c=4000; k=60000; .....
39
.....
Script M-Files
% --------------------------------------------------------% Generate the time array for the solution t=0:0.01:10; % time array (from 0 to 10 seconds with 0.01 second increments) % ----------------------------------------------% Generate the road surface profile A=0.01; % road surface profile amplitude [m] omega=5; % road surface profile frequency [rad/s] z=A*sin(omega*t); % road surface profile [m] % ---------------------------------------% Plot the road surface profile
figure(1) plot(t,z) title(Sinusoidal Road Surface Profile - Amplitude : 1 cm, Frequency : 5 rad/s); xlabel(Time[s]);ylabel(y[m]);
.....
40
.....
Script M-Files
% ------------------------------------------% Generate the transfer functions s=tf(s); % Declare s as the Laplace variable % from z to y hz=(c*s+k)/(m*s^2+c*s+k); % from z to ydot (velocity) hzdot=s*hz; % from z to ydoubledot (acceleration) hzddot=s*hzdot; % ----------------------------------------------% Plot the displacement, velocity, and acceleration of % the vehicle body figure(2) % Plot of displacement note that hz is the TF from z to y lsim(hz,z,t) .....
Prof. Dr. Y. Samim nlsoy ME 304 Control Systems 41
Script M-Files
..... figure(3) % Plot of velocity note that hzdot is the TF from z to ydot lsim(hzdot,z,t) .....
42
0.008
0.006
0.004
0.002 y [m]
Script m-files
This is what you get from the program defaults !
0 1 2 3 4 5 Time [s] 6 7 8 9 10
-0.002
-0.004
-0.006
-0.008
-0.01
43
0.02 0.015 0.01 ydot [m/s] 0.005 0 -0.005 -0.01 -0.015 -0.02 0
Script m-files
You can modify the plots using the edit facility on the Matlab plot
10
44
Sinusoidal Road Surface Profile - Amplitude : 1 cm, Frequency : 5 rad/s 0.1 0.08 0.06 ydoubledot [m/s2] 0.04 0.02 0 -0.02 -0.04 -0.06 -0.08 -0.1
Script m-files
You can modify it using the edit facility on the Matlab plot
0 1 2 3 4 5 6 Time [s] (sec) 7 8 9 10
45
m-files
Function m-Files
Can accept input arguments and return output arguments. Internal variables are local to the function by default. Useful for extending the MATLAB language for your application.
46
Function m-files
function [Fx,Fy]=tireforces(munom,Fx,Fz,alpha,slip,vwl) ..... ..... .....
47
48
49
50
52
53