You are on page 1of 24

College of Engineering

Suez University
MDP2218
Dynamics of Machines
Assoc. Prof. Dr. Mohamed Aziz
Dynamics of Machines
• Total marks (100)
• Final Exam (60) (3 hours)
• Year work (40) = (MT15+Q10+R5+A10)
References
• Kinematics and Dynamics of Machinery by Charles E. Wilson,
Peter Sadler
• Kinematics and Dynamics of Machines by George H. Martin
• Michael M Stanisic - Mechanisms and Machines_ Kinematics,
Dynamics, and Synthesis-Cengage Learning (2015)
1. Introduction to MATLAB®
MATLAB is one of the most widely used examples of engineering software in the
world. The main reason for its wide implementation of common tasks in
engineering (solving systems of equations, plotting) in very easy manner. This
chapter introduce you to some of the basic concepts in MATLAB

1.1- The Command Window


It is a window with a text prompt where you can type commands at the prompt
4
and receive an immediate response.

1.1.1. Use MATLAB as a calculator.


In the Command Window type
>> 2*2

and hit Enter. You’ll get the response


ans =
ans, stores the value of whatever you computed last.

Define variables to carry out the same operation:


>> a=2
a =
2
>> b=2
b =
2
>> a*b
ans =
4

After typing this you should see the variables a and b appear in the Workspace
window,

Workspace panel (window), which lists all the MATLAB variables currently in
memory.
1.1.1. Vector in MATLAB
To enter a vector into MATLAB we use square brackets:
As a row vector:
>> a=[2 4 6]
a =
2 4 6

To access a particular entry in a, you would use the index enclosed in regular
parentheses. In the Command Window type
>> a(3)
ans =
6
Now type
>> a(2)
ans =
4

A column vector:
Semicolons (;) to separate each row and that the semicolons have also appeared in
the Workspace window
>> b = [3;5;7]
b =
3
5
7
The command size() is a built-in MATLAB function that gives the dimensions of
a vector or matrix.

The commutative property in multiplication:


At the Command Window type

Remember that in multiplying a and b, we are multiplying two vectors, not two
numbers.
The dot product
Is defined as
a · b = a1b1 + a2b2 + a3b3

the answer is, indeed,


68.

The outer product

the size of a*b is 1 × 1, in other words a single number (a scalar). The size of b*a,
on the other hand, is 3 × 3.
The transpose operation
To convert a row vector into a column vector we can use the transpose operation,

In MATLAB we perform the transpose using the apostrophe.


>> a'
ans =
2
4
6

To enter a matrix
you would type
>> A =[2 4 6; 3 5 7]
A =
2 4 6
3 5 7

The A matrix has dimension 2 × 3. To access the first row, third column of A, type
>> A(1,3)
ans =
6
1.1- Vector Notation in MATLAB®
Entering vectors one number at a time is fine for small vectors, but there will be
times when we wish to define vectors with hundreds, or even thousands, of entries.
Luckily,

MATLAB has an easy way to do this using the simple colon operator.
Try typing
>> a=0:10
a =
0 1 2 3 4 5 6 7 8 9 10

If we want a different spacing, we place the increment between the first and last
numbers
>> a=0:2:10
a =
0 2 4 6 8 10
Example
The list of odd numbers between 1 and 11.
>> b=1:2:11
b =
1 3 5 7 9 11
Or, even, numbers between 1 and 11.
>> b = a + 1
b =
1 3 5 7 9 11

Square matrix
If A is a square matrix, then A^2 = A*A. to do this use the dot-carat notation
before the operator.

>> c=2.^a
c=
1 4 16 64 256 1024
Placing the dot before the carat (or a multiplication symbol) tells MATLAB to
perform the operation on each element.
1.1- A First Plot
command for plotting in MATLAB is:
plot(c)
After you hit Enter, a new plot window should open that resembles

plot(x,y)
The first argument is a vector of x values on the plot and the second argument is a
vector of the corresponding y values.
1.1- Writing a Simple MATLAB® Script
Create m-file
A script is a series of MATLAB commands that we store in a file called an m-file.

On the Home tab in the Command Window click on the New button and choose
Script from the pulldown menu.
When we are done typing the script, we can hit the Run button (or the F5 key) to
execute it.
The result will be the same, for the most part, as if we had typed each command,
one after the other, in the Command Window.

Comment (percent sign)


A comment is a part of the script that is not executed
a = 2; % set a equal to 2

Clears all
This clears all the variables that are in memory so that you can start with a “clean
slate.”
clear variables; close all; clc
Example
We’ll calculate the sine function in 1° increments
x = 0:360;
theta = x*pi/180;
This multiplies each value in the vector x by π/180. Note that pi is a built-in
constant in
MATLAB. Now that we have theta in radians, we can calculate the sine of theta
y = sin(theta);
theta is a vector with 361 elements; calculating the sine of theta also results in a
vector of 361 elements. Thus, y(1) = sin(0) and y(361) = sin(2π).

Plotting the sine function is simplicity itself:


plot(theta,y)
Change the width of the line
change the plot command to the following
plot(x,y,'LineWidth',2)

Add to the plot are labels for the x and y axes.


Below the plot command add the following lines:
xlabel('theta (degrees)');
xticks(0:60:360);
xlim([0 360])
ylabel('sin(theta)')

Add a title and grid to the plot:


title('The sine function')
grid on

the complete program:


% SinePlot.m
% makes a nice plot of a sine wave from zero to 360 degrees
clear variables; close all; clc
x = 0:360;
theta = x*pi/180;
y = sin(theta);
plot(x,y,'LineWidth',2)
xlabel('theta (degrees)'); xticks(0:60:360); xlim([0 360])
ylabel('sin(theta)')
title('The sine function')
grid on
1.1- Plotting a Filled Square
To create the square we will need to enter the coordinates of the vertices.
To enter this in MATLAB, type the following lines
UnitSquare = 0.5*[-1 1 1 -1 -1;
-1 -1 1 1 -1];
The 0.5 in front scales all the ones inside by one-half. The first line gives the x
coordinates and the second line gives the y coordinates.

plot(UnitSquare(1,:),UnitSquare(2,:),'LineWidth',2)
xlabel('x (m)'); xlim([-2 2])
ylabel('y (m)'); ylim([-2 2])
grid on
axis equal
used to fix stretched in the x or y direction if the x axis is much wider than the y
axis.
axis equal

filled polygon
To make a filled polygon
fill(UnitSquare(1,:),UnitSquare(2,:),'b','LineWidth',2)

The fill command requires that you specify a color (in this case ‘b’ means blue)
right after you give the x and y coordinates.
plot a square with side length 2 centered at the coordinates (1, 1)
define a new matrix, called NewSquare, as follows:
NewSquare = 2*UnitSquare + [1; 1];

% SquarePlot.m
% plots a filled square
clear variables; close all; clc
UnitSquare = 0.5*[-1 1 1 -1 -1;
-1 -1 1 1 -1];
NewSquare = 2*UnitSquare + [1; 1];
fill(NewSquare(1,:),NewSquare(2,:),'b','LineWidth',2)
xlabel('x (m)'); xlim([-2 2])
ylabel('y (m)'); ylim([-2 2])
grid on
axis equal
1.1- A Primitive Animation

To visualize the motion of a mechanism is to animate it


Animate the motion of a projectile being thrown in the air at a 45° angle
% Projectile.m
% animates the path of a projectile
clear variables; close all; clc
t = 0:0.1:15; % vector of times
vx0 = 50; % initial velocity in the x direction
vy0 = 50; % initial velocity in the y direction
g = 9.81; % acceleration of gravity
figure
set(gcf,'Position',[50 50 1200 500])
for i = 1:length(t)
x = vx0 * t(i); % x position
y = vy0 * t(i) - 0.5*g*t(i)^2; % y position
plot(x,y,'o','MarkerFaceColor','b')
axis equal
xlabel('x (m)'); xlim([0 600])
ylabel('y (m)'); ylim([0 150])
grid on
drawnow Drawnow
that tells MATLAB to place everything that is in the graphics buffer onto the plot
window

You might also like