Numerical Optimization: Write a numerical objective function

I have to minimize a numerical objective function of the form:
min Σ(x(i+1)-x(i))^2, i=1..10 and x(i) are the variables to be optimized
I tried using the symssum but doesn't give any results and I have not found anything else in the documentation. Does anyone have any idea how to write this? Thanks in advance.

 採用された回答

Walter Roberson
Walter Roberson 2019 年 1 月 17 日

0 投票

@(x) sum(diff(x).^2)

4 件のコメント

Thalassia Nouni
Thalassia Nouni 2019 年 1 月 17 日
Thank you for your answer, but this returns always the derivative of x, so it is always 1.
Walter Roberson
Walter Roberson 2019 年 1 月 17 日
It would only return the derivative if you are passing a symbolic variable to it; you should be passing a numeric vector to it.
For extra certainty you could code
@(x) sum( (x(2:end) - x(1:end-1)).^2 )
Thalassia Nouni
Thalassia Nouni 2019 年 1 月 19 日
Thank you, this works perfectly!!

サインインしてコメントする。

その他の回答 (0 件)

Community Treasure Hunt

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

Start Hunting!

Translated by