0% found this document useful (0 votes)
30 views11 pages

Lab 2 Manual

lab manual

Uploaded by

Jagadish Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views11 pages

Lab 2 Manual

lab manual

Uploaded by

Jagadish Chandra
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

MSE 211 Computational Methods for Engineers 1/11

Lab 2: Optimization

Objectives
In this lab, students will solve two optimization problems using MATLAB. The first problem is an
unconstrained optimization problem where three different algorithms are used to minimize a function.
Students will also learn the importance of the selection of the starting points. The second problem is in
relation to the synthesis or design of mechanisms for motion, function and path generation. Students will
solve these engineering design problems as optimization problems.

Part I. Comparison of Optimization Methods


A common function used in optimization to evaluate the characteristics of an algorithm is Himmelblau's
function (see below). Write a Matlab function called Lab_2_Fun.m that includes Himmelblau's function
with a vector input x = [x0, y0] (in the function x = x(1) and y=x(2)) and an output z = f(x,y).

Himmelblau's function has four local minima. To find them all, you must use different starting points.
Three unconstrained optimization numerical methods will be employed to minimize this function.
Main Code
Create a main code, Lab_2_main.m in which you can call each method and plot the function. Use the
percentage character % before each line to comment the methods that you are not testing.
%Define call function numerically
[x1,y1] = meshgrid(-5:0.1:5,-5:0.1:5);
f = fx(x1,y1);

% Plot surface
surface = figure; figure(surface);
surf(x1,y1,f); shading interp;

x0=[0; 0] %Initial Guess


%Call Methods (Use % before each line to comment the methods you are not testing)
[X,traj,Z,k,Err] = %Call Method

% Add trajectory for finding minima (3D plot)


plot3(traj(:,1), traj(:,2), Z(:,1) ,'-k+')

%Plot contour (2D plot)


contour_graph = figure; figure(contour_graph);
contour(x1,y1,f,100); hold on;
plot(X(1,:), X(2,:),'-k+')
MSE 211 Computational Methods for Engineers 2/11

I.1 Steepest Descent Method.

 Write a code for the steepest ascent method (Lab_2_sdm.m).


𝐱 𝑖+1 = 𝐱 𝑖 − ∇(𝐱 𝑖 )ℎ

where x = [x, y]T, and an approximation of the step size h is given by


𝜕𝑓 𝜕2 𝑓 𝜕2 𝑓
𝜕𝑥 𝜕𝑥 2 𝜕𝑥𝜕𝑦
∇(𝐱)T ∇(𝐱)
ℎ = |∇(𝐱)T 𝐇(x)∇(𝐱)|; with ∇(𝐱) = and 𝐇(𝐱) =
𝜕𝑓 𝜕2 𝑓 𝜕2 𝑓
[𝜕𝑦 ] [ 𝜕𝑥𝜕𝑦 𝜕𝑦 2 ]
You can find the gradient and Hessian by hand or you can use Matlab. Type syms x y in
the command window and the function f=(x^2 + y - 11)^2 + (x + y^2 - 7)^2.
Type dx=diff(f,x) yielding the first component of the gradient. Repeat to find the other
component of the gradient and the elements of the Hessian, e.g., ddx=diff(dx,x.
Create two functions Lab_2_Grad.m and Lab_2_Hess.m in which the input is the x vector
(include both x and y) and outputs are the gradient and the Hessian matrix, respectively.
While iterating use a tolerance error of εs = 10-4. The approximation error is defined as the
norm between the new and old values of vector x, i.e., 𝜀𝑎 = ‖𝐱 𝑛𝑒𝑤 − 𝐱 𝑜𝑙𝑑 ‖. A pseudocode
of the steepest descent is shown below, where x0 is the initial guess and tol is the tolerance.
function [X,traj,f,k,Err] = Lab2_sdm(x0,tol)
k = 0; ea = 1;
X = x0; traj=[];
f(1) = Evaluate function;
Err=NaN;
while ea > tol,
Evaluate Gradient
Evaluate Hessian;
Evaluate Step Size;
xnew=Evaluate Steepest Descent Formula
traj = [traj xnew]; %Store solution in a vector
k = k+1;
ea = norm(xnew-x); %Evaluate error
Err = [Err ea];
x = xnew;
f(k+1) = Evaluate Function
end

The output gives X, the final solution, traj the vector with all the estimated solutions, f the
vector of the function evaluated at each iteration, (k) number of iterations and (Err) the
error evaluated at each iteration.
 Use as four starting points, [0, 0], [-1, 0], and the other two of your choice in such a way that
the other two local minima are found.
 Report for every starting point, a table that includes number of iterations (about 20 iterations
spread around the search), estimated points (x,y), function evaluation (f) and approximate
error after each iteration (Err). Add a picture of the path followed and its contour.
 Discuss the convergence of this method
MSE 211 Computational Methods for Engineers 3/11

I.2 Newton’s Method.


 Write a code for Newton’s method (Lab_2_Newton.m). Simply modify the equation for
xnew from the steepest descent method and erase the step size line.

 Use the same four starting points.

 Report for every starting point, a table that includes number of iterations (about 20
iterations spread around the search), estimated points (x,y), function evaluation (f) and
approximate error after each iteration (Err). Add a picture of the path followed and its
contour.
 Discuss the convergence of this method.

I.3 MATLAB’s Function

 Use MATLAB function fminunc as follows


options = optimoptions('fminunc','GradObj','on','Algorithm','trust-region');
[X,FVAL,EXITFLAG,OUTPUT] = fminunc(@Lab2_Fun,x0)

 Report only the number of iterations, final estimate point (x,y) and function evaluation (f)
for each starting point.

 Discuss the convergence of this method.

Part II. Synthesis of Mechanisms


In this part, mechanism will be designed to accomplish a particular task for motion generation, function
generation and path generation. All three tasks will require the mechanism to pass through four precision
points. Thus the goal is to find the length of all the links, as well as the location of the ground pivots.
Under the laboratory folder on Canvas, download the following Matlab files:
MSE_211_Lab_2_motion_generation.m
MSE_211_Lab_2_function_generation.m
MSE_211_Lab_2_path_generation.m

These files are partially written, this is you cannot execute them as they are, you would have to complete
the files where indicated in the script and execute them
MSE 211 Computational Methods for Engineers 4/11

II. 1 Motion Generation


It is desired to transport a box through four precision points (position and orientation). Shown below is
an illustration of the task that is required:

Figure 1. Definition of Precision Points for Motion Generation

Define Precision Points


Define the precision points (use the same point on the box) and then determine the 2nd, 3rd, and 4th
precision points relative to the 1st precision point. The relative position is δj and the relative rotation αj,
for j=1,2,3. For example,
𝜹𝟏 = 𝑷𝟐 − 𝑷𝟏 𝛼1 = 𝜓2 − 𝜓1
Thus, δ1 represents the position vector from P1 to P2 and α1 between these vectors. Similarly, determine
δ2, α2, δ3 and α3.

Figure 2. Relative Displacement between Precision Points


MSE 211 Computational Methods for Engineers 5/11

Define Optimization Problem


The vector notation that will be used is shown below. The left side (red) and the right side (blue) are
solved independently. Each set of vector Ri involves two components Ri = [ rix, riy ].

Figure 4. Relative Rotation between Precision Points

Figure 3. Vector Representation

In Fig. 3, R2 is the vector that represent the input link in the first precision point. R3’ is the vector that
connects the mobile pin at the end of R2 to the first precision point P1. Similarly, R4 is the vector that
represents the output link in the first precision point. R3’’ is the vector that connect the pin at the end of
R4 to the first precision point P1.
In Fig. 4, the angle αj is the relative displacement of R3 between the configuration in the first precision
point (solid line) and the jth precision point (dashed line). Similarly, βj is the relative displacement of R2
between the first and the jth precision point configurations and γj is the relative displacement of R4
between the first and the jth precision point configurations.
Designing a four-bar mechanism that has to pass through a number of precision points requires solving a
set of equations. For motion generation, each side of the mechanism is solved independently.
Left side analysis:
Shown below is the relative rotation of the left side of the mechanism and its vector representation

Figure 5. Relative Rotation from First Precision Point to jth Precision Point
MSE 211 Computational Methods for Engineers 6/11

The vector representation can be expanded as:


′ ′ ′
𝑓𝑗𝑥 = 𝑟2𝑥 + 𝑟3𝑥 + 𝛿𝑗𝑥 − 𝑟3𝑥 cos(𝛼𝑗 ) + 𝑟3𝑦 sin(𝛼𝑗 ) − 𝑟2𝑥 cos(𝛽𝑗 ) + 𝑟2𝑦 sin(𝛽𝑗 ) = 0
′ ′ ′
𝑓𝑗𝑦 = 𝑟2𝑦 + 𝑟3𝑦 + 𝛿𝑗𝑦 − 𝑟3𝑦 cos(𝛼𝑗 ) − 𝑟3𝑥 sin(𝛼𝑗 ) − 𝑟2𝑦 cos(𝛽𝑗 ) − 𝑟2𝑥 sin(𝛽𝑗 ) = 0

where j=1,2,3, i.e., a set of three equations.


From this set of equations, only the vectors δj and the angles αj, for j=1,2,3, are known. There are seven
unknown variables: R2, R3', β1, β2, β3, with each of the vectors R being defined by two components.
Since there are three sets of equations (j = 1, 2, 3) and all these equations must be equal to zero, an
objective function of the following form can be established,
2 2 2
𝐹 = √(𝑓1𝑥 )2 + (𝑓1𝑦 ) + (𝑓2𝑥 )2 + (𝑓2𝑦 ) + (𝑓3𝑥 )2 + (𝑓3𝑦 )
Estimate starting points for the seven unknowns, i.e.,
′ ′
𝑟2𝑥 = a 𝑟2𝑦 = b 𝑟3𝑥 =c 𝑟3𝑦 =d β1 = e β2 = f β3 = g
where the a, b, c, d , e, f, and g, are the estimated numerical values.
Use fminunc to minimize the objective function F. Note that if F = 0, then the resulting length of the
links will reach the desired precision points.
Right side analysis:
The right side of the mechanism is solved similarly. The rotation of the follower link between the first
and jth precision point is 𝛾𝑗 .
′′ ′′ ′′
𝑔𝑗𝑥 = 𝑟4𝑥 + 𝑟3𝑥 + 𝛿𝑗𝑥 − 𝑟3𝑥 cos(𝛼𝑗 ) + 𝑟3𝑦 sin(𝛼𝑗 ) − 𝑟4𝑥 cos(𝛾𝑗 ) + 𝑟4𝑦 sin(𝛾𝑗 ) = 0
′′ ′′ ′′
𝑔𝑗𝑦 = 𝑟4𝑦 + 𝑟3𝑦 + 𝛿𝑗𝑦 − 𝑟3𝑦 cos(𝛼𝑗 ) − 𝑟3𝑥 sin(𝛼𝑗 ) − 𝑟4𝑦 cos(𝛾𝑗 ) − 𝑟4𝑥 sin(𝛾𝑗 ) = 0
Note that vectors 𝛿𝑗 and angles 𝛼𝑗 are known, and the other variables 𝑟4𝑥 , 𝑟4𝑦 , 𝑟′′3𝑥 , 𝑟3𝑦
′′
and γj , for
j = 1, 2, 3, are unknown.
Estimate starting points for the seven unknowns, and use fminunc to minimize the objective function
G.
2 2 2
𝐺 = √(𝑔1𝑥 )2 + (𝑔1𝑦 ) + (𝑔2𝑥 )2 + (𝑔2𝑦 ) + (𝑔3𝑥 )2 + (𝑔3𝑦 )

Verify that the links pass through the precision points with the same assembly mode, as the solution
may not be feasible, i.e., the mechanism must be reassembled to reach a particular precision point.
MSE 211 Computational Methods for Engineers 7/11

Laboratory Requirements
 Design a mechanism that will move the box as shown in Fig. 1. Use the partially written Matlab
file MSE_211_Lab_2_motion_generation.m

 Write two sets of optimization problems (left and right side of the mechanism). This requires the
estimation of the initial values of the seven variables (R2, R3', β1, β2, β3 for the left side) and (R4,
R3'', γ1, γ2, γ3 for the right side). Also, write the objective functions to be minimized.

 Include in your report the set of initial values that were assumed, a print out of your objective
functions, and a screenshot of the designed mechanism
Note: Make sure your objective functions are equal to zero. Also, this method does not guarantee that
the design mechanism will work. The assembly modes might change among precision points (unfeasible
design).

II. 2 Path Generation


Assume the case that you want to build a walking spider that climbs a staircase. The tip of the leg must
pass through four precision points as a function of the input angle

Figure 6. Definition of Precision Points for Path Generation

In addition, the relative rotation of the input link must move 60º between each precision point, i.e., the
relative angles with respect to the first precision point are β1 = 60º, β2 = 120º, and β3 = 180º. In doing so,
a second leg can be designed to move the spider through the other 180º.

The problem is similar to Motion Generation, however here the left side of the mechanism has to be
solved first.
MSE 211 Computational Methods for Engineers 8/11

Left side analysis:


The vector equations are expanded as:
′ ′ ′
𝑓𝑗𝑥 = 𝑟2𝑥 + 𝑟3𝑥 + 𝛿𝑗𝑥 − 𝑟3𝑥 cos(𝛼𝑗 ) + 𝑟3𝑦 sin(𝛼𝑗 ) − 𝑟2𝑥 cos(𝛽𝑗 ) + 𝑟2𝑦 sin(𝛽𝑗 ) = 0
′ ′ ′
𝑓𝑗𝑦 = 𝑟2𝑦 + 𝑟3𝑦 + 𝛿𝑗𝑦 − 𝑟3𝑦 cos(𝛼𝑗 ) − 𝑟3𝑥 sin(𝛼𝑗 ) − 𝑟2𝑦 cos(𝛽𝑗 ) − 𝑟2𝑥 sin(𝛽𝑗 ) = 0

where j=1,2,3, i.e., there is a set of three equations. Note that vectors 𝛿𝑗 and angles 𝛽𝑗 are known, for
′ ′
j = 1, 2, 3, and the other variables 𝑟2𝑥 , 𝑟2𝑦 , 𝑟3𝑥 , 𝑟3𝑦 and αj , for j = 1, 2, 3, are unknown

Since there are three sets of equations (6 in total) and all these equations must be equal to zero, an
objective function of the following form can be established.
2 2 2
𝐹 = √(𝑓1𝑥 )2 + (𝑓1𝑦 ) + (𝑓2𝑥 )2 + (𝑓2𝑦 ) + (𝑓3𝑥 )2 + (𝑓3𝑦 )
Estimate starting points for the seven unknowns, i.e.,
′ ′
𝑟2𝑥 = a 𝑟2𝑦 = b 𝑟3𝑥 =c 𝑟3𝑦 =d α1 = e α2 = f α3 = g

where the a, b, c, d , e, f, and g, are the estimated numerical values. Use fminunc to minimize the
objective function F. Note that if F = 0, then the resulting length of the links will reach the desired
precision points.

Right side analysis:


For the right side the equations are:
′′ ′′ ′′
𝑔𝑗𝑥 = 𝑟4𝑥 + 𝑟3𝑥 + 𝛿𝑗𝑥 − 𝑟3𝑥 cos(𝛼𝑗 ) + 𝑟3𝑦 sin(𝛼𝑗 ) − 𝑟4𝑥 cos(𝛾𝑗 ) + 𝑟4𝑦 sin(𝛾𝑗 ) = 0
′′ ′′ ′′
𝑔𝑗𝑦 = 𝑟4𝑦 + 𝑟3𝑦 + 𝛿𝑗𝑦 − 𝑟3𝑦 cos(𝛼𝑗 ) − 𝑟3𝑥 sin(𝛼𝑗 ) − 𝑟4𝑦 cos(𝛾𝑗 ) − 𝑟4𝑥 sin(𝛾𝑗 ) = 0

where j=1,2,3. For this side, the α values that were determined in the left side analysis become known
parameters. Thus, vectors 𝛿𝑗 and angles 𝛼𝑗 are known, and the other variables 𝑟4𝑥 , 𝑟4𝑦 , 𝑟′′3𝑥 , 𝑟3𝑦′′

and γj , for j = 1, 2, 3, are unknown.


Estimate starting points for the seven unknowns, and use fminunc to minimize the objective function
G.
2 2 2
𝐺 = √(𝑔1𝑥 )2 + (𝑔1𝑦 ) + (𝑔2𝑥 )2 + (𝑔2𝑦 ) + (𝑔3𝑥 )2 + (𝑔3𝑦 )

Verify that the links pass through the precision points with the same assembly mode, as the solution
may not be feasible.
MSE 211 Computational Methods for Engineers 9/11

Laboratory Requirements
 Design a mechanism that will move the leg of the spider as shown in Fig. 6 while the prescribed
rotation of the input link is β1 = 60º, β2 = 120º, and β3 = 180º. Use the partially written Matlab
file MSE_211_Lab_2_path_generation.m

 Write two sets of optimization problems (left and right side of the mechanism). This requires the
estimation of the initial values of the seven variables (R2, R3', α1, α2, α3 for the left side) and (R4,
R3'', γ1, γ2, γ3 for the right side). Also, write the objective functions to be minimized.

 Include in your report the set of initial values that were assumed, a print out of your objective
functions, and a screenshot of the designed mechanism
Note: Make sure your objective functions are equal to zero. Also, this method does not guarantee that
the design mechanism will work. The assembly modes might change among precision points (unfeasible
design).

II. 2 Function Generation


Assume you want to control the motion of the output link (Link 4) as a function of the input link (Link
2). Let β1 = 60º, β2 = 180º, and β3 = 270º and γ1 = 90º, γ2 = 120º, and γ3 = 210º. Thus, while the input
link travels 60º from configuration 1 to configuration 2, the output link travels 90º.

Figure 7. Definition of Precision Points for Function Generation

In Function Generation, there is a single set of equations (left and right sides are combined). Usually,
vector R2 is also prescribed so the mechanism is scaled. Let R2 = [1, 1]
MSE 211 Computational Methods for Engineers 10/11

Combined analysis:

Figure 8. Vector Representation for Function Generation

The vector representation is expanded as:

𝑓𝑗𝑥 = −𝑟2𝑥 cos(𝛽𝑗 ) + 𝑟2𝑦 sin(𝛽𝑗 ) − 𝑟3𝑥 cos(𝜑𝑗 ) + 𝑟3𝑦 sin(𝜑𝑗 ) + 𝑟4𝑥 cos(𝛾𝑗 ) − 𝑟4𝑦 sin(𝛾𝑗 ) + 𝑟2𝑥 + 𝑟3𝑥 − 𝑟4𝑥 = 0

𝑓𝑗𝑦 = −𝑟2𝑥 sin(𝛽𝑗 ) − 𝑟2𝑦 cos(𝛽𝑗 ) − 𝑟3𝑥 sin(𝜑𝑗 ) − 𝑟3𝑦 cos(𝜑𝑗 ) + 𝑟4𝑥 sin(𝛾𝑗 ) + 𝑟4𝑦 cos(𝛾𝑗 ) + 𝑟2𝑦 + 𝑟3𝑦 − 𝑟4𝑦 = 0

where j=1,2,3, i.e., a set of three equations. Note that vectors 𝑟2𝑥 and 𝑟2𝑦 are defined in order to scale
the mechanism, and the angles 𝛽𝑗 and 𝛾𝑗 are established with the precision points. All the other
variables, 𝑟4𝑥 , 𝑟4𝑦 , 𝑟3𝑥 , 𝑟3𝑦 and 𝜑𝑗 , for j = 1, 2, 3, are unknown.

Since there are three sets of equations (6 in total) and all these equations must be equal to zero, an
objective function of the following form can be established.
2 2 2
𝐹 = √(𝑓1𝑥 )2 + (𝑓1𝑦 ) + (𝑓2𝑥 )2 + (𝑓2𝑦 ) + (𝑓3𝑥 )2 + (𝑓3𝑦 )
Estimate starting points for the seven unknowns, i.e.,
𝑟3𝑥 = a 𝑟3𝑦 = b 𝑟4𝑥 = c 𝑟4𝑦 =d φ1 = e φ2 = f φ3 = g

where the a, b, c, d , e, f, and g, are the estimated numerical values. Use fminunc to minimize the
objective function F. Note that if F = 0, then the resulting length of the links will reach the desired
precision points.

Verify that the links pass through the precision points with the same assembly mode, as the solution
may not be feasible.
MSE 211 Computational Methods for Engineers 11/11

Laboratory Requirements
 Design a mechanism that will control the output link as a function of the input link as shown in
Fig. 7. Use the partially written Matlab file MSE_211_Lab_2_function_generation.m

 Write the optimization problem of the mechanism. This requires the estimation of the initial
values of the seven variables (R3, R4, φ1, φ2, φ3 for the combined analysis) Also, write the
objective function to be minimized.

 Include in your report the set of initial values that were assumed, a print out of your objective
function, and a screenshot of the designed mechanism
Note: Make sure your objective functions results in G = 0. Also, this method does not guarantee that the
design mechanism will work. The assembly modes might change among precision points (unfeasible
design).

You might also like