How to run optmization on vectors?

3 views (last 30 days)
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

Accepted Answer

Walter Roberson
Walter Roberson on 17 Feb 2019
Edited: Walter Roberson on 17 Feb 2019
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.
  2 Comments
Guilherme Candido
Guilherme Candido on 17 Feb 2019
Hi Walter, thank you for your reply.
Indeed it was alternative 1, my script file had the same name. I have changed it to fminsearchMode and now I get another error message:
Assignment has more non-singleton rhs dimensions than non-singleton subscripts
Error in fminsearch (line 200)
fv(:,1) = funfcn(x,varargin{:});
Error in fminsearchMode (line 22)
x =fminsearch(fun,x0)
I was getting this error before, not sure if I understand. Please, any idea if it is fixable?
Thank you,
G
Walter Roberson
Walter Roberson on 17 Feb 2019
That error message would suggest that your function is returning something that is larger than a scalar. In the code you provided it is not obvious why that could be, other than the fact that you did not provide source for f1 or f2 so we have to assume that either f1 or f2 or both might be returning something larger than a scalar.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!