MATLAB: Maximization problem

maxoptimization

I was wondering if someone could help me with maximisation problem in MATLAB.
I need to maximise sum[-ln(v(i)) – u^2(i)/v(i)], where v(i) = S^2(i) and the following formula is given for S^2:
S^2(n) = w + A*u^2(n-1) + B*S^2(n-1).
I have data set for u(i) for i=1….n. i.e. u(i)'s are given.
Additional info:
w = G*V; V = w/(1-A-B).
A + B + G = 1.
Basically, I need to find optimal parameter values for w, A and B which maximise the sum I gave above.
My data set is too long, so I can't use Excel and I would really appreciate it if someone could help me out with the code. Thanks.

Best Answer

If I'm interpreting correctly, your v (aka S^2) is recursively defined. So presumably you have some way to determine u(0) and S(0)^2. Based on that, you can write a function to determine v(k) either as a matrix calculation or using a loop.
v = @(x) (diag(ones(n,1),0)+diag(-x(3)*ones(n-1,1),-1))\(x(1)+[x(2)*u0^2+x(3)*v0;x(2)*u(1:end-1).^2]);
Now define your objective function
f = @(x) sum(-log(v(x)) - u.^2./v(x));
Then apply a minimization routine
xmin = fminsearch(f,x0)
were x0 is your initial guess for x = [w;A;B].
EDIT TO ADD Based on comments below, here's a fuller solution:
n = 123;
v0 = 0;
u = %get u;
u0 = u(1,1);
v = @(x) (diag(ones(n,1),0)+diag(-x(3)*ones(n-1,1),-1))\(x(1)+[x(2)*u0^2+x(3)*v0;x(2)*u(1:end-1).^2]);
f = @(x) -sum(-log(v(x)) - u.^2./v(x));
x0 = [0.001; 0.0626; 0.8976];
xmin = fmincon(f,x0,[0,1,1],1,[],[],[-Inf;0;0],[Inf;1;1],[],optimset('Algorithm','interior-point'))
-f(xmin)
If you have Global Optimization Toolbox, you can do this multiple times:
ms = MultiStart;
problem = createOptimProblem('fmincon','x0',x0,...
'objective',f,'lb',[-Inf;0;0],'ub',[Inf;1;1],'Aineq',[0,1,1],'bineq',1,'options',optimset('Algorithm','interior-point'));
xmin = run(ms,problem,30)
-f(xmin)
(change the 30 to whatever you consider sufficient). If you don't have Global Opt TB, you could program it yourself. Make a grid of initial points, loop over them, and keep the best solution. Or do a loop and use a random x0 each time.
Related Question