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?

 Accepted Answer

Torsten
Torsten on 9 Nov 2018
Edited: Torsten on 9 Nov 2018
The examples given here should show you how to proceed:
https://de.mathworks.com/help/matlab/ref/fminsearch.html

8 Comments

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.
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
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.
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 !!

Sign in to comment.

More Answers (1)

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

Categories

Community Treasure Hunt

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

Start Hunting!