Minimization and Optimization. Minimize output by optimizing inputs

Hello
Trying to minimize the output by finding the optimium input values. I am unable to construct the minimization part of the code with the constrains I have. Any help would be greatly appreciated.
% Objective:Optimize the lengths to minimze the power using a new variable.
a = 0.1:20;
b = 0.1:20;
d = a + 0.1:20;
c = b + 0.1:20;
e = d + 0.1:20
%Constants
k= 2;
w=1;
v=1.5
%Variables and their constrains
AB = sqrt(a.^2 + b.^2);
BC = sqrt( c.^2 + ((e-d)/2).^2 );
CS = sqrt( c.^2 + ((e-d)/2).^2 );
VAB = sqrt(((((a.*v).^2/(((b.^2).*4))) + (v^2)/2 )));
% VBS = sqrt(((a*v)^2/((4*b*b)) + (v^2)/2 ));
VCS = ((2*c)./(e-d)).*sqrt(AB.^2);
VBC= CS.^2 + BC.^2;
%Actual Power
%P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w; % Power
%
% Objective
%To search and find the values for a, b, c d and e to minimize power (P)
% Not sure how to write the function for above
% fun = @(x)(x(:,1) + x(:,2) + x(:,3) + x(:,4) + x(:,5)).*k.*w;
%
% [X1, X2, X3, X4, X5] = ndgrid(0:.1:2); % Should I give this condition in a nested loop?
%
% X = [X1(:), X2(:), X3(:), X4(:), X5(:)];
%
% P = fun(X);
% [bestP, idx] = min(P(:))
% best_X = X(idx,:)

 Accepted Answer

Sounds like you would be best served with the Problem-Based Optimization Workflow. Declare the variables that can change as optimization variables, set the problem objective to P, and call solve.
Alan Weiss
MATLAB mathematical toolbox documentation

7 Comments

I attempted what you suggested. Unsure where I am going wrong.
%defining optimization variables and an optimization problem object.
a1 = optimvar('a');
b1 = optimvar('b');
c1 = optimvar('c');
d1 = optimvar('d');
e1 = optimvar('e');
prob = optimproblem;
k= 2;
w=1;
v=1.5;
%Variables
AB = sqrt(a.^2 + b.^2);
Unrecognized function or variable 'a'.
BC = sqrt( c.^2 + ((e-d)/2).^2 );
CS = sqrt( c.^2 + ((e-d)/2).^2 );
VAB = sqrt(((((a.*v).^2/(((b.^2).*4))) + (v^2)/2 )));
% VBS = sqrt(((a*v)^2/((4*b*b)) + (v^2)/2 ));
VCS = ((2*c)./(e-d)).*sqrt(AB.^2);
VBC= CS.^2 + BC.^2;
%objective function as an expression in the optimization variables.
P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w;
%the objective function in prob.
prob.Objective = P;
%constraints
cons 1 = a = 0.1:20;
cons 2 =b = 0.1:20;
cons 3 = d = a + 0.1:20;
cons 4 = c = b + 0.1:20;
cons e = d + 0.1:20;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
I am not sure what you are trying to do, but you need to declare variables as documented.
a1 = optimvar('a'); % Don't do this
% Instead, use
a = optimvar('a');
Furthermore, I don't think that your constraints are really constraints, but instead should be bounds. For example,
cons 1 = a = 0.1:20; % Several mistakes here.
% I think what you mean is
a = optimvar('a','LowerBound',0.1,"UpperBound",20);
% Similarly for the other variables: declare with correct names and with
% bounds
Try it this way and see if you like the results.
Alan Weiss
MATLAB mathematical toolbox documentation
I updated the code accordingly. Not sure what I am missing. Unable to generate the output.
%defining optimization variables and an optimization problem object.
a = optimvar('a','LowerBound',0.1,"UpperBound",20);
b = optimvar('b','LowerBound',0.1,"UpperBound",20);
c = optimvar('c','LowerBound',0.1,"UpperBound",20);
d = optimvar('d','LowerBound',0.1,"UpperBound",20);
e = optimvar('e','LowerBound',0.1,"UpperBound",20);
prob = optimproblem;
k= 2;
w=1;
v=1.5;
%the objective function in prob.
prob.Objective = P;
%constraints
% cons1 = e >= (a+d);
% cons2 = d >=a ;
% cons3 = b <=c ;
% cons4 = (e-d) >= (d-a) ;
% cons5 = (c-b) <= b;
cons1 = e - a- d >= 0.1;
cons2 = d - a >= 0.1 ;
cons3 = c-b >= 0.1;
cons4 = b - a >= 0.1 ;
cons5 = (e-c) >=0.1;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
x0.a = 4;
x0.b = 6;
x0.c = 8;
x0.d = 7;
x0.e = 12;
%new variables
AB = sqrt(a.^2 + b.^2);
BC = sqrt( c.^2 + ((e-d)/2).^2 );
CS = sqrt( c.^2 + ((e-d)/2).^2 );
VAB = sqrt(((((a.*v).^2/(((b.^2).*4))) + (v^2)/2 )));
% VBS = sqrt(((a*v)^2/((4*b*b)) + (v^2)/2 ));
VCS = ((2*c)./(e-d)).*sqrt(AB.^2);
VBC= CS.^2 + BC.^2;
%objective function as an expression in the optimization variables.
P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w;
Your code defines the objective before defining P. Put the line defining the problem objective after P is completely defined. Then call solve.
Alan Weiss
MATLAB mathematical toolbox documentation
@Alan Weiss, Thank you Sir!
I think I generated the output. However the optimized results vary with the initial guess. Is there a way to fix it?
%defining optimization variables and an optimization problem object.
a = optimvar('a','LowerBound',0.1,"UpperBound",20);
b = optimvar('b','LowerBound',0.1,"UpperBound",20);
c = optimvar('c','LowerBound',0.1,"UpperBound",20);
d = optimvar('d','LowerBound',0.1,"UpperBound",20);
e = optimvar('e','LowerBound',0.1,"UpperBound",20);
prob = optimproblem;
k= 2;
w=1;
v=1.5;
%new variables
AB = sqrt(a.^2 + b.^2);
BC = sqrt( c.^2 + ((e-d)/2).^2 );
CS = sqrt( c.^2 + ((e-d)/2).^2 );
VAB = sqrt(((((a.*v).^2/(((b.^2).*4))) + (v^2)/2 )));
% VBS = sqrt(((a*v)^2/((4*b*b)) + (v^2)/2 ));
VCS = ((2*c)./(e-d)).*sqrt(AB.^2);
VBC= CS.^2 + BC.^2;
%objective function as an expression in the optimization variables.
P = (AB.*VAB + BC.*VBC + CS.*VCS).*k*w;
%the objective function in prob.
prob.Objective = P;
%constraints
% cons1 = e >= (a+d);
% cons2 = d >=a ;
% cons3 = b <=c ;
% cons4 = (e-d) >= (d-a) ;
% cons5 = (c-b) <= b;
cons1 = e - a- d >= 0.1;
cons2 = d - a >= 0.1 ;
cons3 = c-b >= 0.1;
cons4 = b - a >= 0.1 ;
cons5 = (e-c) >=0.1;
prob.Constraints.cons1 = cons1;
prob.Constraints.cons2 = cons2;
prob.Constraints.cons3 = cons3;
prob.Constraints.cons4 = cons4;
prob.Constraints.cons5 = cons5;
x0.a = 4;
x0.b = 6;
x0.c = 8;
x0.d = 7;
x0.e = 12;
sol = solve(prob,x0)
If you look at the objective function value
[sol,fval] = solve(prob,x0)
you find that the different solutions all have the same objective function value. So there are many solutions. Is this unexpected? This is not something that I can tell you how to fix. it is for you to determine whether the problem is formulated correctly.
Alan Weiss
MATLAB mathematical toolbox documentation

Sign in to comment.

More Answers (0)

Asked:

on 6 Oct 2021

Commented:

on 11 Oct 2021

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!