Optimization of LED channels using MATLAB
1 次查看(过去 30 天)
显示 更早的评论
Hello and thank you for the time taken to help or provide guidance of any kind.
- I have a lighting system that contains 6 distinct channels of LED.
- Each LED channel has output across three regions: violet (v), blue (b), and red (r).
- As there is overlap in v, b, and r in each of the channels, when trying to create a spectral profile with output across v, b, and r, I wanted to create an objective way to optimize the output across the 6 channels. The code below worked for past instances but after an update has stopped working:
%% Optimization
opts = optimoptions('gamultiobj',"PlotFcn","gaplotpareto","PopulationSize",10000);
lb = [0 0 0 0 0 0];
ub = [1 1 1 1 1 1];
[solution,ObjectiveValue] = gamultiobj(@attempt,6,[],[],[],[],lb,ub,[],opts);
function f = attempt(x)
f(1) = ((x(1)*265092064.751006 + x(2)*3009732967.55533 + x(3)*17335448078.8732 + x(4)*0 + x(5)*229298796.101610 +x(6)*486339030716.172)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.08649)^2;
%violet
f(2) = ((x(1)*1129000866482.21 + x(2)*1148116457625.13 +x(3)*746124874241.071 +x(4)*1938895002.91122+x(5)*209908629676.911+x(6)*22127444173.2269)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.268534)^2;
%blue
f(3) = ((x(1)*1356136056216.68 + x(2)*117924056606.891 +x(3)*1006802150711.77 +x(4)*2074325480814.89+x(5)*160860872330.231+x(6)*1962227450.00629)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.285348)^2;
%red
end
%% VALIDATION
% Output across the 6 channels at 1% output:
red = [1356136056216.68 117924056606.891 1006802150711.77 2074325480814.89 160860872330.231 1962227450.00629];
blue = [1129000866482.21 1148116457625.13 746124874241.071 1938895002.91122 209908629676.911 22127444173.2269];
violet = [265092064.751006 3009732967.55533 17335448078.8732 0 229298796.101610 486339030716.172];
denom = [2952496003117.50 1762459756770.75 4050006857763.86 2637843000090.09 1174923525460.69 513647641481.489];
% Preallocating solution matrix
truesoln =zeros(length(solution),6);
for i = 1:length(solution)
redfcn(i,1) = (red*solution(i,:)')./(denom*solution(i,:)');
bluefcn(i,1) = (blue*solution(i,:)')./(denom*solution(i,:)');
violetfcn(i,1) = (violet*solution(i,:)')./(denom*solution(i,:)');
if redfcn(i,1) >= 0.2710 & redfcn(i,1) <= 0.299615 & bluefcn(i,1) >=0.2554 & bluefcn(i,1) <= 0.28196 & violetfcn(i,1) <=0.09082 & violetfcn(i,1) >=0.08217
truesoln(i,:) = solution(i,:);
end
end
The first portion of the code %%Optimization tries to calculate the situation mentioned and the second portion of the code %%Validation works to verify whether output across the channels is indeed what was intended.
After running this code, I'm met with the following error message:
>> Optimization
Error: File: Optimization.m Line: 22 Column: 1
Function definitions in a script must appear at the end of the file.
Move all statements after the "attempt" function definition to before the first local function definition.
As the code used to work in the past, I'm wondering if this is simply a formatting or syntax issue. My knowledge of MATLAB is limited and I apologize if this is a very noob question. Any help is appreciated!
0 个评论
回答(1 个)
Aman
2023-8-2
Hi,
I understand that you have code that does optimisation for LED channels, but you are getting the "Function definitions in a script must appear at the end of the file" error on executing it.
As the error message says, all the local function definitions in a MATLAB script should be present at the end, but in the code shared, they are presented in the middle. In order to solve this issue, you just need to move the function to the end of the script. The modified code will be as follows:
%% Optimization
opts = optimoptions('gamultiobj',"PlotFcn","gaplotpareto","PopulationSize",10000);
lb = [0 0 0 0 0 0];
ub = [1 1 1 1 1 1];
[solution,ObjectiveValue] = gamultiobj(@attempt,6,[],[],[],[],lb,ub,[],opts);
%% VALIDATION
% Output across the 6 channels at 1% output:
red = [1356136056216.68 117924056606.891 1006802150711.77 2074325480814.89 160860872330.231 1962227450.00629];
blue = [1129000866482.21 1148116457625.13 746124874241.071 1938895002.91122 209908629676.911 22127444173.2269];
violet = [265092064.751006 3009732967.55533 17335448078.8732 0 229298796.101610 486339030716.172];
denom = [2952496003117.50 1762459756770.75 4050006857763.86 2637843000090.09 1174923525460.69 513647641481.489];
% Preallocating solution matrix
truesoln =zeros(length(solution),6);
for i = 1:length(solution)
redfcn(i,1) = (red*solution(i,:)')./(denom*solution(i,:)');
bluefcn(i,1) = (blue*solution(i,:)')./(denom*solution(i,:)');
violetfcn(i,1) = (violet*solution(i,:)')./(denom*solution(i,:)');
if redfcn(i,1) >= 0.2710 & redfcn(i,1) <= 0.299615 & bluefcn(i,1) >=0.2554 & bluefcn(i,1) <= 0.28196 & violetfcn(i,1) <=0.09082 & violetfcn(i,1) >=0.08217
truesoln(i,:) = solution(i,:);
end
end
%%
function f = attempt(x)
f(1) = ((x(1)*265092064.751006 + x(2)*3009732967.55533 + x(3)*17335448078.8732 + x(4)*0 + x(5)*229298796.101610 +x(6)*486339030716.172)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.08649)^2;
%violet
f(2) = ((x(1)*1129000866482.21 + x(2)*1148116457625.13 +x(3)*746124874241.071 +x(4)*1938895002.91122+x(5)*209908629676.911+x(6)*22127444173.2269)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.268534)^2;
%blue
f(3) = ((x(1)*1356136056216.68 + x(2)*117924056606.891 +x(3)*1006802150711.77 +x(4)*2074325480814.89+x(5)*160860872330.231+x(6)*1962227450.00629)/(x(1)*2952496003117.50+x(2)*1762459756770.75+x(3)*4050006857763.86 +x(4)*2637843000090.09+x(5)*1174923525460.69+x(6)*513647641481.489)-0.285348)^2;
%red
end
Please refer to the following documentation to learn more about local functions in MATLAB.
I hope it helps!
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Surrogate Optimization 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!