How to set a target in the Optimisation Toolbox?
4 次查看(过去 30 天)
显示 更早的评论
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 个评论
采纳的回答
Matt J
2021-11-18
编辑:Matt J
2021-11-18
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 个评论
Matt J
2021-11-18
编辑:Matt J
2021-11-18
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
更多回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!