Write the f(c) as an anonymous function
Mostrar comentarios más antiguos
I have to test the following problem in an optimization course.
The following function f(c) is used to fit a set of data Dn.
$$
f(c)=\sum_{i=1}^{n}|c_1\ x_i+c_2+c_3 e^{(-(x_i-c_4)^2/c_5)}-y_i\ |^2\
\\c=\left(\begin{matrix}c_1\\\begin{matrix}c_2\\\begin{matrix}c_3\\\begin{matrix}c_4\\c_5\\\end{matrix}\\\end{matrix}\\\end{matrix}\\\end{matrix}\right)\in\mathbb{R}^5\\
D_n=\left\{\left(x_i,\ y_i\right)\ \right|\ i=1,\ \ldots,\ n)
$$


I wish to minimize this function, using the steepest descent algorithm, shown below:
function x=steep_descent_non_quad(f,nablaf,x0,tol,kmax)
% f is a scalar function on R^n
% nablaf is a vector function on R^n
x=x0;r=nablaf(x0);RelErr=1;k=1;n0=norm(x0);
while (k<=kmax)&&(RelErr>tol)
% Compute the optimal step
alpha=optim(f,d,x,tol/2);
% The function optim seeks an approximation to the exact optimal alpha_k
% Using either bisection or golden section
x=x-alpha*r;
RelErr=abs(alpha)*norm(d)/n0;
% Compute new remainder
r=nablaf(x);
k=k+1;
end
I want to write the function f(c) in a way that fits its use in the algorithm.
Let's say I write it this way
c = ones(5,1);
f = @(c)(sum(abs(c(1)*x + c(2) + c(3)*exp(-(x-c(4)^2 / c(5) - y)^2))));
1- Is this correct?
Will this function provide the sum from i to n?
Or should I write it otherwise?
2- can MATLAB directly give me the gradient vector of f or should I program it myself?
Thank you for your help!
Respuesta aceptada
Más respuestas (0)
Categorías
Más información sobre Get Started with Optimization Toolbox en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!