1 MATLAB Intro
1 MATLAB Intro
• MATLAB basics
• Arrays: Unlocking potential of MATLAB
• Loops and Execution Control
• MATLAB files: Scripts and Functions
• Program Output and Plotting
1
12/17/15
Indian captain, Mahendra Singh Dhoni, hits a ball with initial velocity of 35
m/s and angle of 45○. If the boundary is at a distance of 75 m, will he score
a six?
• Setting up the problem:
2
12/17/15
Result
MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Animating results
exitCode = ballAnimation(tOut,XOut);
3
12/17/15
Input block
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
Computation
%% Setting up and Solving the problem
X0 = [0; 0; % starting position is the origin
u0; v0]; % starting velocity is given
tSpan = [0 20]; % simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);
Output block
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
(Math) Expression
Calling a function
plot(XOu
Calling a function
4
12/17/15
MATLAB Code
%% Define Parameters and Initial Conditions
param.g = 9.81; % gravitational acceleration
param.kappa = 0.006; % air drag coefficient
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Animating results
exitCode = ballAnimation(tOut,XOut);
• Suppress “echo”
5
12/17/15
6
12/17/15
• We will learn
• Building arrays
• Colon notations
• Array operations and functions
7
12/17/15
Building Arrays
Array Building Functions
• Recall that we can build arrays as: Command Meaning
ones(m,n) Build m×n matrix of 1’s
>> A = [1, 2; 3 4]; zeros(m,n) Build m×n matrix of 0’s
eye(n) Identity matrix
diag(vec) Create diagonal matrix
• We can also build arrays from diag(A) Diagonal elements of A
rand(m,n) Uniform random number array
existing arrays (if correct size):
randn(m,n) Gaussian Random number array
>> B = [b, c]; magic(m) Magic square matrix
hilb Hilbert matrix
8
12/17/15
9
12/17/15
10
12/17/15
11
12/17/15
12
12/17/15
13
12/17/15
MacLaurin Series
14
12/17/15
15
12/17/15
Scope of Variables
• script shares the variables with workspace from where it was called
16
12/17/15
• All other purposes, you are likely to use scripts (instead of functions)
17
12/17/15
• Other options…
• Plotting data
• Using plot command
• Other options…
19
12/17/15
Plotting
• Log-Log plot
20
12/17/15
MODULE – 1
INTRODUCTION TO MATLAB PROGRAMMING
Dr. Niket Kaisare
Department of Chemical Engineering
IIT–Madras
21
12/17/15
Summary of Module-1
• MATLAB basics
Summary of Module-1
• Execution control
• for and while loops
• if-then statements
• MATLAB files
• Plotting in MATLAB
22