Difficulty implementing simulated annealing algorithm

While there are exaplanations for this (see the following link), I couldn't figure out exactly how to implement it:
This is the code that I am typing, based on the info in the link:
type simple_objective
function y = simple_objective(x)
x1 = x(1);
x2 = x(2);
y = (4-2.1.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-4+4.*x2.^2).*x2.^2;
ObjectiveFunction = @simple_objective;
x0 = [0.5 0.5]; % Starting point
rng default % For reproducibility
[x,fval,exitFlag,output] = simulannealbnd(ObjectiveFunction,x0)
I don't understand what exactly is wrong with that code (I keep getting an error). I am very new to MATLAB and would appreciate any help!

 採用された回答

Stephan
Stephan 2019 年 6 月 25 日

0 投票

rng default % For reproducibility
x0 = [0.5 0.5]; % Starting point
[x,y,flag,output] = simulannealbnd(@simple_objective,x0)
function y = simple_objective(x)
x1 = x(1);
x2 = x(2);
y = (4-2.1.*x1.^2+x1.^4./3).*x1.^2+x1.*x2+(-4+4.*x2.^2).*x2.^2;
end

3 件のコメント

Abbi Hashem
Abbi Hashem 2019 年 6 月 25 日
I still get the following error:
Undefined function or variable 'x'.
Is there something else to add to the code to make this error go away?
Stephan
Stephan 2019 年 6 月 25 日
did you exactly copy my code? it runs for me with result:
Optimization terminated: change in best function value less than options.FunctionTolerance.
x =
-0.0896 0.7130
y =
-1.0316
flag =
1
output =
struct with fields:
iterations: 2948
funccount: 2971
message: 'Optimization terminated: change in best function value less than options.FunctionTolerance.'
rngstate: [1×1 struct]
problemtype: 'unconstrained'
temperature: [2×1 double]
totaltime: 1.1943
Abbi Hashem
Abbi Hashem 2019 年 6 月 25 日
I was typing something wrong, it just did, thank you so much!

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

その他の回答 (0 件)

カテゴリ

質問済み:

2019 年 6 月 25 日

コメント済み:

2019 年 6 月 25 日

Community Treasure Hunt

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

Start Hunting!

Translated by