2. About this Session
• We will coverthe following
topics
• MATLAB basics
• Arrays: Unlocking potential of
MATLAB
3. Starting and Exiting MATLAB
• We will go over starting a MATLABsession, layoutof MATLAB
window, MATLAB editor,etc.
• Also see video“GettingStarted with MATLAB”on MATLAB site
http://in.mathworks.com/videos/getting-started-with-matlab-68985.html
Open and Explain it…
5. MATLAB Code: Main Code
Blocks %% Define Parameters and Initial Conditions
param.g =
9.81;
% gravitational
acceleration
% air drag coefficient
param.kappa =
0.006; u0 =
35*cos(pi/4); v0
= 35*sin(pi/4);
%% Setting up and Solving the problem
X0 = [0; 0;
u0; v0];
tSpan = [0
20];
% starting position is the
origin
% starting velocity is given
% simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [],
param);
%% Displaying the
results figure(1);
plot(XOut(:,1),XOut(:,2
),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
Input
block
Computation
Output
block
6. MATLAB Code: Key Parts
%% Define Parameters and Initial
Conditions
param.g = 9.81;
u0 = 35*cos(pi/
Comment
Assignment
(Math)Expression
[tOut, XOut] = ode45(@bal
Calling a function
plot(XOu
Calling a function
7. MATLAB Code
%% Define Parameters and Initial
Conditions
param.g = 9.81;
% gravitational
acceleration
% air drag coefficient
param.kappa = 0.006;
u0 = 35*cos(pi/4);
v0 = 35*sin(pi/4);
%% Setting up and Solving the
problem
X0 = [0; 0;u0; v0];
tSpan = [0 20];
% starting position is the
origin
% starting velocity is given
% simulation time
[tOut, XOut] = ode45(@ballTrajectoryFun,tSpan,X0, [], param);
%% Displaying the results
figure(1);
plot(XOut(:,1),XOut(:,2),'bo');
xlabel('x (m)'); ylabel('y (m)');
%% Animating results
exitCode = ballAnimation(tOut,XOut);
8. Basic Data Types
• Matlab easily works with
arrays
• Scalars, vectors and arrays
• Assigningvariables
• Row vs. column vectors
• Arrays /Matrices
• Suppress“echo”
• Variables arecase-sensitive
9. Basic MathematicalExpressions
Variable Meaning
pi Number /
eps Machine precision
i Imaginary unit
inf Infinity
NaN Not a Number (e.g., 0/0)
ans Last displayedresult
end Last element of array
realmax Largest real number
intmax Largest integer
Scalar Operations Special
Variables
• + - * / ^
• log, exp
• pow, sqrt
• sin, cos, tan
• asin, acos, atan
• rem, round, ceil, floor
When type functions we use ()… pow2(5)
20. Task 03
Try creating a variable called x with a value
of π/2.
21. Task 04
Try using the sin function to calculate the sine
of x. Assign the result to a variable named y.
22. Task 06
Now try using the sqrt function to calculate the
square root of -9. Assign the result to a
variable named z..
CREATE pi = 9;
Clear the pi variable
23. Arrays are the most powerful aspect of
MATLAB
•We will learn
•Building arrays
•Colon notations
•Array operations and functions
•Also view “Working with Arrays in
MATLAB” on MATLAB website:
http://in.mathworks.com/videos/working-with-arrays-in-matlab-
69022.html
24. Building Arrays
Create random matrix (a = rand(6,4))
Indexing of an array (a(1,2))
Assign a value to the array elements
(a(1,2)=2;)
Assign an empty matrix a=[];
Create an Identity matrix of c =eye(3);
Combine two matrix C = horzcat(A1,...,AN)
(same rows)
25. Building Arrays
Manually crate arrays a= [1 2 3 ; 1 2 3]
Transpose of a matrix a’
Elemental operation a(2,1) select that element
Size of a matrix size (a)
Ones(m,n)
diag(a)
28. Task 02
Create an array named x with two elements in
a single row:
7 and 9.
29. Task 03
Now create an array named x with two
elements, 7 and 9, in a single column. Try
recalling the previous command and changing
the space between the numbers to a
semicolon (;).
30. Task 04
Now try creating a 1-by-3 row vector
named x that contains the values 3, 10,
and 5 in that order.
31. Task 05
Now try creating a 3-by-1 column vector
named x that contains the values 8, 2, and -
4 in that order..
32. Task 06
Try creating a matrix named x with the values
shown below.
5 6 7
8 9 10
33. Task 07
Try creating a 1-by-2 row vector named x that
contains sqrt(10) as its first element
and pi^2 (π2) as its second element.
34. Task 08
Try creating a row vector named x that
contains the values 1, 2, and 3, in that order.
35. Task 09
Try recreating the row vector named x (still
with values 1, 2, and 3), but this time using
the : operator.
36. Task 10
Try creating a row vector named x that starts
at 1, ends at 5, and each element is
separated by 0.5.
37. Task 11
Try creating a row vector named x that starts
at 3 and ends at 13, with each element
separated by 2.
38. Task 12
Try creating a row vector named x that starts
at 3 and ends at 13, with each element
separated by 2.
39. Task 13
Try creating a row vector named x that starts
at 1, ends at 10, and contains 5 elements.
x = linspace(0,1,5)
start
End
Number of Components
40. Task 14
Transpose x from a row vector to a column
vector using the transpose operator.
41. Task 15
In a single command, create a column vector
named x that starts at 5, ends at 9 and has
elements that are spaced by 2.
42. Task 16
Try creating a variable named x that is a 5-by-
5 matrix of random numbers.
rand
43. Task 17
Try using rand to create an array that
contains 5 rows and 1 column. Assign the
result to a variable named x.
rand
44. Task 18
Now try using the zeros function to create a
matrix of all zeros that has 6 rows
and 3 columns (6-by-3). Assign the result to a
variable named x.
rand
45. Indexing into Arrays
Task 20
Create data variable contains 7,4 random
values
Try creating a variable v that contains the
value in the 6throw and 3rd column of the
variable data.
46. Task 21
Info: You can use the MATLAB
keyword end as either a row or column index
to reference the last element.
>> x = A(end,2);
Now try using the end keyword to obtain the
value in the last row and 3rd column of the
variable data. Assign this value to a variable
named v.
rand
48. Task 01
Try creating a vector vs that is the sum of the
vectors v1and v2.
rand
49. Task 02
Try creating a variable va that contains the
value vs divided by 2
Matrix multiplication
Elemental multiplication
rand
50. Task 03
Info: Basic statistical functions in MATLAB
can be applied to a vector to produce a single
output. The maximum value of a vector can
be determined using the max function.
>> xMax = max(x)
Try creating a variable vm containing the
maximum of the va vector.
rand
51. Task 04
Using the round function, create a variable
named vrwhich contains the rounded average
volumes, va.
rand
52. Task 04
The .* operator performs elementwise
multiplication and allows you to multiply the
corresponding elements of two equally sized
arrays.
z = [3 4] .* [10 20]
z = 30 80
rand
53. Task 05
Try creating a variable
named dsize containing the size of a matrix.
rand
55. Solutions to Systems of Linear Equations
Example: a system of 3 linear equations with 3 unknowns (x1, x2, x3):
3x1 + 2x2 – x3 = 10
-x1 + 3x2 + 2x3 = 5
x1 – x2 – x3 = -1
Then, the system can be described as:
Ax = b
1
1
1
2
3
1
1
2
3
A
3
2
1
x
x
x
x
1
5
10
b
Let :
Task 01
56. Solutions to Systems of Linear
Equations (con’t…)
Solution by Matrix Inverse:
Ax = b
A-1Ax = A-1b
x = A-1b
MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = inv(A)*b
x =
2.0000
1.0000
2.0000
Answer:
x1 = 2, x2 = 1, x3 = 2
• Solution by Matrix Division:
The solution to the equation
Ax = b
can be computed using left division.
Answer:
x1 = 2, x2 = 1, x3 = 2
MATLAB:
>> A = [ 3 2 -1; -1 3 2; 1 -1 -1];
>> b = [ 10; 5; -1];
>> x = Ab
x =
2.0000
1.0000
2.0000
57. Task 02
•Consider the following example (Marks earned by students)
Name Math Programmin
g
Thermodyna
mics
Mechanics
Amit 24 44 36 36
Bhavna 52 57 68 76
Chetan 66 53 69 73
Deepak 85 40 86 72
Elizabeth 15 47 25 28
Farah 79 72 82 91