Marquardt's Method

For Multiple Variable Functions

Problem:
Consider the minimization of the following functions:  and the starting point x
(0)= [0,0]T
Write  MATLAB codes to find a minimum of the above function by using  Marquardt's Method.

Solution:

here is the working version of the Marquardt's Method implemented in Matlab, it is strongly based on MATLAB symbolic toolbox so if you do not have this toolbox installed on your computer it won't work, x01 and x02 are starting points for searching and a='(x1^2+x2-11)^2+(x1+x2^2-7)^2' represents the function so just by modifying these parameters you can run this scripts and find the optimal points of a function (actually it finds just minimum points, to find the maximum points run the program with -f(x)

Copyright:

One can use this code in any project and by any means, this code is not copyrighted so feel free to use this code in your projects and homework, however it would be really appreciable if you inform me if you used this code to do an awesome project (maybe we can introduce your project in this page), however like anyone else, I'm not responsible of the damages made by this code.

 

%Marquardt's Method for Multiple Variable Functions

clc;
close all;
%Insert starting points
x01=0;
x02=0;
%Function F in Symbolic Format
a='(x1^2+x2-11)^2+(x1+x2^2-7)^2';
%Insert the desired magnitude of final direction vector (0.0001 is fine)
eserror=0.0001;
Max_Iterations=20000;
convergance_criterion=0.0001;

k=0
gamma=100

%Leave the rest unchanged
%=================================
syms x1 x2
syms alpha real %difining alpha as real prevents matlab to solve function with conj(alpha)

% Hessian Of F

A=[diff(diff(a,x1),x1),diff(diff(a,x1),x2);...
    diff(diff(a,x2),x1),diff(diff(a,x2),x2)]
%First Order Derivative of F

B=[diff(a,x1);diff(a,x2)];
% pretty(simplify(A))


subs(subs(A,x1,x01),x2,x02)

mag_of_B=sqrt(subs(subs(diff(a,x1),x1,x01),x2,x02)^2+subs(subs(diff(a,x2),x1,x01),x2,x02)^2)
records=[];
while (mag_of_B>convergance_criterion)&(k<Max_Iterations)
    k=k+1;
    temp=([x01;x02]-inv(subs(subs(A,x1,x01),x2,x02)+gamma*eye(size(subs(subs(A,x1,x01),x2,x02))))*(subs(subs(B,x1,x01),x2,x02)))'
    disp('x1 x2');
    [x01;x02]
    disp('Hk')
    subs(subs(A,x1,x01),x2,x02)
    disp('gamm es');
    gamma*eye(size(subs(subs(A,x1,x01),x2,x02)))
    disp('diff');
    (subs(subs(B,x1,x01),x2,x02))

    disp('------------');
    F_eval_old=subs(subs(a,x1,x01),x2,x02);
    x01=temp(1,1);
    x02=temp(1,2);
    F_eval_new=subs(subs(a,x1,x01),x2,x02);
    if F_eval_new<F_eval_old
        gamma=.5*gamma;
    else
        gamma=2*gamma;
    end
    mag_of_B=sqrt(subs(subs(diff(a,x1),x1,x01),x2,x02)^2+subs(subs(diff(a,x2),x1,x01),x2,x02)^2)
    records=[records;[k,x01,x02,subs(subs(a,x1,x01),x2,x02)]];

end
records

Home | last update Aug 28, 2008