NoViC, Dept. of Mechanical Eng
Acoustics Lab., NoViC 1. Download Matlab 2
Acoustics Lab., NoViC Activation Key Create “Mathworks” account 3
Acoustics Lab., NoViC 3. Installation 4 Matlab default view
Acoustics Lab., NoViC 1. Input variables and operations (1/5) 5 >> a = 1 a = 1 >> b = 2 b = 2 Command Window >> a + b ans = 3 >> a - b ans = >> a * b ans = 2 >> a / b ans = 4 fundamental arithmetic operations Scalar input (real value) Command Window + : Addition - : Subtraction * : Multiplication / : Division Priority of operation: (*, /) > (+, -) >> a + b / a - b ans = 1 >> (a + b) / (a - b) ans = -3 Command Window Square operation (^) >> a^5 (when a=2) ans = 32 >> a^5+1 ans = 33 >> a^(5+1) ans = 64 Command Window
Acoustics Lab., NoViC 1. Input variables and operations (2/5) 6 >> c = a+j*b c = i >> c = a+i*b c = i Command Window Scalar input (complex value) i or j : complex variable Trigonometric functions >>x = 45/180*pi x = >> sin(x) ans = >> cos(x) ans = Command Window Complex operations >> real(c) ans = 1 >> imag(c) ans = 2 >> conj(c) ans = i >> abs(c) ans = >> angle(c) ans = Command Window real(c): real value of c imag(c): image value of c conj(c): complex conjugate of c abs(c): absolute value of c angle(c): phase value of c >> tan(x) ans = >> exp(j*x) ans = i Real Im 1-2i 1 2
Acoustics Lab., NoViC 1. Input variables and operations (3/5) 7 >> A=[1 2; 3 4] A = >> B=[3 5; 7 9] B = >> C = [1; 5] Command Window Matrix and vector input Matrix-matrix operation >> A + B ans = >> A - B ans = Command Window >> C = [1 5]' C = 1 5 >> D = [2 3] D = 2 3 >> E = [1:2:9] E = ; (semicolon): Changing the row : (colon): Pick out selected row ‘ (inverted comma): Transpose operator >> A * B ans = >> A^3 ans = >> A*C ans = 1 5 >> D*A ans = Command Window Matrix-vector/scalar operation >> A*5 ans = >> A/2 ans =
Acoustics Lab., NoViC 1. Input variables and operations (4/5) 8 Command Window Other matrix operations >> transpose(A) ans = >> det(A) ans = -2 >> inv(A) ans = >> A * inv(A) ans = >> [V,D] = eig(A) V = D = Command Window >> size(A) ans = 2 2 >> size(C) ans = 2 1 >> length(A) ans = 2 >> length(C) ans = 2 >> eye(2,2) ans = >> zeros(1,2) ans = 0 0 >> ones(1,2) ans = 1 1 transpose(A) same with ‘ (inverted comma) det(A) Determinant of A matrix inv(A) Inverse matrix of A matrix eig(A) Eigenvalues and vectors V: Eigenvectors D: Eigenvalues size(A) Size of A matrix length(A) length of A matrix eye(m,n) Make the mXn identity matrix zeros(m,n) Make the mXn matrix of zeros ones(m,n) Make the mXn matrix of ones
Acoustics Lab., NoViC 1. Input variables and operations (5/5) 9 Command Window Comma for matrix computation >> A*B ans = >> A.*B ans = >> A^2 ans = >> A.^2 ans = Command Window >> x=[0:0.1:pi]; >> y=sin(x); ; (semicolon): hide the input/output variables variables save in the workspace Function and vector General matrix multi. Element by element multi. General matrix multi. Element by element multi. Final value (Upper bound) Initial value (Lower bound) Step size X=[ … ] (Dimension: 1X32) Y=[ ] (Dimension: 1X32)
Acoustics Lab., NoViC 2. Plotting the graph (1/1) 10 Setting the variables (input and output values) Plotting the figure >> figure(1) >> plot(x, y, 'r-') >> grid on >> xlabel('x [rad]') >> ylabel('sin(x)') >> title('sin function') Command Window >> x=[0:0.1:pi]; >> y=sin(x); figure(1) numbering the figure Plot(x,y,’r-’) x: x-axis data, y: y-axis data, ‘r-’: red solid line grid on show the grid line on the figure To get the more information of plot, Input the help plot in command window Command Window >> help plot semilogx x-axis is log scale semilogy y-axis is log scale loglog x, y-axes are log scale
Acoustics Lab., NoViC M-file making 11 F5 Dragging and select several statement +F9 key Run only in the selected part F9
Acoustics Lab., NoViC 3. Iteration structure (1/2) 12 >> sum = 0; >> for n = 1:10 sum = sum + n; end >> sum sum = 55 Command Window Example) sum of 1 to 10 for variable = int : n : final statement end Repeat the statement until the final variable Not convenient to use command window Open the editor Click F5 key save the editor Implement the MATLAB program
Acoustics Lab., NoViC 3. Iteration structure (2/2) 13 Example) sum of 1 to 10 while conditional statement statement end Repeat the statement until conditional statement is satisfied C.if loop Example) sum of 1 to 10 if expression1 statement1 elseif expression2 statement2 else statement3 end Implement statement1 if expression1 is satisfied, Implement statement2 if expression1 is unsatisfied and expression2 is unsatisfied, Implement statement3 if expression 1 and 2 are unsatisfied
Acoustics Lab., NoViC 14 function xdot = sdof(t,x) xdot = zeros(2,1); xdot(1) = x(2); xdot(2) = -(2/3)*x(1) - (1/3)*x(2); 1. First-order form t0 = 0; tf = 20; x0 = [0 0.25]; [t, x] = ode45('sdof', [t0 tf], x0); plot(t,x(:,1),'linewidth',3); hold on; plot(t,x(:,2),'--r','linewidth',3); title('Example 1.9.2'); xlabel('Time'); ylabel('Displacement (solide) and velocity (dashed)'); grid on; sdof.m Tutorial_ode45.m where
Acoustics Lab., NoViC Start matlab 2. input this code -> urlwrite(' nt.com/vibrationtoolbox/vtoolbox/ma ster/vtbud.m','vtbud.m'); 3. then input this code -> vtbud 4. Installation will be started You have to select the path where the function files are