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

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 ?
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.
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
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.
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일
I'll save you some headache: the solutions are clearly
x = ones(n,1);

카테고리

도움말 센터File Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

태그

질문:

2018년 11월 9일

댓글:

2018년 11월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by