0

How can I plot the following data onto a contour instead?

close all;
clear all;
clc;
syms x 
f=@(x) -100.*(x(2)-x(1).^2).^2+ (1-x(1)).^2;
x0=[-9, 1];
options=optimset('display', 'iter', 'TolX',1e-6); 
%options = optimset('PlotFcns',@optimplotfval);
x=fminsearch(f, x0, options)
a= %need help pulling this from fminsearch    [a,b,c]=fminsearch(f,x0,options] ???
b=%need help pulling this from fminsearch
fcontour(f, 'Fill', 'On');
hold on;
plot(a,b,'*-r');
xlim([-50 50]);
ylim([0 45]);plot

I would like to plot the point (a,b) for which the function is minimum on a contour tracing convergence towards the center.

Mixter
  • 193
  • 9
  • [`fminsearch`](https://www.mathworks.com/help/matlab/ref/fminsearch.html) will return `[x,TC]` which is the `x` that achieved minimum function value `TC`. Are `a` and `b` accessible from `a = x(1)` and `b=x(2)`? – SecretAgentMan Nov 26 '19 at 15:58
  • For the plotting, there are related posts for contour plots ([1](https://stackoverflow.com/q/16868074/8239061), [2](https://stackoverflow.com/q/56994963/8239061), [3](https://stackoverflow.com/q/56780316/8239061)). – SecretAgentMan Nov 26 '19 at 16:03
  • That's what I've been thinking [a,b] = fminsearch(....) inside a for loop and save each (a,b) to plot on the contour and draw the minimization path. – Mixter Nov 26 '19 at 16:08
  • Did you read [`fminsearch`](https://www.mathworks.com/help/matlab/ref/fminsearch.html) documentation? It will only return the final solution but the example shows how to get the steps along the way. – SecretAgentMan Nov 26 '19 at 16:09
  • Can you clarify what you're defining `a` and `b` as? I see you have `x = [x1 x2]`. Do you define `a = x1` and `b = x2`? And `c = f(x)`? – SecretAgentMan Nov 26 '19 at 16:11
  • a and b were meant to be updated with the coordinates of the point corresponding to the minimum of the function. Which I think is the 'x' from ['x',TC] on your first comment. The steps along the way is what I need to plot on a contour. – Mixter Nov 26 '19 at 16:25
  • Ok, thank you. This function appears unbounded if `x` isn't constrained. I'm getting the plot and the solutions, but the function marches off to `-inf` unless you constrain `x`. You can arbitrarily keep increasing `x` to make `f` get smaller. Is this expected? – SecretAgentMan Nov 26 '19 at 16:27
  • The points should converge to a minimum. But the Tolerance should be the constraint. I am thinking there is some manipulation to be done (mathematically), but I wanted the coutour and line trace for better visualization. Thanks – Mixter Nov 26 '19 at 16:31
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/203122/discussion-between-secretagentman-and-mixter). – SecretAgentMan Nov 26 '19 at 16:33
  • You can [edit] your question to update it with the refined problem statement for context and include the updated question. – SecretAgentMan Nov 26 '19 at 17:08

0 Answers0