Outline Lesson 2
Outline Lesson 2
1. About Matlab
1.1. Matlab
MATLAB is an application software of Mathworks Corporation (USA). This is a very
powerful mathematical processing software, through the implementation of mathematical
operations on the matrix (MAT - Matrix), which is programmed by mathematical and
computer experts to solve the algorithms through mathematical processing tool libraries
(LAB - Laboratory). The software also provides many different modules, serving specific
fields and specialties.
The purpose of Matlab software is for researchers, designers, and programmers
1.2. Scope of application
Matlab serves as a powerful computational tool that allows you to quickly calculate the
value of a complex expression and store the value of the expression in the computer's
memory
Malab provides tools for processing arrays of data: vectors and matrices, allowing the
calculation of the results of expressions with vector input data
Matlab provides functions to solve common engineering problems such as:
Processing polynomials (multiplying, dividing, finding the 0 point (solution) of
polynomials).
Solve general equations.
Solving Linear Equations
Solving Differential Equations
Processing measurement signals with fast furier implementation
Interpolation operations for processing data in tables
Optimal Problem Solving
Differential Integral
Graphic Tools
Matlab provides programming tools for building application programs
There are also separate application modules for in-depth research such as
Solving Differential Equations of Specific Functions for Solving Persistent
Problems
Simulink allows simulation of machine structures
Stateflow: for the study of gas or liquid flows
Fuzzy logic: the study of fuzzy logic
1.3. Interface of Matlab
1.3.1. Interface
On the main interface window there are several work areas, as well as some smaller
windows:
Toolbar: contains icons that perform a number of normal (Open, New, Save, Cut,
Paste...) and separate functions (Simulink, Guide, Help...).
Command window: This is the most important window, which can allow you to
perform math operations, or link to other windows.
Some sub-windows perform their own functions (notification of the current
directory, through executed command lines, existing variable names,...)
1.3.2. How to write expressions, use variables, function keys
a. Calculate numeric expressions:
Number expressions can be typed directly into the Matlab command window, after
pressing Enter the result of the expression will appear on the screen
Calculations: Addition +, Subtraction -, Multiplication *, Division/, Power ^ and
calculation priority brackets
Long command lines can be split into multiple lines (by pressing Enter), but at the
end of each line (except the last line) we must add a comma...
b. Use functions, constants, and variables
Các hàm: sqrt, sin, cos, tan, cot, sind, cosd, exp, log, log10, asin, acos, atan, acot
Defined constants: eps, realmax, realmin, pi, inf
When writing an expression, the calculation result is stored in a sea with the
default name ans.
Normally, to save the calculation result, we usually use the following syntax
assignment:
<variable name> = <expression>
or <variable name> = <expression>;
In which:
Variable names are set by the user, with no spaces and are case-sensitive, bonus.
in syntax 1: the result of the calculation of the expression will appear on the
screen and stored under the variable name,
in syntax 2: the result does not appear on the screen
To check the current value of the variable we just need to type the name of the
variable
In an expression instead of entering a specific value, we can type the name of the
variable
To check the variables that exist in the Matlab environment we type whos, or whos
To remove the sea from the field, we can use the following commands:
clear, clear <variable name1>, <variable name2>...
c. Function keys
To edit the command lines we use the arrow keys or use the collage function
Backward deletion key, del key, end, pageup, pagedown...
Other common keyboard shortcuts like in application software in Windows:
Ctrl+C, Ctrl+N, Ctrl+O, Ctrl+X,...
Note – Search for functions in the software library: F1 –
Help For example:
c, Jaw eye
Syntax: <Variable name>=eye(<RowNum>, <ColumnNum)
Or: <Variable name>=eye(<size>)
The first syntax will create a rectangular unit matrix, which is: the number of rows is
equal to <RowNum> and the number of columns is equal to <ColumnNum>.
The second syntax that creates the matrix has: number of rows = number of columns =
<size>
VD1: I=eye(3,4)
VD2: I2=eye(3)
d, Hàm diag
Application: Diagonal arrays commonly encountered in hardness matrices and mass
matrices in durability computing
Syntax: <variable name>=diag(v)
<variable name>=diag(v,n)
In which:
v is a row vector, or column, or maybe a matrix
If v is a vector, the function will result in a square matrix with elements on a fixed
diagonal that are molecules of the vector v
If v is a matrix, the result is that the column vector of the elements is on the
diagonal of the matrix v
n is the integer that indicates which diagonal STT of the matrix will place the
elements of the vector v (on the main line is the line 1,2,3,...; below the main line
is the line -1,-2,-3,...)
VD1: v=[1 3 5 3]; M=diag(v) will give the result:
VD4: v=[3 5 2; 2 3 5; 6 7 8]
A=diag(v)
The result will be:
If A is a vector, the function will return the result of the sum of the vector's elements
If A is a matrix, the function will return a row vector with elements that are the sum of the
elements of each column in matrix A
c, Hàm cumsum
Syntax: B = cumsum(A)
B = cumsum(A,dim)
Describe:
If A is a vector, B = cumsum(A) will give the result that vector B has elements
that are the cumulative sum of the elements of vector A.
If A is a matrix, cumsum(A) will give the result that the matrix is the same size as
A, and its elements are respectively the sum of the elements on each column of the
matrix A
B = cumsum(A,dim) It will give the result that matrix B is the same size as A, the
elements of vector B will be the sum of the elements along the row or column of
matrix A, depending on the value of <dim>. For example, cumsum(A,1) will
calculate along the columns, cumsum(A,2) will calculate along the rows.
VD:
x=[1 2 3 4 5 ]; Y=cumsum(x) Result: Y = [1 3 6 10 15]
A = [1 2 3; 4 5 6]; B=cumsum(A) Result: B = 1 2 3
5 7 9
A = [1 2 3; 4 5 6]; C=cumsum(A,2) Result: C=1 3 6
4 9 15
f. Hàm diff
Syntax:
Y = diff(X) : Calculate the deviation between adjacent elements of the X
vector.
Y = diff(X,n) : Gives nth order deviation vector
Describe:
If X is a vector, the result is a vector less than X 1 element:
[X(2)-X(1) X(3)-X(2) ... X(n)-X(n-1)]
If X is a matrix, the diff(X) function gives the result of a column matrix of
deviations,
thus diff(X,2) gives the same result as diff(diff(X)).
VD: VD2:
x = [1 2 3 4 5]; y = diff(x)
Result: y = 1 1 1 1
z = diff(x,2)
Result: z = 0 0 0
3. Graphics in Matlab
3.1. Some graphing commands
Plot functions come in many forms, depending on the
input variables:
If Y is a vector, the PLOT(Y) function will print a
plot Y depending on its index in the vector
If X and Y are two vectors of the same magnitude of
the function PLOT(X,Y), the graph of the function
Y(X) will be printed
For example, to graph a Sine function with a
diaphragm value from 0 to 2 Pi, we use the following
command lines:
X=0:pi/100:2*pi; Y=sin(X); plot(X,Y)
If the argument in the function consists of multiple sets of X, Y, the function will
produce multiple graphs on the screen, and Matlab will automatically display the graphs
with different colors.
Example:
Nếu ta có: y1= sin(X); y2=sin(X-0.25); y3=sin(X-0.5)
The function: plot(x,y1,x, y2,x,y3) will give the graph
set as the drawing
In the plot function, we can also specify the line type, color, and marking style of the
node points with the syntax:
plot(x,y,'color_style_marker')
Where color_style_marker is a set of 1,2 or 3 characters separated by parentheses that
designate the line style, color, and marking style of the button points The letters of color
include: 'c', 'm', 'y', 'r', 'g', 'b', 'w', and 'k'. Corresponding to blue, young brick color,
yellow, red, blue, navy color, white and black.
Line style characters include '-' for streaks, '- -' for
dashes, ':' dotted lines, '-.' tiled lines, and 'none'
when no lines are drawn.
Characters for marking points include: '+', 'o', '*',
and 'x'.
For example, the plot statement(x,y,'y:+') plots a
graph as a yellow dotted line with a + mark
marking the node points.
Heed:
1. The plot command will automatically open a new graphics window. If this
window already exists, the default plot command will use this window. To open a
new window and bring it to date we use the figure command
figure(n) :where n is the number on the window's title bar
2. Draw a new curve to an existing
graph: The hold command allows a
curve to be added to an existing graph. If
you set hold on mode, Matlab will not
delete the existing graph and draw a new
curve when you call the drawing
command.
For example: hold on
plot(X,y1)
plot(X,y2)
plot(X,y3,'g-.')
3. Subplot function: divide the
graphic window into multiple parts to
display multiple graphs on one window
Where m,n,p are integers, it will divide the
graphic window into m rows and n columns and
bring the pth window to the present. The sub-
window index is defined from right to left and
from top to bottom.
subplot(1,3,1); plot(X,y1)
subplot(1,3,2); plot(X,y2)
subplot(1,3,3); plot(X,y3)
3.2. Annotations on graphs
A complete graph must always have the name and full annotations of the graph, here, we
use a few commands:
Syntax:
Title('Graph Name') : Export graph name
xlabel('x-axis name') : Export x-axis name
ylabel('y-axis name') : Exports the y-axis name
Grid on/off : Enable/disable grid lines on the graph
Legend('đ ồ_thị_1', đ ồ_thị_2', ' đ ồ_thị_3') : Annotations for different lines
4. Application of Matlab in Automotive Design Calculations
4.1. Application in automotive theoretical
calculations Give the input parameters as follows:
Number of engine revolutions: n min = 320, n max = 3200
Ratio of the number of hands: i1 = 54.03; i2 = 31.35; i3 = 18.2; i4 = 10.56; i5 = 6.13
Wheel Radius: Rbx = 0.432 (m)
Vehicle Gravity: G=82500(N)
Grip coefficient: f = 0.02
Air resistance coefficient Cw = 0.85
Front area: A = 4.3 m2
Gravitational acceleration: g = 10 m/s2
Maximum power Ne max = 81 (kW)
Some recipes to know:
- Calculate the traction force at the numerical arms:
M e .iti .t
Fki rbx
+ In which:
2 3
n n n
Ne Ne max a e
b e
c
e
(kW)
nN nN nN
104 N
M e (Nm) 1, 047n
e
With: The engine is a 4-stroke diesel engine, direct combustion chamber, so the
coefficients selected are: a= 0.5; b=1.5; c=1.
- Rolling resistance: Ff = G.f
- Air Resistance:
1
𝐹𝑤 𝜌𝐶 𝐴𝑣2
25,92 𝑤
f=0.02;% He so bam;
C=0.85;
Result:
Heed:
Syms commands are commands for declaring symbolic variables, generating
symbolic expressions, and performing symbolic calculations.
ezplot is also a function used to draw graphs
ezplot and plot are two functions in matlab that are used to draw charts. plot is
used to draw simple 2D charts, while ezplot is used to plot curves and more
complex functions.
4.2. Application of matlab in Automotive
Computational Design Calculate the hardness of tweezers with
the initial parameters as follows:
Index of Leaves Width b, cm Tweezers leaf
thickness h, cm
1-6 4,5 0,70
7-9 4,5 0,575
1/2 Length of the tweezers (from the hanging light to the tip of the leaf):
l(1)=68.5; l(2)=67.2; l(3)=57.6; l(4)=50.4; l(5)=43.0; l(6)=35.0; l(7)=28.0; l(8)=20.6;
l(9)=1 3.0
Applicable formula: