Write the f(c) as an anonymous function

2 次查看(过去 30 天)
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!

采纳的回答

Matt J
Matt J 2023-3-26
编辑:Matt J 2023-3-26
Is this correct?
There is no need for abs(). Also, your division and exponentiation operations need to be element-wise,
f = @(c) sum( ( c(1)*x + c(2) + c(3)*exp(-(x-c(4)).^2 ./ c(5) - y).^2 );
can MATLAB directly give me the gradient vector of f or should I program it myself?
If you want an analytical gradient calculation, you must do it yourself. However, there are file exchange tools that will implement a finite difference derivative calculation, if you're willing to settle for that, e.g.,
  9 个评论
Walter Roberson
Walter Roberson 2023-3-26
With the c being in R, the abs() would be needed only if x or y are in C... we are not actually told that they are in R.
Torsten
Torsten 2023-3-26
I think a bracket is missing:
f = @(c) sum( ( c(1)*x + c(2) + c(3)*exp(-(x-c(4)).^2 ./ c(5) ) - y).^2 );
instead of
f = @(c) sum( ( c(1)*x + c(2) + c(3)*exp(-(x-c(4)).^2 ./ c(5) - y).^2 );
And maybe you have to assume c(5) > 0 in the optimizer.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Surrogate Optimization 的更多信息

产品


版本

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by