can't turn on the jacobian for lsqnonlin

7 views (last 30 days)
Sushant  Kumar
Sushant Kumar on 28 May 2013
i have defined my vector objective function like this....
function [r5,jacr5] = myfunction5(N)
global F1
F1 = 1008000;
r = zeros(10000,1);
f = zeros(10000,1);
jacr5 = zeros(10000,3);
for p = 1:10000
r(p,1) = 5 * sin(2*3.14*F1*(p/(10000000))+0.3);
end
for q1 = 1:10000
f(q1,1) = N(1) * sin((2*3.14*N(2)*(q1/10000000)) + N(3));
end
r5 = r-f;
if nargout>1
jacr5(:,1) = - myfunction1(N(2),N(3));
jacr5(:,2) = - myfunction2(N(1),N(2),N(3));
jacr5(:,3) = - myfunction3(N(1),N(2),N(3));
end
end
where myfunction1 goes something like....
function r1 = myfunction1(y,z)
f1 = zeros(10000,1);
for q1 = 1:10000
f1(q1,1) = sin((2*3.14*y*(q1/10000000)) + z);
end
r1 = f1;
end
similar is myfunction2 and 3.
Now in the main function where i have called lsqnonlin(), i have tried to turn on the jacobian but the "options" statement is basically doing nothing. Also when i tried to change the tolerance to 1e-8 by writing
options = optimset('Jacobian','on','TolX',1e-8);
it was of no use. Because of this, i am constantly getting this message when i run the optimization...
Local minimum possible.
lsqnonlin stopped because the final change in the sum of squares relative to its initial value is less than the default value of the function tolerance.
value of N thus achieved is not as close to the answer as it should be. Please suggest something soon.

Answers (2)

Alan Weiss
Alan Weiss on 28 May 2013
Did you pass the options to the solver? I mean, when you call lsqnonlin, did you include options something like this:
x = lsqnonlin(fun,x0,lb,ub,options)
Alan Weiss
MATLAB mathematical toolbox documentation
  1 Comment
Sushant  Kumar
Sushant Kumar on 29 May 2013
ya i included these lines in the main code...
options = optimset('Jacobian','on','Display','iter'); [N,resnorm,residual,exitflag,output,lambda,jacob] = lsqnonlin(@myfunction5,n, [inf 988000 -inf],[inf 1011000 inf],options);
but i don't think the jacobian is on bacuse when checked with optimtool "lsqnonlin".....it still shows jacobian off.
i basically need the value for N(2) close to F1(1008000) which in this case i am getting N(2) = 1000473.14 which is too much of error by my requirements. Actually, N(1), N(2), N(3) should be very near to 5, 1008000 and 0.3, their counterparts in function r. and this should work for any set of amplitude, frequency and phase chosen in function r. Please suggest somthing soon.

Sign in to comment.


Alan Weiss
Alan Weiss on 29 May 2013
Did you get iterative display? Yes? Then the options were accepted and used by the solver.
To check the values of options, do not issue an optimset command. Just enter "options" at the command line. You will see the values in the options structure.
As far as your problem of not getting accurate enough solutions, there are several possibilities. One is to use pi instead of 3.14 in your formulas. Another is to give a better initial guess, such as taking the result N and feeding it back in as n, the initial guess. For more suggestions, see the documentation.
Alan Weiss
MATLAB mathematical toolbox documentation
  2 Comments
Sushant  Kumar
Sushant Kumar on 30 May 2013
ya..i am getting the display. actually the problem now is different. the optimizer is searching for a solution very close to the initial point. for example, i want it to detect frequency in the range 989000 to 101100 when starting point is 1000000. but it is giving me correct answers only in the region 999000 to 1001000. if i choose F1 outside this region, then the optimizer is giving me answer very far from the coorect value for F1. i have tried putting the final point as new initial point and used pi as well. no use apparently. i need it to search for a little far away points, not just in the close vicinity.
Sushant  Kumar
Sushant Kumar on 30 May 2013
i tried checking the near by points too. e.g for F1 = 996000, values of N(1), N(2),N(3) returned was near 1.6, 994569, 1.6. so i defined delta = [4 1430 -1.3] and displayed [myfunction5(N) myfunction5(N+delta) myfunction5(N-delta)]. To my confirmation myfunction5(N+delta term was almost zero which is what is desired. So, it is confirmed that actual minimum exist. Just that the optimizer is not getting there. how to fix? inputs appreciated!

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!