Final Lab Record
Final Lab Record
Fifth Semester
B Tech. Automation and Robotics Engineering
Mahilash S
AM.EN.U4ARE22045
8 To minimize a computationally 23
expensive function using parallel
computing in MATLAB.
f(x, y) = x² + y² + xy – 10x – 4y
This is a smooth, unconstrained quadratic function. The goal is to find the values of x and y
that minimize this expression using MATLAB’s fminunc solver.
APPARATUS
● · Computer
● · MATLAB Software
PROCEDURE
CODE
[x,fval] = fminunc(fun,x0,options);
1
disp('The minimum value is at:'); % Display results
disp(x);
disp(fval);
figure;
surf(x1, x2, z)
hold on
xlabel('x1')
ylabel('x2')
grid on
2
RESULT
Fig 1 – Surface plot of the objective function showing the minimum point identified by fminunc.
CONCLUSION
The experiment successfully demonstrated the use of MATLAB’s fminunc function to find
the local minimum of a nonlinear, unconstrained function using the quasi-Newton
optimization algorithm. By visualizing the function surface and its contours, we gained
insight into the topography of the objective function. The solver efficiently converged to a
solution, starting from an initial guess, and provided the optimal point along with the
minimum function value. This experiment highlights the power and ease of MATLAB's
optimization toolbox for solving real-world nonlinear minimization problems without
constraints.
3
EXPERIMENT 2
AIM: Unconstrained Minimization Using fminsearch
This is an unconstrained nonlinear function. The objective is to locate the minimum point
using the derivative-free simplex search method provided by MATLAB’s fminsearch.
APPARATUS:
● · Computer
● · MATLAB software
PROCEDURE:
5. Plot the surface of the objective function and mark the minimum point found.
CODE:
% Initial guess
x0 = [0, 0];
% Call fminsearch
4
% Display the results
disp('Minimum point:');
disp(x);
disp('Minimum value:');
disp(fval);
figure;
surf(x1, x2, z)
hold on
xlabel('x1')
ylabel('x2')
grid on
5
RESULT:
Fig 2: Surface plot showing the function minimized using fminsearch, with the minimum point highlighted.
CONCLUSION:
The experiment successfully used MATLAB’s fminsearch function to find the minimum of an
unconstrained function. Since fminsearch does not require gradients, it is ideal for optimizing
smooth functions when derivatives are unavailable or difficult to compute.
6
EXPERIMENT 3
AIM: Constrained Minimization Using fmincon
APPARATUS:
● · Computer
● · MATLAB software
PROCEDURE:
1. Define the objective function as an anonymous function.
4. Use fmincon to solve the problem with the defined constraints.
5. Plot the feasible region and mark the solution point.
CODE:
% Initial guess
x0 = [0.5, 0.5];
A = [-1, -1];
b = -1;
7
% Lower bounds
lb = [0, 0];
% Call fmincon
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
[x, fval] = fmincon(fun, x0, A, b, [], [], lb, [], [], options);
% Display results
disp('Optimal point:');
disp(x);
disp('Minimum value:');
disp(fval);
z = x1.^2 + x2.^2;
figure;
hold on
xlabel('x')
ylabel('y')
zlabel('Objective Function')
grid on
8
RESULT:
Fig 3.1: Surface plot of the objective function under linear constraints using fmincon, showing the optimal
solution.
CONCLUSION:
This experiment illustrates how fmincon can be used to solve a simple constrained
optimization problem. The function minimized subject to bounds and inequality constraints
showcases fmincon's ability to handle real-world conditions.
9
EXPERIMENT 4
AIM: Minimization with Bound Constraints
–1 ≤ x ≤ 2
APPARATUS:
● · Computer
● · MATLAB Software
PROCEDURE:
2. Set the lower and upper bounds for the decision variable.
5. Plot the function over the interval and mark the minimum.
CODE:
% Bounds
lb = -1;
ub = 2;
10
% Use fminbnd to find the minimum
% Display results
disp(x);
disp(fval);
hold on
xlabel('x')
ylabel('f(x)')
grid on
11
RESULT:
Fig 4 – Plot of the function within bounds, showing the minimum point identified using fminbnd.
CONCLUSION:
The experiment demonstrates how MATLAB's fminbnd function can effectively find the
minimum of a single-variable function within given bounds. This method is efficient for
bounded scalar problems where no gradients are needed.
12
EXPERIMENT 5
AIM: Minimization with Nonlinear Inequality Constraints
f(x, y) = x² + y²
(x – 1)² + y² ≤ 1
This experiment demonstrates how to solve a problem with nonlinear inequality constraints
using MATLAB’s fmincon.
APPARATUS:
● · Computer
● · MATLAB software
PROCEDURE:
3. Choose an initial guess that satisfies or is near the constraint boundary.
CODE:
% Objective function
% Initial guess
x0 = [0.5, 0.5];
13
% Nonlinear constraint function
% Call fmincon
options = optimoptions('fmincon','Display','iter','Algorithm','sqp');
[x, fval] = fmincon(fun, x0, [], [], [], [], [], [], nonlcon, options);
% Display results
disp('Optimal point:');
disp(x);
disp('Minimum value:');
disp(fval);
Z = x1.^2 + x2.^2;
figure;
hold on
xlabel('x')
ylabel('y')
zlabel('f(x,y)')
grid on
14
RESULT:
Fig 5 - 3D plot of the objective function with constraint boundary and optimal point marked.
CONCLUSION:
This experiment shows how fmincon handles nonlinear inequality constraints. The constraint defines
a circular feasible region, and the optimizer successfully finds the minimum point within that region.
15
EXPERIMENT 6
AIM: Nonlinear Constraints with Gradients
f(x, y) = x² + y²
x² – y ≥ 0
This experiment shows how providing analytical gradients can improve optimization
efficiency using fmincon.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
CODE:
% Main script
% Initial point
x0 = [1; 1];
16
% Set options to specify gradients
'Display', 'iter');
% Call fmincon
[x, fval] = fmincon(@objfun, x0, [], [], [], [], [], [], @nonlcon, options);
% Display result
disp('Minimum point:');
disp(x);
disp('Minimum value:');
disp(fval);
% Plotting
z = x1.^2 + x2.^2;
figure;
hold on
17
xlabel('x')
ylabel('y')
zlabel('Objective Function')
grid on
f = x(1)^2 + x(2)^2;
end
end
18
RESULT:
Fig 6 – Surface plot of objective function with constraint curve and optimal point using gradients.
CONCLUSION:
This experiment shows that using gradient information in fmincon enhances performance and
precision. It is especially useful when dealing with complex problems or when faster
convergence is desired.
19
EXPERIMENT 7
AIM: Banana Function Minimization
This non-convex function is shaped like a banana valley and is often used to test the
performance of optimization algorithms. We'll use fminunc to find the minimum.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
CODE:
% Initial guess
x0 = [-1.2, 1];
% Use fminunc
20
options = optimoptions('fminunc','Display','iter','Algorithm','quasi-newton');
% Display result
disp('Minimum point:');
disp(x);
disp('Minimum value:');
disp(fval);
% Plotting
figure;
hold on
xlabel('x')
ylabel('y')
zlabel('f(x, y)')
grid on
21
RESULT:
Fig 7 – Surface plot of the Rosenbrock function with minimum point marked using fminunc.
CONCLUSION:
The experiment demonstrates the use of fminunc to solve the non-convex Rosenbrock
function. Despite the function’s curved valley structure, fminunc successfully found the
global minimum due to its robust quasi-Newton algorithm.
22
EXPERIMENT 8
AIM: Minimizing an Expensive Function Using Parallel Computing
This function is computationally expensive due to added sine and cosine terms. The goal is to
minimize it using fminunc with parallel computing enabled to accelerate evaluations.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
1. Define the objective function with some delay to simulate computational cost.
CODE:
x0 = [0, 0];
% Optimization options
'UseParallel', true);
23
if isempty(gcp('nocreate'))
parpool;
end
% Call fminunc
% Display result
disp('Minimum point:');
disp(x);
disp('Minimum value:');
disp(fval);
figure;
hold on
xlabel('x')
ylabel('y')
zlabel('f(x, y)')
grid on
end
24
RESULT:
Fig 8 – Surface plot of an expensive function, showing the minimum point located using parallel evaluation in
fminunc.
CONCLUSION:
By enabling parallel computing, the optimization process becomes significantly faster for
expensive function evaluations. This is ideal for simulations or models with high
computational cost per iteration.
25
EXPERIMENT 9
AIM: Solve Nonlinear Problem with Many Variables
This experiment involves optimizing a nonlinear function with many variables. The
purpose is to test the scalability of fminunc when the number of variables is large (e.g., n =
50).
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
4. Observe how well the solver handles a large number of variables.
CODE:
n = 50;
fun = @(x) sum((x - 1).^2 + sin(x).^2); % Objective function (sum of squared deviations and sinusoidal terms)
% Optimization options
'MaxIterations', 500);
26
[x, fval] = fminunc(fun, x0, options); % Solve the problem
disp(x');
disp(fval);
RESULT:
Fig 9 – Since this is a high-dimensional problem, no 2D or 3D plot is shown. The result is the optimized
variable vector.
CONCLUSION:
27
EXPERIMENT 10
AIM: Linear Programming Using linprog (Solver-Based
6x₁ + 4x₂ ≤ 24
x₁ + 2x₂ ≤ 6
–x₁ + x₂ ≤ 1
x₁ ≥ 0, x₂ ≥ 0
This experiment demonstrates how to solve a linear programming problem using MATLAB’s
linprog function.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
CODE:
b = [24; 6; 1];
28
lb = [0; 0]; % Lower bounds (x1 ≥ 0, x2 ≥ 0)
disp('Optimal point:');
disp(x);
disp(fval);
RESULT:
Fig 10 – Text-based solution output showing optimal values of decision variables and minimum objective
function value.
CONCLUSION:
This experiment successfully applied linprog to solve a linear programming problem with
multiple inequality constraints. The method provides a fast and reliable solution for problems
involving linear cost functions and constraints, commonly used in operations research and
resource allocation.
29
EXPERIMENT 11
AIM: Maximize Long-Term Investments Using Linear Programming (Solver-Based)
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
1. Convert the maximization problem into a minimization by negating the profit
coefficients.
CODE:
f = [-0.07; -0.12; -0.15]; % Objective function (maximize => minimize negative profit)
beq = 1;
lb = [0.2; 0; 0];
ub = [1; 1; 0.5];
30
options = optimoptions('linprog','Display','iter'); % Call linprog
[x, fval] = linprog(f, [], [], Aeq, beq, lb, ub, options);
disp(x);
disp('Maximum profit:');
RESULT:
Fig 11 – Display of optimal investment distribution among three funds and the corresponding maximum
expected profit.
CONCLUSION:
The experiment demonstrates the use of linprog in financial optimization. The optimal
allocation meets all investment constraints while maximizing returns. This highlights the
practical application of linear programming in portfolio management and decision-making.
31
EXPERIMENT 12
AIM: Mixed-Integer Linear Programming Using intlinprog
subject to:
2x₁ + x₂ ≤ 6
x₁ + x₂ ≤ 4
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
CODE:
b = [6; 4];
32
lb = [0; 0]; % Variable bounds
disp(x);
disp(fval);
RESULT:
Fig 12 – Tabular output showing optimal integer values for decision variables and minimum cost.
CONCLUSION:
The experiment demonstrates how intlinprog handles problems where variables must be
integers. Such problems arise in real-life scenarios like scheduling, resource assignment, and
logistics, where fractional values are not acceptable.
33
EXPERIMENT 13
AIM: Factory-Warehouse-Sales Allocation Model Using MILP
Minimize total cost of shipping from factories to warehouses and then to sales points,
while satisfying demand and supply constraints, with some binary allocation decisions.
This experiment illustrates how to model and solve a supply chain problem using intlinprog.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
3. Add constraints for factory supply, warehouse capacity, and sales demand.
4. Enforce binary (0/1) decisions on shipping routes using integer constraints.
CODE:
intcon = 1:4;
A = [1 0 1 0; % Factory 1 to WH 1 and 2
34
0 1 0 1]; % Factory 2 to WH 1 and 2
b = [1; 1];
Aeq = [1 1 0 0; % WH 1
0 0 1 1]; % WH 2
lb = zeros(4,1);
ub = ones(4,1);
% Solve MILP
options = optimoptions('intlinprog','Display','iter');
% Display result
disp(x);
disp('Total cost:');
disp(fval);
35
RESULT:
Fig 13 – Binary vector indicating shipping routes chosen and the minimum total cost.
CONCLUSION:
This experiment shows how intlinprog can be applied to supply chain logistics. By modeling
allocation as binary decisions, it effectively selects optimal routes under resource constraints,
demonstrating the power of MILP in real-world scenarios.
36
EXPERIMENT 14
AIM: Solve Sudoku Using Integer Programming
Each variable represents the presence (1) or absence (0) of a digit in a specific cell.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
1. Represent the Sudoku grid as a 9×9×9 binary variable (row, column, digit).
a. Each number appears once per row, column, and 3×3 subgrid.
4. Reconstruct the solved Sudoku grid from the binary solution vector.
CODE:
Aeq = eye(64);
beq = ones(64,1);
intcon = 1:64;
lb = zeros(64,1);
37
ub = ones(64,1);
Aeq(end,6) = 1;
options = optimoptions('intlinprog','Display','off');
[x, ~] = intlinprog(f, intcon, [], [], Aeq, beq, lb, ub, options);
RESULT:
Fig 14 – Binary matrix showing valid assignment of digits to cells in a 4×4 Sudoku grid.
CONCLUSION:
This experiment shows how integer programming can solve constraint satisfaction problems
like Sudoku. Although solving a full 9×9 Sudoku requires a large binary variable set and
complex constraints, intlinprog proves effective for structured logical problems.
38
EXPERIMENT 15
AIM: Quadratic Programming with Bound Constraints
To solve a quadratic programming problem with bound constraints using the quadprog
function in MATLAB.
APPARATUS:
● Computer
● MATLAB Software
PROCEDURE:
2. Define bound constraints as vectors lb (lower bounds) and ub (upper bounds).
CODE:
% Define bounds
39
x = quadprog(H, f, [], [], [], [], lb, ub);
% Display result
disp('Optimal solution:');
disp(x);
RESULT:
CONCLUSION:
The quadprog function successfully minimized the quadratic function under the given bound
constraints. This method is efficient for convex quadratic problems commonly found in
control systems and portfolio optimization.
40
EXPERIMENT 16
AIM: Portfolio Optimization Using Quadratic Programming
To optimize a portfolio by minimizing the risk (variance) subject to return and budget
constraints using quadratic programming in MATLAB.
APPARATUS:
● MATLAB R2020b
● Optimization Toolbox
PROCEDURE:
1. Define the covariance matrix H representing the variance between assets.
4. Include the constraint that the sum of all asset weights must equal 1 (fully invested).
CODE:
f = [0; 0; 0];
41
% Desired portfolio return
targetReturn = 0.16;
Aeq = [r';
1 1 1];
beq = [targetReturn;
1];
% Lower and upper bounds (no short selling, max 100% in one asset)
lb = [0; 0; 0];
ub = [1; 1; 1];
% Display result
disp(x);
42
RESULT:
Fig 16 Optimal weights of assets in the portfolio and the minimized risk value.
CONCLUSION:
43
EXPERIMENT 17
AIM: Multiobjective Optimization Using fgoalattain
APPARATUS:
● Optimization Toolbox
PROCEDURE:
CODE:
(x(1)-1)^2 + (x(2)-2)^2];
% Initial guess
x0 = [0; 0];
44
% Weights for each goal
lb = [-5; -5];
ub = [5; 5];
[], [], [], [], [], [], [], options, lb, ub);
% Display result
disp('Optimal solution:');
disp(x);
disp(fval);
45
RESULT:
Fig 17 Optimized decision variables and the values of each objective function at the optimal point.
CONCLUSION:
46
EXPERIMENT 18
AIM: Generate and Plot Pareto Front
To generate and visualize the Pareto front for a multiobjective optimization problem using
MATLAB’s gamultiobj function.
APPARATUS:
PROCEDURE:
1. Define two or more conflicting objective functions in a single vectorized function.
CODE:
nvars = 2;
lb = [-5, -5];
ub = [5, 5];
[x, fval] = gamultiobj(objfun, nvars, [], [], [], [], lb, ub);
figure;
title('Pareto Front');
grid on;
47
RESULT:
Fig 18 A 2D plot showing the Pareto front, where each point represents a trade-off between the two objectives.
CONCLUSION:
The Pareto front provides insight into trade-offs between multiple conflicting objectives. The
gamultiobj function in MATLAB efficiently finds a diverse set of non-dominated solutions,
useful in decision-making scenarios.
48
EXPERIMENT 19
AIM: Problem-Based Linear Programming
APPARATUS:
● Optimization Toolbox
PROCEDURE:
CODE:
49
prob = optimproblem('Objective', obj, 'Constraints', cons);
disp('Optimal solution:');
disp(sol);
RESULT:
Fig 19 Optimal values of x(1) and x(2) and the minimum value of the objective function.
CONCLUSION:
MATLAB’s problem-based approach offers an intuitive way to define and solve linear
programming problems symbolically, improving code readability and flexibility.
50
EXPERIMENT 20
AIM: Problem-Based Nonlinear Optimization
APPARATUS:
● Optimization Toolbox
PROCEDURE:
CODE:
plot(x, y)
51
RESULT:
Fig 20 Optimal values of x1 and x2 that minimize the nonlinear objective under constraints.
CONCLUSION:
52