0% found this document useful (0 votes)
7 views43 pages

Lec1 - Matlab - Basic Operation

This document serves as an introductory lecture on basic operations in MATLAB, covering scalar and matrix operations, as well as vector input and manipulation. It includes examples of mathematical functions, control statements, and practice exercises to reinforce the concepts. The document emphasizes the importance of matrix operations over iterative statements for efficiency.

Uploaded by

puter071421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views43 pages

Lec1 - Matlab - Basic Operation

This document serves as an introductory lecture on basic operations in MATLAB, covering scalar and matrix operations, as well as vector input and manipulation. It includes examples of mathematical functions, control statements, and practice exercises to reinforce the concepts. The document emphasizes the importance of matrix operations over iterative statements for efficiency.

Uploaded by

puter071421
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 43

Lec.

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

- The variable ‘a’ is created in both workspace and variable editor


- The variable ‘a’ is a scalar but also, ‘a’ is 1x1 matrix.

- ‘clc’ -> clear command prompt


- ‘clear all’ -> clear variable space (workspace)
Basic Operation of Scalar
- M-file (any file name is fine)

- The variable ‘a’ is created in both workspace and variable editor


- The variable ‘a’ is a scalar but also, ‘a’ is 1x1 matrix.
Basic Operation of Scalar
- Scalar

Operation Matlab Expression


a+b a+b
a–b a–b
axb a*b
a/b a/b
ab a^b
- Scalar Operation Priority
Operation Matlab Expression
Parenthesis ()
exponentiation ^
multiplication & division *, /, \
addition & subtraction +, -
ab a^b
Basic Operation of Scalar
- Basic Mathematical Function

function description example

Square root

nth root of real number (when x is


negative, n should be odd number)

exponential function (ex)

absolute value

natural logarithms (ln)

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

function description example

if x>0 return +1, if x<0 return -1, if x=0 return 0


Practice

- Calculate following equations:

x=2, y=6+x, x=y+7


4*50+3*100+102-400/2
56*pi/180 % radian
25/(25-1)
x=2, y=6x3+ 4/x
2*(sqrt(5)-1)/(sqrt(5)+1)2-1 x=3, y=7(x1/3)+4x0.58

10*acos(-1) cos(pi) + sin(pi/2)

exp(3) % e3 tand(30)

log10(105) % log105 sind(90)


Practice

- Substitute x=pi/5 and calculate both sides and prove equality.

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

- Radius of circles are given as R1=16, R2=6.5, R3=12, R4=9.5. Determine


the distance of C2C4 using the 2nd cosine law.
- Write the code in a M-file and execute the file using ‘F5’.

c1c2 = r1 + r2, c1c3= r1+r3, c1c4=r1+r4,


c2c3=r2+r3, c3c4=r3+r4

Use acos()
From cosine law:
Practice

- Radius of circles are given as R1=16, R2=6.5, R3=12, R4=9.5. Determine


the distance of C2C4 using the 2nd cosine law.
- Write the code in a M-file and execute the file using ‘F5’.

r1=16; r2=6.5; r3=12; r4=9.5;


c12=r1+r2; c13=r1+r3;c14=r1+r4;
c23=r2+r3; c34=r3+r4;

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 ‘:’

- Usage of colon ‘:’


Basic Operation of Matrix
- Sum of column elements and indicate as ‘row vector’

- 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)

- diagonal matrix: diag(a)


Basic Operation of Matrix
- Inverse: inv(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

- Calculate magnitude and direction of total force:

F1M=400; F2M=500; F3M=700;


Th1=-20; Th2=30; Th3=143;
F1=F1M*[cosd(Th1) sind(Th1)];
F2=F2M*[cosd(Th2) sind(Th2)];
F3=F3M*[cosd(Th3) sind(Th3)];
F_tot=F1+F2+F3;
F_tot_M = sqrt(F_tot(1)^2 +
F_tot(2)^2);
Th=atand(F_tot(2)/F_tot(1));
Vector Input
- row vector

- column vector
Vector Input
- row vector

- vector size: size(a)


Manipulation Matrix
- Matrix Addition & Subtraction

- Matrix Multiplication
Manipulation Matrix
- Matrix division (Inverse Matrix)

Systems of linear equation


AX = B
X = A-1B
Manipulation Matrix
Operation Matlab Exp.
- Matrix operation a+b a+b
a–b a–b
axb a.*b
a/b a./b
ab a.^b
Specially Defined Matrixes in MATLAB
- Empty 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);

% ‘.’ means operation of


equally located elements.
Practice

- Calculate following equations (x=0:0.01:1) and draw graphs:

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.

Alternative if, else if, else,


statement switch

Control Statement Iterative statement while, for

branch statement break, return


Statement (Switch)

- 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

*ctrl + c => escape from infinity loop


Statement (For)
for k= initial value of k: increment: final value of k
- sum of 0 to 100. - multiplication of 1 to 10
For vs. Column Vector

- for statement - Column vector

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

- Determine an integer i that satisfies 12+22+32+…+i2>1000.


n=0;
s=0;
while(s<1000)
n=n+1;
s=s+n^2;
end

- Determine y using statements (hint: if, elseif, else) x=1;


if x>10
y = ln x for x > 10 y = log(x);
y=sqrt(x) for 0 ≤ x ≤ 10 elseif (x>=0)&&(x<=10)
y=exp(x) -1 for x < 0 y = sqrt(x);
else
y = exp(x)-1;
end

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy