How to set a target in the Optimisation Toolbox?
3 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Greetings, I have the following code which simply finds a value which maximises the area of a square.
Lets say I want the Area to be not maximum but lets say 23. (or any number between 0 and max value). How can I set such a goal ?
Any help would be much appreciated.
Optim.m
clc; clear all; close all
FitnessFunction = @SquareF;
numberOfVariables = 1;
A = []; b = [];
Aeq = []; beq = [];
lb = 0;
ub = 1;
options = optimoptions(@gamultiobj,'PlotFcn',{@gaplotscorediversity,@gaplotstopping,@gaplotspread});
[x,Volume] = gamultiobj(FitnessFunction,numberOfVariables,[],[],[],[],lb,ub,options);
formatSpec = 'Max Area is %5f\n';
fprintf(formatSpec,Volume)
and
SquareF.m
function y = SquareF(x)
%y(1) = (x+2)^2 - 10;
Lt = 10; % Total Perimeter Length Available
A = Lt*x(1); % Side A
B = Lt-A; % Side B
y(1) = -A*B; %Area m^2
rL = A+B;
end
0 commentaires
Réponse acceptée
Matt J
le 18 Nov 2021
Modifié(e) : Matt J
le 18 Nov 2021
Lets say I want the Area to be not maximum but lets say 23. (or any number between 0 and max value)
It sounds like you are saying you want to maximize area but with the constraint that the area can be no larger than 23 and the perimeter no greater than Lt. For a square, that's the same as saying that the length of the side x is bounded above by min( sqrt(23), Lt/4).
Lt=15;
lb=0;
ub=min( sqrt(23), Lt/4);
fun=@(x) -x^2; %objective
[x,negativeArea]=fmincon(fun,sqrt(23),[],[],[],[],lb,ub)
1 commentaire
Matt J
le 18 Nov 2021
Modifié(e) : Matt J
le 18 Nov 2021
Alternatively, you might be saying you want to bring the area as close to 23 as possible, but still with the constraint that the perimeter is <=Lt, If so, then that would be as below, but happens to give the same result.
Lt=15;
lb=0;
ub=Lt/4;
fun=@(x) (23-x^2).^2; %objective
x=fmincon(fun,sqrt(23),[],[],[],[],lb,ub)
Area=x.^2
Plus de réponses (1)
Voir également
Catégories
En savoir plus sur Solver Outputs and Iterative Display dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!