Lab 2 Manual
Lab 2 Manual
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.
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;
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
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.
Report only the number of iterations, final estimate point (x,y) and function evaluation (f)
for each starting point.
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
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
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).
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
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.
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𝑦′′
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).
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:
𝑓𝑗𝑥 = −𝑟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).