2
$\begingroup$

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[[4]] (x2[t] + x3[t]), 
 x2'[t] == x[[4]] (x1[t] + 0.15 x2[t]), 
 x3'[t] == x[[4]] (0.2 + x3[t] (x1[t] - 3.5)), x1[0] == x[[1]], 
 x2[0] == x[[2]], x3[0] == x[[3]]}, {x1, x2, x3}, {t, 1 + dt}];
 R = {x1[1] - x[[1]], x2[1] - x[[2]], x3[1] - x[[3]], 
 x1[1 + dt] - x1[dt], x2[1 + dt] - x2[dt], 
 x3[1 + dt] - x3[dt]} /. SOL[[1]];
 Return[R];]
$\endgroup$
13
  • $\begingroup$ 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. $\endgroup$
    – MarcoB
    Commented Feb 16, 2016 at 20:58
  • $\begingroup$ 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. $\endgroup$ Commented Feb 16, 2016 at 20:59
  • 1
    $\begingroup$ "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? $\endgroup$ Commented Feb 16, 2016 at 21:42
  • 1
    $\begingroup$ So you're trying to find parameters for the Rossler attractor using LM. You might be interested in using ParametricNDSolve[] instead. $\endgroup$ Commented Feb 17, 2016 at 20:01
  • 2
    $\begingroup$ 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 $\endgroup$
    – mhass
    Commented Feb 17, 2016 at 21:51

1 Answer 1

1
$\begingroup$
FindMinimum[3 (x - 4)^2 + 5 (y + 2)^2 - 9, {x, y}, 
 Method -> "LevenbergMarquardt"]

(* {-9., {x -> 4., y -> -2.}} *)

$\endgroup$
1
  • $\begingroup$ the function I wrote takes a vector of 4 elements and returns a vector of 6 elements, and I don't know how to express my function output as a sum of squares. I want to optimize the input to minimize the output as possible (closer to zero). $\endgroup$
    – mhass
    Commented Feb 16, 2016 at 21:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.