Write the f(c) as an anonymous function

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!

 Akzeptierte Antwort

Matt J
Matt J am 26 Mär. 2023
Bearbeitet: Matt J am 26 Mär. 2023

1 Stimme

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 Kommentare

Charbel
Charbel am 26 Mär. 2023
@Matt J Thank you for the answer. Should I define the vector c beforehand though?
Matt J
Matt J am 26 Mär. 2023
Not for the purpose of creating the anonymous function. Obviously, you need an initial guess c0.
Charbel
Charbel am 26 Mär. 2023
@Matt J thanks a lot!
Matt J
Matt J am 26 Mär. 2023
You're quite welcome, but please Accept-click the answer to indicate that your quesiton is resolved.
Charbel
Charbel am 26 Mär. 2023
One more question, when I run the code with that function f(c), will it do the sum based on all the (x,y) pairs?
Matt J
Matt J am 26 Mär. 2023
It should, but you can easily check it.
Charbel
Charbel am 26 Mär. 2023
ok thank you.
Walter Roberson
Walter Roberson am 26 Mär. 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.
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.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Produkte

Version

R2023a

Gefragt:

am 26 Mär. 2023

Kommentiert:

am 26 Mär. 2023

Community Treasure Hunt

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

Start Hunting!

Translated by