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일

1 개 추천

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

Charbel
Charbel 2023년 3월 26일
@Matt J Thank you for the answer. Should I define the vector c beforehand though?
Matt J
Matt J 2023년 3월 26일
Not for the purpose of creating the anonymous function. Obviously, you need an initial guess c0.
Charbel
Charbel 2023년 3월 26일
@Matt J thanks a lot!
Matt J
Matt J 2023년 3월 26일
You're quite welcome, but please Accept-click the answer to indicate that your quesiton is resolved.
Charbel
Charbel 2023년 3월 26일
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 2023년 3월 26일
It should, but you can easily check it.
Charbel
Charbel 2023년 3월 26일
ok thank you.
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.
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개)

카테고리

도움말 센터File Exchange에서 Get Started with Optimization Toolbox에 대해 자세히 알아보기

제품

릴리스

R2023a

질문:

2023년 3월 26일

댓글:

2023년 3월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by