how to find the minimum of a function with certain boundaries defined by another function
Show older comments
Hi all,
I would like to use 5 characteristic peaks to imitate a signal by changing the relative intensity of the peaks. c1-c5 are the relative intensities (my variables).
The constrain is the output (x_temp and y_temp) from function "color" need to be close to [0.5 0.5], then I try to look for the c1-c5 values that produce the minimum of function "rmse". function color and rmse are both predefined and not listed here.
Here is the code I have written until now. Using for loops for 5 times looks quite dummy, and in the future I might need to do the same thing for 10 peaks...can i write this in an elegant way, for example with functions? I read about fminsearch, but I don't know how to add constrains. Can somebody help? Thanks in advance.
p.s. I don't have the optimization toolbox, so fmincon doesn't work for me.
p1 = 21; p2 = 21; p3 = 6; p4 = 6; p5 = 6; % how many points
nr_point = p1*p2*p3*p4*p5; % the number of data points
cvalue = zeros(nr_point,6);
c1 = linspace(1,5,p1); % the range of each variable
c2 = linspace(2,3,p2); %
c3 = linspace(1,1.2,p3);
c4 = linspace(0.8,1,p4);
c5 = linspace(0.25,0.45,p5);
cvalue = zeros(nr_point,5); % cvalue saves all the tested c1-c5 values
result = zeros(nr_point,1); % result saves the result of function rmse
for t1 = 1:p1
for t2 = 1:p2
for t3 = 1:p3
for t4 = 1:p4
for t5= 1:p5
% peak1-5 are predifined n*1 vectors, the following tries to modify relative intensity:
peak_temp = peak1*c1(t1) + peak2*c2(t2) + peak3*c3(t3) + peak4*c4(t4)+ peak5*c5(t5);
% color is the name of a predefined function, it returns 3*1 matrix
colorspace = color(peak_temp);
nr = p2*p3*p4*p5*(t1-1)+p3*p4*p5*(t2-1)+p4*p5*(t3-1)+p5*(t4-1)+t5; % this is quite dummy...
x_temp = colorspace(1); x_diff(nr) = x_temp - 0.5;
y_temp = colorspace(2); y_diff(nr) = y_temp - 0.5; % the difference with [0.5 0.5]
result(nr) = rmse(peak_temp);
end
end
end
end
end
index = find(abs(x_diff)<0.001 & abs(y_diff)<0.001); % my constrain
[a,b] = min((result(index))); % minimum of the output of function "rmse"
cvalue(index(b),:); % the c1-c5 values
Answers (0)
Categories
Find more on L*a*b* Color Space in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!