How does [theta, cost] = fminunc(@(t)(costFunction(t, X, y, lambda)), startingTheta, options) work?
Show older comments
I have inserted a snippet of my code. I'm trying to understand exactly how this function works. I'm a beginner in machine learning so please try to walk me through it.
%My code
% Set options for fminunc, maximum iterations allowed is 500
options = optimset('MaxIter', 500);
% fminunc()
% Optimization function that finds the min point of cost function J
fprintf('Run fminunc');
[theta, cost] = fminunc(@(t)(costFunction(t, X, y, lambda)), startingTheta, options);
fprintf('Done \n');
fprintf('Theta: %f \n', theta);
fprintf('Cost at theta found by fminunc: %f\n', cost);
On stack exchange, I found an answer that stated
X = 1; y = 1; % Defining variables which aren't passed into the costFunction
% but which must exist for the next line to pass them as anything!
f = @(t)(costFunction(t, X, y)); % Explicitly define costFunction as a function of t alone
[theta, cost] = fminunc(f, 0, options);
which confused me even more. My code works, but i want to completely understand the usage of this function. My exact question is, what values are being passed into t, X, and y? Nowhere in my code do i define t to equal anything and the code runs fine, how? The user i quoted above also stated the X and y variables arent passed into the costFunction... How does this function run without any values being passed into its parameters? Any help for a beginner would be greatly appreciated, thanks!
1 Comment
"On stack exchange, I found an answer that stated ..." more half-baked MATLAB-related advice on stack exchange. Whilst interesting to read and for getting ideas, stack exchange is not a particularly good resource for accurate descriptions of how MATLAB works. For that you should read the documentation, the blogs, and this forum.
Accepted Answer
More Answers (0)
Categories
Find more on Get Started with Statistics and Machine Learning Toolbox in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!