Optimization problem using Quasi Newton method

Given x_t and c_mn, the objective function is defined as:
I am trying to solve the above objective function for theta using quasi newton method. But there seem to be some problem with my code as it's not working. Could somebody please help me with this?
Following is my matlab code:
Code for defining objective function:
function f = objfuntc(x_t,c_mn,theta )
L = length(x_t );
[M,N] =size(c_mn);
f = 0 ;
for t = 1:L
xs = 0 ;
for n = 1:N
for m=1:M
xs = xs + c_mn(m,n)*exp(1i*2*pi*m*(t - n -2) + theta);
end
end
f = f + (x_t(t) - xs)^2 ;
end
Code for calling objective function:
x_t = rand(16,1 );
L = length(x_t);
a = 1;
M = L;
c = rand(M*L/a,1 );
c_mn=reshape(c,[M,L/a]);
J = @(theta) objfuntc(x_t,c_mn,theta )
theta0 = 10 ;
options = optimoptions('fminunc','Algorithm','quasi-newton ');
[theta, thetaval] = fminunc(J,theta0,options)

댓글 수: 12

Why do you want to write your own optimizer in MATLAB? That is generally a bad idea, when optimization tools written by professionals are readily available.
christina
christina 2018년 6월 24일
I am using MATLABs fminunc. But my problem is, I can't seem to run the code.
Torsten
Torsten 2018년 6월 25일
The variable you want to return from "objfun" is called "J", not "f".
christina
christina 2018년 6월 25일
Ahh, sorry that was a typo here. I still have the same problem.
Torsten
Torsten 2018년 6월 25일
J = @(theta) objfun(x_t,c_mn,theta )
instead of
J = @(theta) objfuntc(x_t,c_mn,theta )
christina
christina 2018년 6월 25일
Still not fixed.
Torsten
Torsten 2018년 6월 25일
편집: Torsten 2018년 6월 25일
Put this code in one file, name it "main.m", open it in MATLAB and run it.
function main
x_t = rand(16,1 );
L = length(x_t);
a = 1;
M = L;
c = rand(M*L/a,1 );
c_mn=reshape(c,[M,L/a]);
J = @(theta) objfuntc(x_t,c_mn,theta )
theta0 = 10 ;
options = optimoptions('fminunc','Algorithm','quasi-newton ');
[theta, thetaval] = fminunc(J,theta0,options)
end
function f = objfuntc(x_t,c_mn,theta )
L = length(x_t );
[M,N] =size(c_mn);
f = 0 ;
for t = 1:L
xs = 0 ;
for n = 1:N
for m=1:M
xs = xs + c_mn(m,n)*sin(2*pi*m*(t - n -2) + theta);
end
end
f = f + (x_t(t) - xs)^2 ;
end
end
Many thanks, that worked! But if I slightly change the objective function, it shows some error again. I don't understand why. Could you please help me with this?
function main
x_t = rand(16,1 );
L = length(x_t );
a = 1 ;
M = L ;
c = rand(M*L/a,1 );
c_mn=reshape(c,[M,L/a ]);
J = @(theta) objfuntc(x_t,c_mn,theta )
theta0 = 10 ;
options = optimoptions('fminunc','Algorithm','quasi-newton ');
[theta, thetaval] = fminunc(J,theta0,options )
end
function f = objfuntc(x_t,c_mn,theta )
L = length(x_t );
[M,N] =size(c_mn );
f = 0 ;
for t = 1:L
xs = 0 ;
for n = 1:N
for m=1:M
% xs = xs + c_mn(m,n)*sin(2*pi*m*(t - n -2) + theta );
xs = xs + c_mn(m,n)*exp(1i*2*pi*m*(t - n -2) + theta );
end
end
f = f + (x_t(t) - xs)^2 ;
end
end
Torsten
Torsten 2018년 6월 25일
편집: Torsten 2018년 6월 25일
x_t and c_mn can be complex-valued ?
You will have to use
f = f + (x_t(t) - xs)*conj(x_t(t) - xs) ;
instead of
f = f + (x_t(t) - xs)^2 ;
Best wishes
Torsten.
christina
christina 2018년 6월 25일
It can be complex-valued.
Torsten
Torsten 2018년 6월 25일
I complemented my comment.
christina
christina 2018년 6월 25일
Thank you so much! That fixed everything.

댓글을 달려면 로그인하십시오.

답변 (0개)

카테고리

도움말 센터File Exchange에서 Problem-Based Optimization Setup에 대해 자세히 알아보기

제품

릴리스

R2016a

태그

질문:

2018년 6월 24일

댓글:

2018년 6월 25일

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by