Minimize non linear function with undefined vector

I have to do the picture's question.
I already built a function for the function to minimize:
function z=f(x)
n = size(x,2);
z = symsum(100(x(1,i)-x(1,i-1).^2).^2 + ...
(1-x(1,i-1))^2,i, 2, n);
end
Now, I want to use the fminunc matlab function to finish the exercise. However, I have several questions:
1) I don't know where I can mention that x is the 2d vector, then 10, 100, 1000? 2) As there is no specifications about x0, I don't know how to say that I want to start from - infinite.
Any ideas?

 採用された回答

Torsten
Torsten 2018 年 11 月 9 日
編集済み: Torsten 2018 年 11 月 9 日

0 投票

The examples given here should show you how to proceed:
https://de.mathworks.com/help/matlab/ref/fminsearch.html

8 件のコメント

Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
In this link, it's only a 2d vector, si you can simply write by hand the function. Here, I want to generalize it for different values of n.
Torsten
Torsten 2018 年 11 月 9 日
編集済み: Torsten 2018 年 11 月 9 日
function main
n = 10000;
x0 = zeros(n,1)
sol = fminunc(@fun,x0)
end
function z = fun(x)
z = 0;
for i = 2:numel(x)
z = z + ...
end
end
Can you fill the "..." with your function definition ?
Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
編集済み: Nolwen Brosson 2018 年 11 月 9 日
Thank you for your code. So, if you say that x0 is a n-dimension vector, matlab know that x (in the fun function), will be a n-dimension vector?
I still have an error, but I guess the hardest part is done :)
n = 10000;
x0 = zeros(n,1);
[sol, fminval] = fminunc(@fun,x0);
function z=f(x)
z = symsum(100*(x(1,i)-x(1,i-1).^2).^2 + ...
(1-x(1,i-1))^2,i, 2, numel(x));
end
Torsten
Torsten 2018 年 11 月 9 日
編集済み: Torsten 2018 年 11 月 9 日
Numerical solvers don't work with symbolic variables. Just fill the '...' with the expression you want to sum.
Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
編集済み: Nolwen Brosson 2018 年 11 月 9 日
function main
n = 10000;
x0 = zeros(n,1);
[sol, fminval] = fminunc(@fun,x0);
end
function z = fun(x)
z = 0;
for i = 2:numel(x)
z = z + 100*(x(i,1)-x(i-1,1).^2).^2 + ...
(1-x(i-1,1))^2
end
end
Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
I get this error with this code: fminunc stopped because the size of the current step is less than the default value of the step size tolerance.
Torsten
Torsten 2018 年 11 月 9 日
編集済み: Torsten 2018 年 11 月 9 日
For which value of n did you get this message ?
Start with small values for n.
And you should remove the semicolon behind
[sol, fminval] = fminunc(@fun,x0);
to see the solution.
Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
Oh yeah it works !!! Thank you !! It was just the semicolon. Thanks !!

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

その他の回答 (1 件)

Bruno Luong
Bruno Luong 2018 年 11 月 9 日
編集済み: Bruno Luong 2018 年 11 月 9 日

0 投票

I'll save you some headache: the solutions are clearly
x = ones(n,1);

1 件のコメント

Nolwen Brosson
Nolwen Brosson 2018 年 11 月 9 日
I know, but I just want to solve it with matlab

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

カテゴリ

ヘルプ センター および File ExchangeStartup and Shutdown についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by