Quasi newton method for optimization

I am trying to solve the above objective function for theta using quasi newton method. But I am getting the following error. Can somebody please help me fix this error?
Following is my matlab code:
Code for defining objective function:
function f = objfun(x_t,c_n,theta)
t=0:length(x_t)-1;
L=length(t);
n=0:length(c_n)-1;
N=length(n);
for i=1:L
for j=1:N
f=@(theta) sum((x_t(i)-sum(c_n(j).*exp(-((t(i)-n(j)*a)^2/theta^2))))^2)
end
end
end
Code for calling objective function:
a = 1;
x_t=rand(32,1);
c_n=rand(32,1);
f = @(theta) objfun(x_t,c_n,theta)
theta0 = 20;
options = optimoptions('fminunc','Algorithm','quasi-newton');
[theta, thetaval] = fminunc(f,theta0,options)
%

 Réponse acceptée

Alan Weiss
Alan Weiss le 22 Juin 2018
Try this for your function:
function f = objfuntc(x_t,c_n,theta)
L = length(x_t);
N = length(c_n);
f = 0;
for t = 1:L
xs = 0;
for n = 1:N
xs = xs + c_n(n)*exp(-(((t-n)/theta)^2));
end
f = f + (x_t(t) - xs)^2;
end
Call it like this:
a = 1;
x_t = rand(32,1);
c_n = rand(32,1);
f = @(theta) objfuntc(x_t,c_n,theta)
theta0 = 20;
options = optimoptions('fminunc','Algorithm','quasi-newton');
[theta, thetaval] = fminunc(f,theta0,options)
Alan Weiss
MATLAB mathematical toolbox documentation

5 commentaires

HN
HN le 14 Oct 2020
What if one wants to see all the values of theta and function evaluated values ?
I've some similar problem. I would love to post my question if you have time to share your thoughts.
Thank you
Alan Weiss
Alan Weiss le 14 Oct 2020
Are you talking about a history of the iterations? Use an output function. This example has an output function with some history; adapt it as you like.
Alan Weiss
MATLAB mathematical toolbox documentation
HN
HN le 14 Oct 2020
Modifié(e) : HN le 15 Oct 2020
My function has three optimization parameters . These are rp, alpha and beta. The objecive function f is evaluated for minimal output. These three variables must simultaneously create a grid and the the function will be evaluated with every point and check for minimum. Is this quasi-newton algorithm ok for this problem Alan Weiss ? How about my program ? is it right ?
Thank you
function f= objfun(rp,alpha,beta)
rp=1000; % initial value
L=1000; % leg length - Equal for all legs
alpha=2*pi/3; % intial vlaue
beta=4*pi/3; % intial vlaue
z=707.1068; % Height the platform
ts=1/50;
t=0:ts:1;
for k=1:length(t)
for j=1:length(t)
th(j)=-0.2*cos(2*pi*t(j));
psi(k)=0.2*sin(2*pi*t(k));
phi(j)=atan2(sin(psi(k))*sin(th(j)),(cos(psi(k))+cos(th(j))));
R=Rot('y',th(j))*Rot('x',psi(k))*Rot('z',phi(j));
x(k)=1/2*rp*(-cos(phi(j))*cos(psi(k))+cos(phi(j))*cos(th(j))+sin(phi(j))*sin(psi(k))*sin(th(j)));
y(k)=-rp*cos(psi(k))*sin(phi(j));
Gcc=[Gc(1:3,1:2),Gc(1:3,6)]
C(:,:,:)=pinv(-Gcc)*Gcp % matrix
f=@(rp,alpha,beta) sum((C'*C)*vp) % objective function
end
end
end
Alan Weiss
Alan Weiss le 14 Oct 2020
Sorry, I don't know what you are asking. Please start a new question with a clear problem statement.
Alan Weiss
MATLAB mathematical toolbox documentation
HN
HN le 15 Oct 2020
Ok, thank you.

Connectez-vous pour commenter.

Plus de réponses (1)

victor d
victor d le 12 Fév 2019

0 votes

In my Research (data in Excel format )
5 independent variables
1 dependent variables.
So i need to do the following neural Network algorithms.
1.Backpropagation algorithms
2.conjugate gradient method
3.Quasi-Newton method

Catégories

En savoir plus sur Optimization Toolbox dans Centre d'aide et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by