You are on page 1of 18

MAE 341

Mechanical Design 1

MATLAB
TUTORIAL
MATLAB
 MATLAB is a software package for doing numerical
computation. It was originally designed for solving
linear algebra type problems using matrices. It’s
name is derived from MATrix LABoratory.

 MATLAB has since been expanded and now has


built-in functions for solving problems requiring
data analysis, signal processing, optimization, and
several other types of scientific computations. It
also contains functions for 2-D and 3-D graphics
and animation.
MATLAB Windows
Current Directory: This is where all
Current Directory
your files from the current directory
are found. You can do file navigation
here.
Command window: This is the
main window. It is characterized by
Edit Window the MATLAB command prompt '>>'.
When you launch the application,
MATLAB puts you in this window. All
Workspace commands are typed here, at the
Command Window MATLAB prompt. Use this window for
matlab operations with single line
commands.
Edit window: This is where you write or edit and save your own programs
in files called 'M-files'. You can use any text editor to carry out these tasks.
Workspace: This subwindow lists all variables that you have generated so
far and shows their type and size.
Graphics window: The output of all graphics commands typed in the
command window is flushed to the Graphics or Figure window, a separate
window with white background color.
Variables
 Variable names are case sensitive.
 Variable names must start with a letter and can be
followed by letters, digits and underscores.
Examples :
>> x = 4;
>> abc_123 = 0.105;
>> 3ab = 26;
Error: Unexpected MATLAB expression

Special Parameters in Matlab


• pi Value of π
• i and j i = j = square root of -1
Matrices in Matlab

 MATLAB treats all variables as matrices. For


our purposes a matrix can be thought of as
an array, in fact, that is how it is stored.
 Vectors are special forms of matrices and
contain only one row OR one column.
 Scalars are matrices with only one row AND
one column

  4 4   5
[ ]
𝐴= 2
6
5
1
𝐵= 8
9 []  
𝐶=[ 4 ]

Ordinary Matrix Vector Scalar


Matrices in Matlab
 A scalar can be created in MATLAB as follows:
>> x = 43;
 A matrix with only one row is called a row vector. A row
vector can be created in MATLAB as follows:
>> y = [22,13,-5]
y= Note the commas
22 13 -5
 A matrix with only one column is called a column vector.
A column vector can be created in MATLAB as follows:
>> z = [12;10;-3]
z=
12
10 Note the semicolons
-3
Matrices in Matlab

 MATLAB treats row vector and column vector very


differently
 A matrix can be created in MATLAB as follows (note
the commas and semicolons)
>> X = [1,2,3;4,5,6;7,8,9]
X=
123
456
789
 Matrices must be rectangular!
Matrices in Matlab
Matrix Addition
Increase all the elements of a matrix by a single value
>> x = [1,2;3,4]
x=
12
34
>> y = x + 5 Matrix dimensions must agree
y=
67 >> z = [4,0.3]
89 z=
Adding two matrices 4 0.3
>> xsy = x + y >> xsz = x + z
xsy = ??? Error using => plus
79 Matrix dimensions must agree
11 13
Matrix Multiplication
Matrix multiplication Element wise multiplication
>> a = [1,2;3,4]; (2x2) >> a = [1,2;3,4];
>> b = [1,1]; (1x2) >> b = [1,½;1/3,¼];
>> c = b*a >> c = a.*b
c= c=
46 11
>> c = a*b 11
??? Error using ==> mtimes
Inner matrix dimensions
must agree.
Element wise Operations
>> a = [1,2;1,3]; Element wise power
>> b = [2,2;2,1]; operation
>> c = a.^2
Element wise division
c=
>> c = a./b
14
c=
19
0.5 1
>> c = a.^b
0.5 3
c=
14
Element wise multiplication
13
>> c = a.*b
c=
24
23
Solve Systems of Linear Equations
𝐶
  3+𝐶 4+𝐶 5=h

2  𝐶 3+3 𝐶 4+4𝐶 5=0


2𝐶
  3+6𝐶 4+20𝐶 5=0
 1 1 1 𝐶3 h
[ 2
2
3
6 ][ ] [ ]
4 𝐶4 = 0
20 𝐶 5 0

A C B

 
𝐴𝐶=𝐵
𝐶= 𝐴 −1 𝐵
 
Solve Systems of Linear Equations
Solve the system using Matlab as follows:
1. Create a new m-file in edit window
2. In m-file, define distance h as symbol
Syms h
3. Write A matrix
A = [1,2,1;2,3,4;2,6,20];
3. Write B matrix
B = [h;0;0];
4. Find the solution (C matrix)
C=inv(A)*B
5. Save and run M-file

Inv: Takes inverse of a matrix


Plotting in Matlab
clc Clears the command window
Clears database
clear
x = 0:0.01:2*pi; Independent variable (x) starts from zero,
increases with 0.01 increments to 2pi
y = sin(x); Define functions in terms of variable x
z=cos(x); Plot y and z on the same figure

plot (x,y,x,z);
xlabel ('X values');
ylabel ('Y values');
title ('Sample Plot');
legend ('Y data’,‘Z data');
grid on;
%This is a comment
Cam Design Using Matlab

1. Follower rises h mm in 90 degrees.


2. It dwells for 90 degrees.
3. It falls in 90 degrees.
4. It dwells for 90 degrees.
Cam Design Using Matlab

3-4-5 Polynomial for Rise

1. Follower dwells for 90 degrees.


2. It rises h mm in 90 degrees.
3. It dwells for 90 degrees.
4. It falls h mm in 90 degrees.
Cam Design Using Matlab
clc t=[theta1,theta2,theta3,theta4]
clear s=[s1,s2,s3,s4];
h=10
beta1=90 plot (t,s, 'LineWidth',4,'color', ‘r')
theta1=0:0.01:90;
s1=0*theta1 xlabel ('\fontsize{20}\it Cam angle
(theta)');
beta2=90 ylabel ('\fontsize{20}\it
theta2=90:0.01:180; Displacement (mm)');
s2=h*[10*((theta2-beta1)/beta1).^3-15*((theta2- grid on;
beta1)/beta1).^4+6*((theta2-beta1)/beta1).^5];
set(gca,'fontsize',20)
beta3=90 axis([0 360 0 11])
theta3=180:0.01:270;
s3=0*theta3+h % title ('Cam s-Diagram');

beta4=90
theta4=270:0.01:360;
s4=h-h*[10*((theta4-beta1-beta2-beta3)/beta4).^3-

15*((theta4-beta1-beta2-beta3)/beta4).^4
Cam Design Using Matlab

You might also like