Page 1 of 4

MME 213: Computational Methods for Engineers

Department of Mechanical and Manufacturing Engineering

Miami University

Laboratory Exercise #2

Introduction to Functions and Loops in Matlab

Objective: This laboratory exercise will introduce Matlab functions as a computational tool for engineering design and analysis.

I. Matlab – functions

Function files are m-file which starts with the word function. They can accept inputs arguments and return outputs. In engineering analysis and design it is essential to use such programming structures to facilitate cause and effect information based on the model.

Let us consider the following problem:

1.1 A simple function

Consider the following equations:

cxybatx????)cos(

.

The goal of this exercise is to create a function in Matlab which reads user input for the values of a, b, c, and t and evaluates the values for x and y. Open a new m-file and write the following code and save it as computexy.m.

function [x , y] =computexy(t,w)

% w contains a,b,c

a= w(1);

b=w(2);

c=w(3);

x=cos(a*t)+b;

y= abs(x) +c;

Try it:

[u,v]=computexy(pi/4, [1 1.2 1.4] )

1.2 Using function in a loop

The function developed in Ex. 1.1 (computexy.m) can be used in another Matlab m-file if needed. Actually this approach is utilized often for functions. So in this exercise create a matlab m-file as follows and save it as loopfunction.m . This will use the final form of computexy.m from the previous exercise.

n=4;

c=linspace(1,1.4,n)

for k=1:n

[u,v]=computexy(0:pi/4:pi, [c(k) sqrt(1.8/(1+k)^3) 1/0.85] )

end

Also use whos command and find out the size of u and v (the results of the above script). Create and label the appropriate plot of the result.

Page 2 of 4

II. Matlab – decisions

The simplest m-files perform instructions sequentially. That is the program statements are computed line-by-line from the top to the bottom of the file. Decisions (or selections) allow the branching of flow based on a decision. One example of a decision structure is based on the if-statement. This structure allows for flow based on a logical condition.

if condition

Statements

end

condition is a logical expression which is either true or false. Statements are only evaluated if the condition is true. For example, consider the following modification of the computexy.m function file.

function [x , y] =computexy(t,w)

% w contains a,b,c

if t > 0

a= w(1);

b=w(2);

c=w(3);

x=cos(a*t)+b;

y= abs(x) +c;

end

The above modification will restrict the program to only calculate the values of x,y if t is greater than zero.

Other logical conditions include: equal (==), Not equal (~=), less than (<), less than or equal to (<=) and greater than or equal to (>=). Two other forms of decision structures are available.

Engineering Applications:

Exercise 1: Slider Crank Mechanism

The word mechanism has many meanings. In kinematics, a mechanism is a means of transmitting, controlling, or constraining relative movement. Movements which are electrically, magnetically, pneumatically operated are excluded from the concept of mechanism. The central theme for mechanisms is rigid bodies connected together by joints. A machine is a combination of rigid or resistant bodies, formed and connected. They move with definite relative motions and transmit force from the source of power to the resistance to be overcome. A machine has two functions: transmitting definite relative motion and transmitting force. These functions require strength and rigidity to transmit the forces.

This mechanism is composed of three important parts: The crank which is the rotating disc, the slider which slides inside the tube and the connecting rod which joins the parts together. As the slider moves to the right the connecting rod pushes the wheel round for the first 180 degrees of wheel rotation. When the slider begins to move back into the tube, the connecting rod pulls the wheel round to complete the rotation. One of the best examples of a crank and slider mechanism is a steam train. Steam pressure powers the slider mechanism as the connecting rod pushes and pulls the wheel around. The cylinder of an internal combustion engine is another example of a crank and slider mechanism

Figure: Slider Crank Mechanism

Page 3 of 4

The displacement x of the slider of the slider crank mechanism is given by

Develop a function file called slidercrank.m which has inputs of a, l, and and return a result for the value of x. Use this function slidercrank.m in another m-file (called useslidercrank.m) to evaluate the value of the displacement for the following parameters: a=1, l=1.5 and from 0 to 180 degrees. Plot the result obtained.

Exercise 3:

Page 4 of 4

Laboratory Assignment: Lab 2: Report (20 points) and Lab Quiz (5 points)

Objective of the report: This report will assess what you have learned about Matlab as well as the specific topics related to the exercises in this lab.

Task at hand: Create and submit a concise report on your approach to the solution of and the results obtained for Exercise 1 and 2.

Report Format: Page limit is 5 pages (including title page). Points will be deducted for exceeding page limit. This report should have following sections:

1. Title Page: This should contain the name of the class, class number, section, your name, lab report number, date submitted.

2. Problem Solutions/Results: This section of the report will address the specific solutions for exercise 2 and 3 conducted during this lab. For each problem the following items are required:

a. Matlab code used.

b. Final result (this could be a plot/graph or data or both) and its explanation/discussion.

Further, explain the Matlab code used for Exercise 3 so that a student who is just starting EAS 102 can understand.

3. Conclusions/Comments: In this section please write what you liked and what topic was most difficult to understand. Also specify the courses in the curriculum where this might be useful in future. Also, any suggestions you have for improving this lab would be appreciated.

TO GET YOUR ASSIGNMENTS DONE AT A CHEAPER PRICE,PLACE YOUR ORDER WITH US NOW