Trouble with optimization of two variables based on data set
Show older comments
Hello, Im an trying to use some of MATLABs optimization algorithms to essentially find the best fit value for two unknown variables. The equation Im working with is as follows:
W = X * 10^(-Y * Z)
In this equation I have the values of W and Z, and am trying to optimize the best value for X and Y. In addition to this, the variables of W and Z are 1020*1020 arrays of varying values. Therefore, I would Ideally want to use a for loop that iterates across all these values performing that optimization with a best fit approach. I have been attemtping to use a few different methods that I have before such as Fmincon/Fminsearch but am running into some problems.
Here is one of the ways I was attemtping to do this by minimizing the sum of the difference from each iteration. However to begin with I would primarily just like to get a value for any location of i & j. Which I can solve for myself but cannot get MATLAB to do for me.
(With Fmincon using Optimizer Tool)
function f = objectiveFcn(optimInput, W, Z)
% Edit the lines below with your calculation
X = optimInput(1);
Y = optimInput(2);
sum_1 = 0;
for i = 1:1020
for j = 1:1020
diff_1 = (X * 10.^(-Y * (Z(i, j)))) - W(i, j);
sum_1 = sum_1 + diff_1.^2;
end
end
f = diff_1;
end
One problem when doing this is the error message: Not enough input arguments. I understand this as when using the optimize tool I can set the function inputs but can only set one variable as a fixed input and not both W/Z. Is there a way around this or have I missed something?
I tried to use the function shown above as it should essentially remain the same for fminsearch when not using the optimizer tool (I renamed it two_var). This was posted by a member of the community and I tried to change it to fit my needs.
[a1,fv1] = fminsearch(@two_var, optimInput, Z, W);
[a2,fv2] = fminsearch(@(x, y)norm(two_var(x, y)), optimInput , W, Z);
My understanding is the first fminsearch returns the the minimum of the function, and the second one returrns minimum that is greater than zero. I am Error saying Argument 3 must be an option structure. Does this imply I am also unable to use to fixed input variables in this function? If that Is the case does anyone know of a method I can explore to find the find the result I am looking for and I can investigate it.
Thanks in advance
Answers (1)
Torsten
on 29 Mar 2022
Try this code:
Lq = Lq(:);
Tq = Tq(:);
n = numel(Lq);
A = [ones(n,1),-Lq];
b = log10(Tq);
sol_lin = A\b;
sol0 = [exp(sol_lin(1));sol_lin(2)];
fun = @(p) Tq - p(1)*10.^(-p(2)*Lq);
sol_nlin = lsqnonlin(fun,sol0);
Kq = sol_nlin(1)
Aq = sol_nlin(2)
Categories
Find more on Surrogate Optimization 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!