Lec1 - Matlab - Basic Operation
Lec1 - Matlab - Basic Operation
1 Basic Operation
서울과학기술대학교 기계자동차공학과
한상조, 김진현, 정광필, 강수민
https://www.youtube.com/watch?
Introduction to MATLAB v=joilU9m-sNk
MATLAB
- Abbreviation of ‘MATrix LABoratory’
- Program for Matrix and Vector calculation
Features
- Compatible with ‘C’ and ‘Fortran’
- Easy to use for beginners
- Large number of Tool Box
- Graphic functions
- Strong to vector and matrix
How to use…
- Make a m-file
- MATLAB prompt
- ‘Ctrl + c’ stop running
- case-sensitive
Scalar Operation
Basic Operation of Scalar
- Command Prompt
Square root
absolute value
log
factorial
Basic Operation of Scalar
- Basic Mathematical Function
function description example
radian x
degree x
radian x
degree x
radian x
degree x
radian x
degree x
exp(3) % e3 tand(30)
x=pi/5;
(cos(x/2))^2
(tan(x)+sin(x))/(2*tan(x))
Practice
- There’s an object in a room and the object has the initial temperature of T0.
Then, the temperature of the object changes according to T ,
where T is current temp., Ts is room temp., k (0.45) is a constant, and t (hour)
is time.
- Assume that a beer (initially 50˚C) is moved to a fridge (4 ˚C). Determine the
temperature of the beer after 3 hours.
Ts=4;
T0=50;
k=0.45;
t=3;
T=Ts+(T0-Ts)*exp(-k*t)
Practice
Use acos()
From cosine law:
Practice
gamma1 = acosd((-
c23^2+c12^2+c13^2)/(2*c12*c13));
gamma2 = acosd((-
c34^2+c13^2+c14^2)/(2*c13*c14));
c24 = sqrt(c12^2+c14^2-
2*c12*c14*cosd(gamma1+gamma2))
Matrix Operation
Basic Operation of Matrix
- use comma (,)
- use space ( )
Basic Operation of Matrix
- Approach to an element 1
- Approach to an element 2
Basic Operation of Matrix
- Approach to an element 3
- Approach to an element 4
Basic Operation of Matrix
- Usage of colon ‘:’
- Determinant of a matrix
Basic Operation of Matrix
- transpose: transpose(a) or a‘
- eigenvalues: eig(a)
Basic Operation of Matrix
- trace: sum of diagonal elements -> trace(a)
- rank
Practice
- Calculate a column vector X such that satisfies following equation. Use inv().
x=
>> a=[1 -1 2; 3 2 9; 0 1 -4]; 2.2609
>> b=[1;-2;3]; -0.4783
>> x=inv(a)*b -0.8696
- Calculate the matrix A such that satisfies following eigenvalue and eigenvector:
AV1 = λ1V1
2
AV2 = λ2V2
A=[λ1V1 λ2V2][V1 V2]-1
Practice
- column vector
Vector Input
- row vector
- Matrix Multiplication
Manipulation Matrix
- Matrix division (Inverse Matrix)
- zero matrix
- matrix of ones
Practice
- Define a row vector t and calculate x(t) = e-0.5tsin(5t)
- Plot x(t) as following:
plot(t,x); %plot(x,y)
xlabel('time(s)');
ylabel('x(t)');
title('x(t) calculation');
grid on;
Practice
- Define a row vector t and calculate x(t) = e-0.5tsin(5t)
- Plot x(t) as following:
T=0:0.1:10;
x=exp(-0.5*T).*sin(0.5*T);
y = 2x+10
y=6x3+ 4/x
y=7(x1/3)+4x0.58
alternative, iterative, branch statement
MATLAB Control Statement
- MATLAB is a procedure-oriented language.
- switch
Statement (if)
- if
Statement (if, else)
- else if
Statement (if, elseif, else)
- if, elseif, else
Statement (While)
- sum of 0 to 100. - multiplication of 1 to 10
In MATLAB, using matrix operation is preferred since loop time of iterative statement is
much longer. The calculation speed is even ten times slower than matrix operation.
Practice