0% found this document useful (0 votes)
57 views17 pages

r0737973 - Lab Session Unconstrained & Constrained Optimization

Uploaded by

krnc
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)
57 views17 pages

r0737973 - Lab Session Unconstrained & Constrained Optimization

Uploaded by

krnc
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/ 17

FACULTY OF ENGINEERING TECHNOLOGY

DE NAYER (SINT-KATELIJNE-WAVER) CAMPUS

Lightweight Design
Lab 1 – Sizing optimization of the cantilever beam

Nicolas Kraus – r0737973

Supervisors: Master’s Course


Lab: Y. Konstantinos
Theory: Prof. D. Moens

Academic Year 2023-2024


Content

1 Problem formulation ................................................................................ 2


1.1 Derivation of the functions ................................................................. 3
2 Execution and Results ............................................................................. 4
2.1 Visualizing the design space in 3D .................................................... 6
3 Appendix................................................................................................... 8
A Optimization code for all three approaches ....................................... 8
B Code for visualization in three-dimensional space ........................... 10
4 Bibliografie ............................................................................................. 14

List of figures

Figure 1 - Drawing of the cantilever beam, from the assignment paper. ................................ 2
Figure 2 - Typical notation of an optimization problem (Martins & Ning, 2020). ..................... 2
Figure 3 - Gradient and eigenvalues of the Hessian of the final design point. ........................ 5
Figure 4 ................................................................................................................................. 5
Figure 5 - Weak minima. ....................................................................................................... 5
Figure 6 - Error message associated with incorrect design point. .......................................... 5
Figure 7 - Design space in 3D, t and w are fixed. .................................................................. 6
Figure 8 - Contour plot of the (active) deflection constraint, and the gradients. ...................... 6
Figure 10 - The gradient can be seen to be much better. ...................................................... 7
Figure 9 - Enlarged design space, yielding better values. ...................................................... 7
Figure 11 - 3D visualization of the incorrect final design point, t and w are again fixed. ......... 7

List of tables

Table 1 - Results for both using one design point and LHS. .................................................. 4
Table 2 - Second, incorrect result obtained from the LHS method. ........................................ 5
Table 3 - Results for the global search. ................................................................................. 6

1
1 PROBLEM FORMULATION

Given an I-beam subject to a load as shown in Figure 1. Our task is to minimize the mass of
the beam by finding the best values for the beam cross sectional dimensions while still obeying
the given constraints. To fill in the blanks in the required values, I will assume the steel is from
the series S235. The fixed parameters are:
F = 10 kN; L = 3000 mm; E = 210.000 N/mm²; ρ = 7.85 ∗ 10−6 kg/mm³;
The design variables are free to be optimized between the given bounds:
10 ≤ 𝑙, 𝑏 ≤ 250 [𝑚𝑚]; 1 ≤ 𝑡, 𝑤 ≤ 50 [𝑚𝑚];
While obeying the given constraints:
𝛿𝑚𝑎𝑥 = 5 𝑚𝑚; 𝜎𝑚𝑎𝑥 = 235 𝑁/𝑚𝑚²
(motivated by personal preference, I changed the units from m to mm)

Figure 1 - Drawing of the cantilever beam, from the


assignment paper.

Balance of forces and moment:


∑ 𝐹𝑥 = 0 ⟶ 𝐹𝑥 = 0
∑ 𝐹𝑦 = 0 ⟶ 10000 − 𝐹𝑦 = 0 ⟺ 𝐹𝑦 = 10000 𝑁
∑𝑀 = 0 ⟶ 10000 ∗ 3000 − 𝑀 = 0 ⟺ 𝑀 = 3 ∗ 107 𝑁𝑚𝑚
To satisfy the notation that our MATLAB-code expects us to use, we need to transform the
problem into an optimization model. it is described following the general expression of a
constrained optimization problem:

Figure 2 - Typical notation of an optimization


problem (Martins & Ning, 2020).

With 𝑥
⃗⃗⃗ (= x in figure 2) being the design vector that represents one specific design in the model
space. In this case, 𝑥 ⃗⃗⃗ consists of four variables:
⃗⃗⃗ = [𝑙, 𝑏, 𝑡, 𝑤] = [𝑥1 , 𝑥2 , 𝑥3 , 𝑥4 ]
𝑥 (1.1)

2
1.1 Derivation of the functions

1.1.1 Minimization function


The mass of the beam must be minimized, with the cross-sectional area containing all the
design variables.
𝑓(𝑥 ) = 𝑚(𝑙, 𝑏, 𝑡, 𝑤) = 𝜌 ∗ 𝑉(𝑙, 𝑏, 𝑡, 𝑤) = 𝜌𝐿 ∗ 𝐴(𝑙, 𝑏, 𝑡, 𝑤) [𝑘𝑔] (1.2)

With the cross section of the beam defined as:


𝐴(𝑙, 𝑏, 𝑡, 𝑤) = 𝑡(𝑙 − 2𝑤) + 2𝑏𝑤 [𝑚𝑚²] (1.3)

And thus, yielding the function to minimize:


𝑚(𝑙, 𝑏, 𝑡, 𝑤) = 𝜌𝐿 ∗ [𝑡(𝑙 − 2𝑤) + 2𝑏𝑤] (1.4)

1.1.2 Inequality constraints


Both the deflection and tensile stress are constrained. Both constraints contain the second
moment of inertia 𝐼𝑥𝑥 , which contains the design variables, and determines both constraints to
a large extent. Since both functions are dependent on the same design variables as the
optimization function, we can simply reuse 𝑥 again. The deflection of the beam can be defined
using the Euler-Bernouilli beam theory:
𝐹𝐿3
𝑔1 (𝑥 ) = 𝛿(𝑥 ) = ≤ 𝛿𝑚𝑎𝑥 = 5 [𝑚𝑚] (1.5)
3𝐸𝐼𝑥𝑥

I choose to neglect the shear stress at the clamping caused by the transverse force 𝐹𝑦 , as its
influence will be negligible. The formula describing the maximum stress is then:
𝑀 𝑙
𝑔2 (𝑥 ) = 𝜎(𝑥 ) = 𝐼 𝑏 ∗ 2 ≤ 𝜎𝑚𝑎𝑥 = 235 [𝑁/𝑚𝑚²] (1.6)
𝑥𝑥

It is important to note that the applied constraint on stress is defined as the tensile stress at
the top of the beam, specifically at the location of clamping into the wall. In this scenario, this
tensile stress represents the maximum total stress in the beam. However, in cases where
additional loads, such as torsional moments, are applied on top of the normal load, the
constraint should be defined using the Von Mises stress. This is because the maximum stress
may no longer occur at the clamping point.
For simplicity, let’s isolate the derivation of 𝐼𝑥𝑥 . We can see the cross-section as a combination
of three rectangles, allowing us to apply Steiner's theorem:
𝑡(𝑙−2𝑤)3 𝑏𝑤 3 𝑙−𝑤 2
𝐼𝑥𝑥 (𝑥 ) = ∑ 𝐼𝑖 + ∑ 𝐴𝑖 ∗ 𝑒𝑖2 = 12
+2[ 12
+ 𝑏𝑤 ∗ ( 2
) ] [𝑚𝑚4 ] (1.7)

Implementing this in both 𝑔1 (𝑥) and 𝑔2 (𝑥 ), and transforming the inequality constraints so that
they comply with the expected form, defined in Figure 2.
−1
𝐹𝐿3 𝑡(𝑙−2𝑤)³ 𝑏𝑤 3 𝑙−𝑤 2
𝑔1 (𝑥 ) = [ 12 + 2[ + 𝑏𝑤 ∗ ( ) ]] − 𝛿𝑚𝑎𝑥 ≤ 0 (1.8)
3 12 2

−1
𝑙 𝑡(𝑙−2𝑤)3 𝑏𝑤 3 𝑙−𝑤 2
𝑔2 (𝑥 ) = M𝑏 ∗ 2 [ 12
+2[ 12
+ 𝑏𝑤 ∗ ( 2
) ] ] − 𝜎𝑚𝑎𝑥 ≤ 0 (1.9)

3
1.1.3 Bounded constraints
These were already defined earlier in the problem formulation, as previously discussed.
10 ≤ 𝑙, 𝑏 ≤ 250 [𝑚𝑚] (1.10)

1 ≤ 𝑡, 𝑤 ≤ 50 [𝑚𝑚] (1.11)

2 EXECUTION AND RESULTS

The algorithm applied is 'fmincon', which utilizes an interior penalty method, specifically
'interior-point' (MATLAB Help Center - fmincon, sd). It transforms the constrained problem into
an unconstrained one by creating a barrier in the function 𝑓(𝑥 ) at the location where the
gradient of the constraint function 𝑔j (𝑥 ) would become zero. The ‘interior’ in its name means
that the optimized design point must remain within the feasible design region (Martins & Ning,
2020).
Our task is to find an optimum using three different approaches, which are:
1. Choose an appropriate initial design point, optimize from there on;
2. Use the Latin Hypercube Sampling (LHS) method to generate 50 different design points,
increasing the likelihood of finding a better optimum;
3. Employ a global search function.
When choosing an initial point, I make sure that it is in the feasible design region, although the
interior point algorithm should work for infeasible starting points. For simplicity, I choose the
𝑥0 = [250, 250, 50, 50]. The optimum obtained from this point is the same as the
starting point ⃗⃗⃗⃗
one obtained using LHS:
Table 1 - Results for both using one design point and LHS.

l b t w Mass Stress Deflection


250 mm 250 mm 1 mm 11.9551 mm 146.0957 kg 43.75 N/mm² 5 mm

From the design results, it is evident that the deflection constraint serves as the active
constraint, dictating the minimal design point, while the stress constraint remains inactive.
Since the deflection constraint reaches its limit, being zero, the design is indeed optimized.
But the design variables end up at the edge of the feasible design space, suggesting that a
real optimum has not been attained. For a design point to be considered a local minimum, it
should satisfy the sufficient optimality conditions: having a gradient of zero and a Hessian that
is positive-definite (its eigenvalues should be greater than zero).
When operating within a constrained design space, an optimum can be reached when the
gradients of the constraint function and the objective function are aligned, as a true local
minimum might not be feasible due to the imposed constraints. To illustrate this concept, we
can utilize the derivation of the Lagrange multiplier method for equality constraint optimization,
as an inequality constraint behaves similarly at its boundary. Given the newly created
Lagrange function for multiple constraints:
𝑗
Λ(𝑥1 , … , 𝑥𝑖 , 𝜆1 , … , 𝜆𝑗 ) = 𝑓(𝑥1 , … , 𝑥𝑖 ) − ∑𝑘=1 𝜆𝑘 ∙ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.1)

4
Suppose there is only one active constraint, as is the case in this exercise. We can simplify
the equation to:
Λ(𝑥1 , … , 𝑥𝑖 , λ) = 𝑓(𝑥1 , … , 𝑥𝑖 ) − 𝜆 ∙ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.2)

For a point of the Lagrange function to be considered as an optimum, its derivative it should
equal zero, giving:
⃗ Λ(𝑥1 , … , 𝑥𝑖 , λ) = 0 ⟺ ∇
∇ ⃗ 𝑓(𝑥1 , … , 𝑥𝑖 ) − λ ∙ ∇
⃗ 𝑔(𝑥1 , … , 𝑥𝑖 ) = 0 (2.3)

⃗ 𝑓(𝑥1 , … , 𝑥𝑖 ) = λ ∙ ∇
∇ ⃗ 𝑔(𝑥1 , … , 𝑥𝑖 ) (2.4)

(De Samblanx, 2021)


This means that when working within constrained design spaces, a minimum is reached when
the gradients align with each other, given a 'scaling' value λ. This equation still holds true when
a true local minimum is found, as can be seen by the gradient of 𝑓(𝑥 ) equaling zero when λ is
zero. This will be visually examined in a 3D space in the next chapter.
The gradient, as seen in Figure 3, is nonzero in every direction, indicating that the obtained
design point is indeed not a true minimum. The eigenvalues of the Hessian show that it is close
to being positive-semidefinite, suggesting that the function has a shape that is comparable as
the three-dimensional shape shown in Figure 5. When the bounded constraint for ‘l’ is loosened
to [10, 2500] and ‘b’ to [1, 250], the gradient can then be observed to approach zero
(Figure 4).

Figure 3 - Gradient and eigenvalues of the Hessian of the final design point. Figure 4

Most of the starting points from LHS ended up at this optimum, but some of the optimized
designs using LHS yield other results, shown in Table 2. These resulting dimensions are
physically impossible, rendering this design meaningless. Additionally, the deflection
constraint is not satisfied. When obtaining this result, MATLAB provides the response shown
in Figure 6. MATLAB suggests using 'feasibility mode' to resolve the problem. However, this is
unnecessary because the LHS method is already applied, and the problem is successfully
tackled.
Table 2 - Second, incorrect result obtained from the LHS method.

l b t w Mass Stress Deflection


10 mm 250 mm 1 mm 50 mm 586.6305 kg 9.9026 N/mm² 28.2931 mm

Figure 5 – Weak minima. Figure 6 - Error message associated with


incorrect design point.

5
When a global minimum is searched using ‘GlobalSearch’ and ‘createOptimProblem’, the
results shown in Table 3 are again physically meaningless, although mathematically the mass
is indeed minimized. This issue could be solved by defining additional constraints on the
dimensions of the beam, ensuring the discovery of a feasible solution.
Table 3 - Results for the global search.

l b t w Mass Stress Deflection


10 mm 10 mm 50 mm 50 mm -82.4250 kg -61.7496 -176.4274
N/mm² mm

2.1 Visualizing the design space in 3D


Since the original design space exists in a five-dimensional space (four variables and f(x)), it
cannot be visualized graphically, making it difficult to comprehend what is truly happening.
Plotting the design problem in a three-dimensional space by fixing two variables can provide
insight. By fixing the values for 't' and 'w' at the obtained optimized values, we create an
optimization space with the free variables 'l' and 'b'. Running this algorithm yields the same
optimum as attained for the first two approaches.
Looking at Figure 7, the actual design space that fmincon creates is not shown, only the
minimization function (blue) and both constraints (green and pink) are shown as defined
earlier. The actual design space, generated by fmincon, would resemble the blue design space
with barriers rising when a gradient of zero in an active constraint is approached. In this figure,
it can be seen that the stress constraint is indeed inactive across the full design space.

Figure 7 - Design space in 3D, t and w are fixed. Figure 8 - Contour plot of the (active)
deflection constraint, and the gradients.

As observed from the contour lines shown in Figure 8, the optimizer reaches this barrier.
However, the gradients do not align with each other, contrary to expectations from the previous
chapter. The obtained design point is, in fact, the only feasible design point; all other values
for 𝑥 result in 𝑔1 (𝑥 ) values higher than zero, violating the constraint condition.
When the bounded constraint for ‘l’ is loosened to [10, 2500] and ‘b’ to [1, 250], the gradients
can then be observed to align with each other. Although this design is not permitted and is
subject to various physical issues, such as being highly susceptible to buckling, it represents
a mathematically superior result.

6
Figure 10 - Enlarged design space, yielding better values.

Figure 9 - The gradient can be seen to


be much better.

This process can be repeated for different fixed variables to examine the design space for the
other free variables. The results should be somewhat consistent.
In addition, the incorrect results of the LHS approach can also be plotted in 3D, giving a visual
explanation to the error.

Figure 11 - 3D visualization of the incorrect final


design point, t and w are again fixed.

7
3 APPENDIX

A Optimization code for all three approaches


clc; clear; close all; warning('off','all');
% Define Colorpalette in rgb
clp = [0.79, 0.17, 0.57;...
0.0, 0.29, 0.29;...
0.99, 0.078, 0.44];

syms x1 x2 x3 x4, 'real'; % define symbolically the design variables


% l = x1, b = x2, t = x3, w = x4
%x3 = 2.5;
%x4 = 2.5;

E=210000;
rho = 0.000007850;
L=3000;
F = 10000;

f = rho*L*((x1-2*x4)*x3+2*x2*x4);
func = matlabFunction(f,'vars', {[x1, x2, x3, x4]});

A = []; % linear inequalities matrix is empty


b_0 = []; % linear inequalities vector is empty
Aeq = []; % linear equalities matrix is empty
beq = []; % linear equalities vector is empty

lb = [10, 10, 1, 1]; % lower bounds


ub = [250, 250, 50, 50]; % upper bounds

%{
%===============VERSION ONE -CHOOSE A STARTING POINT YOURSELF
x0=[250, 250, 50, 50]; % initial design point
[xstar, fstar, ~,~,lambda,grad,hessian] = ...
fmincon(func,x0,A,b_0,Aeq,beq,lb,ub, @con);
opt = fstar
values = xstar
%===============
%}

%===============VERSION TWO - LHS


rng default
points = lhsdesign(50,4);
for i = 1:50
points_scaled(i, :) = points(i, :).*[250, 250, 49, 49] + [10, 10, 1, 1];
end

8
for i = 1:50
[xstar, fstar, ~,~,lambda,gradient,hessian] = ...
fmincon(func,points_scaled(i, :),A,b_0,Aeq,beq,lb,ub, @con);

xstar_tot(i,:) = xstar;
fstar_tot(i) = fstar;

end

opt = min(fstar_tot)
opt_index = find(fstar_tot == opt);
values = xstar_tot(opt_index,:)
[~,eigenvalues] = eig(hessian)
gradient
%===============

%{
%===============VERSION THREE - GLOBAL SEARCH
x0=[25, 25, 5, 5]; % initial design point
gs = GlobalSearch;
problem = createOptimProblem('fmincon', 'x0', x0, 'objective',func, ...
'lb',lb,'ub',ub,'nonlcon',@con);
[xstar, fstar] = run(gs,problem);
opt = fstar
values = xstar
%===============
%}

%calculate the deflection and stress in the final design:


l = values(1); b = values(2); t = values(3); w = values(4);
%l = 10; b = 250; t = 1; w = 50; % manual values
Deflection = (F*L^3)/(3*E)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)
Stress = (F*L*l)/(2)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)
Ixx = (t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2
%%
function [cineq,ceq]=con(x)

l = x(1); b = x(2); t = x(3); w = x(4);


E=210000;
L=3000;
F = 10000;
%
ceq =[];
cineq =[((F*L^3)/(3*E)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-
5);
((F*L*l)/(2)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-235)
];
end

9
B Code for visualization in three-dimensional space
clc; clear; close all;
% Define Colorpalette in rgb
clp = [0.79, 0.17, 0.57;...
0.0, 0.29, 0.29;...
0.99, 0.078, 0.44];

syms x1 x2 , 'real'; % define symbolically the design variables


% l = x1, b = x2, t = x3, w = x4
x3 = 1;
x4 = 11.9551;

E=210000;
rho = 0.000007850;
L=3000;
F = 10000;

f = rho*L*((x1-2*x4)*x3+2*x2*x4);
func = matlabFunction(f,'vars', {[x1, x2]});
x0=[50, 50]; % initial design point --> l en b

A = []; % linear inequalities matrix is empty


b_0 = []; % linear inequalities vector is empty
Aeq = []; % linear equalities matrix is empty
beq = []; % linear equalities vector is empty

%lb = [10, 10, 1, 1]; % lower bounds


%ub = [250, 250, 50, 50]; % upper bounds
lb = [10,10];
ub = [250, 250];

[xstar, fstar, ~,~,lambda,grad,hessian] = fmincon(func,x0,A,b_0,Aeq,beq,lb,ub,


@con);

xstar
fstar
hessian
[eigvect,eigval] = eig(hessian)

plot_constr(f,fstar,xstar,clp,lb,ub,grad)
%%
function [cineq,ceq]=con(x)

l = x(1); b = x(2); t = 1; w = 11.9551;

E=210000;
L=3000;
F = 10000;

ceq =[];
cineq =[((F*L^3)/(3*E)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-
5);
((F*L*l)/(2)*((t*(l-2*w)^3)/12+((b*w^3)/12+b*w*((l-w)/2)^2)*2)^(-1)-235)
];
end
10
%%

function plot_constr(f,fstar,xstar,clp,lb,ub,grad)
%{
Inputs: symbolic function, f
optimum value, fstar
optimum point, xstar
color palette, clp
side constraints, lb, ub
gradient of the objective, grad
%}

t = 1;
w = 11.9551;

E=210000;
rho = 0.000007850;
L=3000;
F = 10000;
% Define the (in)equality constraint as function handle
%stiffness constraint
%cineq_1 =@(x1,x2) ((F*L^3)/(3*E)*((t*(x1-2*w).^3)./12+((x2.*w^3)/12+x2*w*((x1-
w)./2).^2).*2).^(-1)-5);
cineq_1 =@(x1,x2) ((F.*L.^3)./(3.*E).*((t.*(x1-
2.*w).^3)./12+((x2.*w.^3)./12+x2.*w.*((x1-w)./2).^2).*2).^(-1)-5);
%strength constraint
cineq_2 =@(x1,x2) ((F.*L.*x1)./(2).*((t.*(x1-
2.*w).^3)./12+((x2.*w^3)./12+x2.*w.*((x1-w)./2).^2).*2).^(-1)-235);

np=40; % define the points to create the meshgrid

x1_range = linspace(lb(1), ub(1), np); % Adjust the number of points np for finer
resolution
x2_range = linspace(lb(2), ub(2), np); % Adjust the number of points np for finer
resolution

[X1, X2] = meshgrid(x1_range, x2_range); % create a meshgrid of the range, here


[np x np]

figure('color','w');
%plotting the minimization function
ss = ezsurf(f,[lb(1),ub(1)],[lb(2),ub(2)]); ... Plot the function
set(ss ,'FaceColor','b','FaceAlpha',.5,'EdgeColor','k','EdgeAlpha',.8); hold
on;

surf(X1,X2,cineq_1(X1,X2),'FaceColor',clp(2,:),'EdgeColor','k','EdgeAlpha',.5);
hold on; % plot constraint 1
surf(X1,X2,cineq_2(X1,X2),'FaceColor',clp(3,:),'EdgeColor','k','EdgeAlpha',.5);
hold on; % plot constraint 1

11
%plotting the optimum
scatter3(xstar(1),xstar(2),fstar,'o','SizeData',40,'MarkerFaceColor','y','MarkerEd
geColor','k'); hold on;
grid on; grid minor;
title(['$ f(x_1,x_2) = ', latex(f),'$'],'interpreter','latex');
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');
legend({'$f_{(x_1,x_2)}$','doorbuiging',
'spanning'},'interpreter','latex','box','off')

%SOLVING THE ERROR ON PLOTTING THE GRADIENT WHEN BOUNDEDCONSTR == 250 BY


%ENLARGING THE ZONE SLIGHTLY
np=400; % define the points to create the meshgrid
x1_range = linspace(lb(1), 260, np); % Adjust the number of points np for finer
resolution
x2_range = linspace(lb(2), 260, np); % Adjust the number of points np for finer
resolution
[X1, X2] = meshgrid(x1_range, x2_range); % create a meshgrid of the range, here
[np x np]

figure('color','w');
%stiffness constraint
ind = cineq_1(X1,X2)<=0; % find points that cineq_1<=0
X1d = X1(ind); X2d= X2(ind);
x1_range = linspace(min(X1d), max(X1d), np); % Adjust the number of points np for
finer resolution
x2_range = linspace(min(X2d), max(X2d), np); % Adjust the number of points np for
finer resolution

[X1d, X2d]=meshgrid(x1_range, x2_range);


contourf(X1d, X2d, cineq_1(X1d,X2d), [-100:10:10],...
'ShowText','on','linecolor',clp(2,:),'fill','off'); hold on; % plot contours
of g1
% The range [-100:10:10] defines contours from the value -100 with step 10
% until 10; change accrodingly
colormap(bone);

% plot gradient of the OBJECTIVE


quiver(xstar(1),xstar(2),grad(1)*50,grad(2)*50,'color','b','linewidth',.5,...
'AutoScaleFactor',.6); hold on;

% plot the gradient of the CONSTRAINT numerically: Forward Finite


% Differences --> neem een infinitesimale stap (eps = 2^(-52)) en die wat
% hier de helling van is (rico). dit is dan een lokale benadering voor de
% gradiënt op dat punt. zo hoef je niet de gradiënt van het volledige veld
% te kennen.
h_current = cineq_1(xstar(1),xstar(2));
% at the optimum the constraint must be zero
% (because of the way our constraint is defined)

12
%bereken hoogte voor een infinitecimale stap met grootte sqrt(eps)
h_current_dx1 = cineq_1(xstar(1)+sqrt(eps),xstar(2));
h_current_dx2 = cineq_1(xstar(1),xstar(2)+sqrt(eps));
%verschil tussen beginpositie en stap geeft gradiëntbenadering
grad_h = [h_current_dx1-h_current, h_current_dx2-h_current]./sqrt(eps);
%./sqrt(eps)? --> definitie afgeleide navolgen
grad_h = grad_h./norm(grad_h);
quiver(xstar(1),xstar(2),grad_h(1)*50,grad_h(2)*50,'color',clp(2,:),'linewidth',1.
5,...
'AutoScaleFactor',.6); hold on;

scatter(xstar(1),xstar(2),'MarkerFaceColor','y','SizeData',40,'MarkerEdgeColor','k
'); % plot optimum
legend({'$h_{1(x_1,x_2)} \leq 0$','$ \nabla f({x}^{*}) $',...
'$ \nabla h({x}^{*}) $',['$ x^{*} = [ ',num2str(xstar(1)),', \
',num2str(xstar(2)),']$']},'interpreter','latex','box','off',
Location='northwest');
grid on; grid minor;
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');

figure('color','w');
ind = cineq_2(X1,X2)<=0; % find points that cineq_2<=0
X1d = X1(ind); X2d= X2(ind);
x1_range = linspace(min(X1d), max(X1d), np); % Adjust the number of points np for
finer resolution
x2_range = linspace(min(X2d), max(X2d), np); % Adjust the number of points np for
finer resolution
[X1d, X2d]=meshgrid(x1_range, x2_range);
contourf(X1d, X2d, cineq_2(X1d,X2d), [-100:10:10],...
'ShowText','on','linecolor',clp(2,:),'fill','off'); hold on; % plot contours
of g1
% The range [-100:10:10] defines contours from the value -100 with step 10
% until 10; change accrodingly
colormap(bone);
% plot gradient of the objective
quiver(xstar(1),xstar(2),grad(1),grad(2),'color','b','linewidth',.5,...
'AutoScaleFactor',.6); hold on;

% plot the gradient of the constraint numerically: Forward Finite


% Differences
h_current = cineq_2(xstar(1),xstar(2)); % at the optimum the constraint must be
zero
h_current_dx1 = cineq_2(xstar(1)+sqrt(eps),xstar(2));
h_current_dx2 = cineq_2(xstar(1),xstar(2)+sqrt(eps));
grad_h = [h_current_dx1-h_current, h_current_dx2-h_current]./sqrt(eps);
grad_h = grad_h./norm(grad_h);
quiver(xstar(1),xstar(2),grad_h(1),grad_h(2),'color',clp(2,:),'linewidth',1.5,...
'AutoScaleFactor',.6); hold on;
scatter(xstar(1),xstar(2),'MarkerFaceColor','y','SizeData',40,'MarkerEdgeColor','k
'); % plot optimum
legend({'$h_{1(x_1,x_2)} \leq 0$','$ \nabla f({x}^{*}) $',...
'$ \nabla h({x}^{*}) $',['$ x^{*} = [ ',num2str(xstar(1)),', \
',num2str(xstar(2)),']$']},'interpreter','latex','box','off');
grid on; grid minor;
xlabel('$x_1$','interpreter','latex');
ylabel('$x_2$','interpreter','latex');
end

13
4 REFERENCES

De Samblanx, G. (2021). Notities bij het vak Wiskundige Modellen. Acco.


Martins, J. R., & Ning, A. (2020). Engineering Design Optimization. Cambridge University
Press.
MATLAB Help Center - fmincon. (sd). Opgeroepen op May 10, 2024, van MathWorks:
https://nl.mathworks.com/help/optim/ug/fmincon.html

14
FACULTY OF ENGINEERING TECHNOLOGY
DE NAYER (SINT-KATELIJNE-WAVER) CAMPUS
Jan De Nayerlaan 5
2860 SINT-KATELIJNE-WAVER, België
tel. + 32 16 30 10 30
[email protected]
www.fet.kuleuven.be

You might also like