# Using the Levenberg - Marquardt algorithm to minimize a user-defined function

I wrote a function in Matlab that optimizes another user defined function using lsqnonlin with 'levenberg-marquardt' option. Now, I'd like to use the first to optimize a user defined function written in Mathematica.

An alternative solution would be to explain how to use Levenberg - Marquardt optimization in Mathematica because I searched a lot about it and can't understand how to use it.

The objective function takes a vector of 4 elements and returns a vector of 6 elements.I don't know how to express the output as a sum of squares. I want it to be closer to zero as possible.

This is the main code

{clc , clear all, close all %c l e a r screen and v a r i a b l e s
global p N a %set global parameters p N x0 a
a (1)=0.15; %system parameter a
a (2)=0.2; %system parameter b
a (3)=3.5; %system parameter c
N=3; %s i z e of system
p=2; %i n t e g e r p i s chosen so that the s i z e of the
%r e s i d u a l i s >= than the q u a n t i t i e s to be optimized ( s i z e of
%r e s i d u a l i s given by p?N
x0 (1)=7; %i n i t i a l guess f o r x1 (0)
x0 (2)=7; %i n i t i a l guess f o r x2 (0)
x0 (3)=7; %i n i t i a l guess f o r x3 (0)
%Quantities to be optimized v0
%For t h i s case only x0 (1) , x0 (2) , x0 (3) , and T are considered
v0(1)=x0(1) ;
v0(2)=x0(2) ;
v0(3)=x0(3) ;
T=4; %i n i t i a l guess f o r period
v0(4)=T;
%Set l s q n o n l i n to implement the LMA algorithm
OPTIONS = optimoptions('lsqnonlin','Algorithm','levenberg-marquardt','TolX',1e-16);
%,'TolX ' , 1 e ?12 , 'TolFun ' , 1 e ?12 , ' MaxIter ' , 1 0 0 0 ) ;
%Call in the r e s i d u a l function to find optimized q u a n t i t i e s
x = lsqnonlin('abcd2',v0,[],[],OPTIONS) ;}


And this is the objective function

{function R = abcd2(b0)
global p N a
dt =1/2^10; %s e t the s t e p s i z e
% Rossler system written in dimensionless time
xdot =@(t,x)[b0(4)*(-x(2)-x(3));b0(4)*(x(1)+a(1)*x(2));b0(4)*(a(2)+x(3)*(x(1)-a(3)))];
%perform numerical i n t e g r a t i o n to f i n d x ( 1 ) , x ( dt ) , and x(1+dt )
[t,vv]=ode45(xdot,[0 1],[b0(1);b0(2);b0(3)]) ;
[t1,vv1]=ode45(xdot,[0 dt],[b0(1);b0(2);b0(3)]);
[t2,vv2]=ode45 (xdot,[0 1+dt],[b0(1);b0(2);b0(3)]);
R = zeros(p*N,1); %p r e a l l o c a t i n g f o r the r e s i d u e
k = size(vv) ; %f i n d l a s t value from i n t e g r a t i o n
k1 = size(vv1) ;
k2 = size(vv2) ;
%build r e s i d u a l
R(1)=vv(k(1),1)-b0(1); %R(1)=x1(1) -x1 (0)
R(2)=vv(k(1),2)-b0(2); %R(2)=x2(1) -x2 (0)
R(3)=vv(k(1),3)-b0(3); %R(3)=x3(1) -x3 (0)
R(4)=vv2(k2(1),1)-vv1(k1(1),1); %R(4)=x1(1+dt)-x1 ( dt )
R(5)=vv2(k2(1),2)-vv1(k1(1),2); %R(5)=x2(1+dt)-x1 ( dt )
R(6)=vv2(k2(1),3)-vv1(k1(1),3); %R(6)=x3(1+dt)-x1 ( dt )
end}


This is the function i want to optimize

residue[x_] := Block[{R = Table[1, {6}], dt = 2^-10, SOL},
SOL = NDSolve[{x1'[t] == -x[] (x2[t] + x3[t]),
x2'[t] == x[] (x1[t] + 0.15 x2[t]),
x3'[t] == x[] (0.2 + x3[t] (x1[t] - 3.5)), x1 == x[],
x2 == x[], x3 == x[]}, {x1, x2, x3}, {t, 1 + dt}];
R = {x1 - x[], x2 - x[], x3 - x[],
x1[1 + dt] - x1[dt], x2[1 + dt] - x2[dt],
x3[1 + dt] - x3[dt]} /. SOL[];
Return[R];]

• Have you tried using FindMinimum? You can set the optimization algorithm using Method -> "LevenbergMarquardt". See this tutorial on (Introduction to Local Minimization) for an introduction. – MarcoB Feb 16 '16 at 20:58
• FindMinimum[] is able to use Levenberg-Marquardt (or Gauss-Newton) if you can express your objective function as a sum of squares. The advanced documentation for the optimization functions should have a few words on this. – J. M. is away Feb 16 '16 at 20:59
• "I don't know how to express my function output as a sum of squares." - then Levenberg-Marquardt might not even be the most appropriate optimization to use. In any event, how does one speak of a "minimum vector"? Minimum with respect to its norm? If so, which norm? – J. M. is away Feb 16 '16 at 21:42
• So you're trying to find parameters for the Rossler attractor using LM. You might be interested in using ParametricNDSolve[] instead. – J. M. is away Feb 17 '16 at 20:01
• I'm trying to find the period T and initial conditions that will solve for the periodic solution (the initial conditions that lies on the limit cycle or periodic orbit) which minimizes the residue (x(T)-x(0)) to be equal to zero – mhass Feb 17 '16 at 21:51

FindMinimum[3 (x - 4)^2 + 5 (y + 2)^2 - 9, {x, y},