Hi Dennis,
I see that you are trying to plot the cost of every iteration of "fminunc" for every call to the function. You want the plot to update after every call such that the previous plot is not erased. You have the correct idea of doing it through the "PlotFcn" option in the "optimoptions".
You can accomplish your objective by creating a custom plotting function which takes the state of the "fminunc" and the figure handle as inputs and updates the figure with the new values. For this you will need to create the figure outside your for loop and then after every call to "fminunc" you need to save the figure. You can refer to the following code to understand how to do it.
function myOptimPlotFunction(state, figHandle)
% Custom plotting function that updates the figure with the current cost
figure(figHandle); % Make the figure current
plot(state.iteration, state.fval, 'bo-'); % Plot current value
xlabel('Iteration');
ylabel('Cost');
drawnow; % Force MATLAB to render the figure
end
num_labels = ...; % Number of labels
options = optimoptions(@fminunc, 'OutputFcn', @(state,~,~)myOptimPlotFunction(state, figHandle), 'PlotFcn', []);
for i = 1:num_labels
figHandle = figure; % Create a new figure for each label
initial_theta = zeros(n + 1, 1);
[all_theta(i,:), ~, ~, output] = fminunc(@(theta)(my_costfunction(theta, X, (y == i), lambda)), initial_theta, options);
% After optimization, save the figure
saveas(figHandle, sprintf('cost_function_label_%d.png', i));
% You might want to close the figure if you don't want it to be open after saving
% close(figHandle);
end
Here are the main changes introduced in the code:
- "myOptimPlotFunction" is the plotting function that will be used to plot the fval against iterations. Here "drawnow" is used to update the figure after every plot.
- The "optimoptions" are specified such that it will use "myOptimPlotFunction" as the "PlotFcn".
- Inside the for loop, a saveas command is executed to save the figure after every call to "fminunc".
- "my_costfunction" is the placeholder cost function you can replace with your cost function.
This will update the plot accordingly and the file will also be saved after every "fminunc" call.
For more information on "drawnow","saveas" and custom plotting functions, refer to the below documentation:
- drawnow:https://www.mathworks.com/help/matlab/ref/drawnow.html
- saveas:https://www.mathworks.com/help/matlab/ref/saveas.html
- Custom plotting function:https://www.mathworks.com/help/optim/ug/output-functions.html
I hope this helps.