Chapter 1 - Intro To Modeling and Simulation
Chapter 1 - Intro To Modeling and Simulation
Introduction to
Modeling and Simulation
Dr. H. Maverengo
MTR502
Chap1 2
Agenda
• Dynamic Systems
• Modeling of Dynamic Systems
• Introduction to Matlab
Dynamic Systems
Chap1 4
• Static
» Output does not change with time
» Output at any time depends on input at that
time only
• Dynamic
» Output is not instantaneously proportional to
input or disturbance, may continue after input
held constant
» Present output depends on past inputs
Chap1 5
Dynamic Systems in Engineering
Disciplines
• Mechanical systems
• Electrical systems
• Fluid systems
• Thermal systems
• Mixed systems
» Electro-Mechanical
» Fluid-Mechanical
» Thermo-Mechanical
» Electro-Thermal
Modeling
• What is a model?
» Physical models (e.g., scale model)
» Graphs or plots (e.g., time-dependent
behavior)
» Mathematical models
• Modeling
» Identifying physical dynamic effects
» Writing differential equations using
conservation and property laws
» Expressing in differential equations
forms
Chap1 7
Modeling Steps
• Inaccuracies
propagate in each
step (e.g.,
linearization,
ignoring higher
dynamics)
• Iterative
modifications
needed to get
required output
• Example:
modeling steps for
your favorite sport
device
Chap1 9
T 2T 2T 2T T
f (T , , 2 , 2 , 2 )0 f (T , ) 0
t x y z t
Chap1 12
Linear System
Introduction to Matlab
Chap1 16
Outline
Getting Started
Workspace
Command Window
Command History
Chap1 18
Chap1 19
Making Folders
• Click the ‘Make New Folder’ button, and change the name of the
folder. Do NOT use spaces in folder names. In the MATLAB
folder, make a new folder: ME584\MatlabIntro
Chap1 20
MATLAB Basics
Chap1 21
Chap1 22
Help/Docs
• help
The most important function for learning MATLAB on
your own
• To get info on how to use a function:
» help sin
Help lists related functions at the bottom and links to
the doc
• To get a nicer version of help with examples and easy-to-
read descriptions:
» doc sin
• To search for a function by specifying keywords:
» doc + Search tab
Chap1 23
Outline
Scripts: Overview
• Scripts are
collection of commands executed in sequence
written in the MATLAB editor
saved as m-files (.m extension)
Help file
Comments
Possible breakpoints
Chap1 26
• COMMENT!
Anything following a % is seen as a comment
The first contiguous comment becomes the script's help file
Comment thoroughly to avoid wasting time later
Exercise: Scripts
Exercise: Scripts
» disp('Hello World!');
» disp('I am going to learn MATLAB!');
Chap1 29
Outline
Naming variables
• Variable names
first character must be a LETTER
after that, any combination of letters, numbers and _
CASE SENSITIVE! (var1 is different from Var1)
Scalars
Arrays
Row Vectors
• Command window:
• Workspace:
Chap1 35
Column Vectors
• Command window:
• Workspace:
Chap1 36
Matrices
1 2
• Element by element a
» a= [1 2;3 4]; 3 4
» d = [a;b];
» e = [d c];
» f = [[e e];[a b a]];
» str = ['Hello, I am ' 'John'];
Strings are character vectors
Chap1 38
save/clear/load
• Use save to save variables to a file
» save myFile a b
saves variables a and b to the file myfile.mat
myfile.mat file is saved in the current directory
Default working directory is
» \MATLAB
Make sure you’re in the desired folder when saving files. Right
now, we should be in:
» MATLAB\ME584\MatlabIntro
Exercise: Variables
Exercise: Variables
» help clock
» start=clock;
» size(start)
» help datestr
» startString=datestr(start);
» save startTime start startString
Chap1 41
Exercise: Variables
Exercise: Variables
» load startTime
» disp(['I started learning Matlab on ' ...
startString]);
Chap1 43
Outline
• Exponentiation (^)
» 4^2
» (3+4*j)^2
Built-in Functions
Exercise: Scalars
Exercise: Scalars
» secPerDay=60*60*24;
» tau=1.5*secPerDay;
» endOfClass=5*secPerDay
» knowledgeAtEnd=1-exp(-endOfClass/tau);
» disp(['At the end of 6.094, I will know ' ...
num2str(knowledgeAtEnd*100) '% of Matlab'])
Chap1 48
Transpose
• For vectors of real numbers .' and ' give same result
Chap1 49
Element-Wise Functions
Operators: element-wise
4 1 1 1 1 2 3 1 2 3
2 2 2 .* 1 2 3 2 4 6
1 2 3 .* 2 ERROR
1 3 3 3 1 2 3 3 6 9
1 4 4 3 3.* 3 3 3 3
2 .* 2 4
3 1 3
1 2 12 22
3 1.* 3 1 3 1 3 4 .^ 2 2 2
3 4
Can be any dimension
Chap1 52
Operators: standard
4 1 2 1 2 1 2 1 1 1 1 2 3 3 6 9
1 2 3* 2 11 3 4 ^ 2 3 4 * 3 4
2 2 2 * 1 2 3 6 12 18
1 Must be square to do powers 3 3 3 1 2 3 9 18 27
1 3* 3 1 11 3 3* 3 3 3 3
Chap1 53
» secPerMin=60;
» secPerHour=60*secPerMin;
» secPerDay=24*secPerHour;
» secPerMonth=30.5*secPerDay;
» secPerYear=12*secPerMonth;
» secondConversion=[secPerYear secPerMonth ...
secPerDay secPerHour secPerMin 1];
» currentTime=clock;
» elapsedTime=currentTime-start;
» t=secondConversion*elapsedTime';
Chap1 55
» currentKnowledge=1-exp(-t/tau);
» disp(['At this time, I know ' ...
num2str(currentKnowledge*100) '% of Matlab']);
Chap1 57
Automatic Initialization
Automatic Initialization
Vector Indexing
Matrix Indexing
• Picking submatrices
» A = rand(5) % shorthand for 5x5 matrix
» A(1:3,1:2) % specify contiguous submatrix
» A([1 5 3], [1 4]) % specify rows and columns
Chap1 63
Advanced Indexing 1
12 5
c
2 13
» d=c(1,:); d=[12 5];
» e=c(:,2); e=[5;13];
» c(2,:)=[3 6]; %replaces second row of c
Chap1 64
Advanced Indexing 2
Exercise: Indexing
Exercise: Indexing
» [val,ind]=min(abs(knowledgeVec-0.5));
» halfTime=tVec(ind);
» disp(['I will know half of Matlab after ' ...
num2str(halfTime/secPerDay) ' days']);
Chap1 67
Outline
Plotting
• Example
» x=linspace(0,4*pi,10);
» y=sin(x);
1 1
10 x values: 0.8
0.6
1000 x values: 0.8
0.6
0.4 0.4
0.2 0.2
0 0
-0.2 -0.2
-0.4 -0.4
-0.6 -0.6
-0.8 -0.8
-1 -1
0 2 4 6 8 10 12 14 0 2 4 6 8 10 12 14
Chap1 70
Exercise: Plotting
Exercise: Plotting
» figure
» plot(tVec/secPerDay, knowledgeVec);
Chap1 72
Matlab Tutorial
• Matlab tutorials
» http://www.engin.umich.edu/group/ctm/basic/basi
c.html
» http://www.engin.umich.edu/group/ctm/model/mode
l.html
• Tutorials included in Matlab
Chap1 73
Homework 1: chapter 1
•
•
•
•
Chap1 74
References