Write the f(c) as an anonymous function

3 views (last 30 days)
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!

Accepted Answer

Matt J
Matt J on 26 Mar 2023
Edited: Matt J on 26 Mar 2023
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 Comments
Walter Roberson
Walter Roberson on 26 Mar 2023
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 on 26 Mar 2023
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.

Sign in to comment.

More Answers (0)

Categories

Find more on Get Started with Optimization Toolbox in Help Center and File Exchange

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!