MATLAB: Fminunc with partial sum

fminuncsummation

Hi, I am facing the following problem where I have sum in my objective for fminunc. Here's a minimum example of the code (I know that this minimum example could be solved without fminunc, but I would like to see how that works for a more complicated problem). The following code does not work, leaving the "syms t" away, Matlab complains about undefined t. The crucial point is that I want to solve for a vector y_p using fminunc where in the objective function I am building a sum of specific elements of y_p.
function [y_p, y_t]=min_example(y, lambda)
T=length(y);
syms t
myobjective=@(y_p) ones(1,T)*(y-y_p)+...
lambda*symsum((y_p(t+1)-2*y_p(t)+y_p(t-1)),t,2,T-1);
y_p = fminunc(myobjective,y);
y_t = y - y_p;
end
Any help is highly appreciated. Thanks!

Best Answer

Yes you are correct, I forgot the squares in this example
function [y_p, y_t]=min_example(y, lambda)
myobjective=@(y_p) norm(y-y_p).^2 + lambda*norm( diff(y_p,2) ).^2;
y_p = fminunc(myobjective,y);
y_t = y - y_p;
end