MATLAB: How to run optmization on vectors

fminsearchoptimization

Hello,
Please, I would like to ask for some help on a procedure that I'm running.
I have some large vectors that are input for a model that is solved through Least Squares Method. The model is a function of 6 parameters (the vectors v1[], v2[], v3[] and v4) and the 2 vectors to be optmized (x1[] and x2[]). The function dOptTgt is the quadratic sum of the errors of the model.
function dOptT = dOptTgt(x1,v1,v2,x2,v3,v4)
t1=f1(x1,v1,v2,x2,v3,v4) %math only

t2=f2(x1,v1,v2,x2,v3,v4) %math only
a1=(t1-v3)./v3
a2=(t2-v4)./v4
dOptT=(a1.^2+a2.^2)*10000
end
I have tried to use fminsearch() on the vectors,
x1=...
x2=...
fun=@(x) dOptTgt(x(:,1),v1,v2,x(:,2),v3,v4)
x0=[x1,x2]
Y=fun(x0)
x=fminsearch(fun,x0)
but got an error message. As it seems, fminsearch cannot be run on vectors:
Attempt to execute SCRIPT fminsearch as a function
Error in fminsearch (line 22)
x=fminsearch(fun,x0)
Also tried lsqnonlin(), not sure if the problem was correctly setup; the outputs afer fitting do not make the residuals zero, but just a good approximation.
x1=...
x2=...
fun=@(x) dOptTgt(x(:,1),v1,v2,x(:,2),v3,v4)
x0=[x1,x2]
options = optimoptions('lsqnonlin','Display','iter')
[x,resnorm] =lsqnonlin(fun,x0,[],[],options)
The solution I put in place was a loop selecting the elements i of each vector and using fminsearch().
x1=...
x2=...
for i=1:size(...)
fun=@(x) dOptTgt(x(1),v1(i),v2(i),x(2),v3(i),v4(i)))
x0=[x1(i),x2(i)]
a =fminsearch(fun,x0)
end
It works, but takes a lot of time to run, and doesn't seem to be the most efficient way to do that.
Please, is there a way to run this problem more efficiently?
Thank you,
G

Best Answer

That particular error message has several possible causes:
  1. You have accidentally called your own MATLAB script fminsearch which interferes with using the MATLAB one; or
  2. You are using an older release of MATLAB that implemented fminsearch as a .m and you accidentally inserted some text before the "function" statement, thereby accidentally converting the function .m into a script .m . This would require R2014b or earlier; or
  3. You have a corrupt installation and instead of being able to find the fminsearch.p file all it can find is the .m file that gives the help information; or
  4. you do not have a license to execute fminsearch and all that MATLAB can find instead is the version that has the help information. In the particular case of fminsearch this would not apply as fminsearch is part of basic MATLAB, but this could occur for other functions.