Trying to plot results from fmincon

andré nilsson (view profile)

on 13 Feb 2019
Latest activity Commented on by Alan Weiss

Alan Weiss (view profile)

on 13 Feb 2019
Hi im trying to plot the returning x-values from fmincon optimization. The fmincon is running in a for loop and returning 2 different x-values(x(1) and x(2)) for every iteration. I want to plot this x values as two different curves(one for x(1) and one for x(2)) in the same graph. The problem is that i only can graph 'o'(rings), I want lines and I want x(1) and x(2) separated. how do I solve this.
for n=1:1:7
if n==1
nonlcon = @nonlcon1;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(1,xopt,'--o')
hold on
elseif n==2
nonlcon = @nonlcon2;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(2,xopt,'--o')
hold on
elseif n==3
nonlcon = @nonlcon3;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(3,xopt,'o')
hold on
elseif n==4
nonlcon = @nonlcon4;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(4,xopt,'o')
hold on
elseif n==5
nonlcon = @nonlcon5;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(5,xopt,'bo-')
hold on
elseif n==6
nonlcon = @nonlcon6;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(6,xopt,'bo-')
hold on
elseif n==7
nonlcon = @nonlcon6;fun=@fun1;
[xopt,fopt] = fmincon(fun,x0,A,b,Aeq,beq,LB,UB,nonlcon)
plot(7,xopt,'o')
hold on
set(gca,'XTickLabel',{'Mon','Tue','wed','Thu','Fri','sat','Sun'})
xlabel('day');
ylabel('hours');
end
end
this is how it look right now

Alan Weiss (view profile)

on 13 Feb 2019

I think that you want your horizontal axis to be the iteration number and your vertical axis to be the x(1) or x(2) value.
This example shows, among other things, how to obtain the history of points visited. From there I think that it should be easy to create the appropriate plots.
Alan Weiss
MATLAB mathematical toolbox documentation

andré nilsson

andré nilsson (view profile)

on 13 Feb 2019
Yes, you´re correct that I want to plot the x values on the y-axis and the iterations on the x-axis of the graph. My problem is that I want to split the x values from the fmincon so they become x(1) and x(2), so I can just plot 2 lines in the graph. but right now I just get the ouput x= value 1 value 2. From your link I couldnt find any info to work around this, so is there a way so get output x(1) and x(2) separatly?
Alan Weiss

Alan Weiss (view profile)

on 13 Feb 2019
Suppose you have the history in your output function, with each row of the history is one iteration, and column 1 corresponds to x(1). Then something like
y1 = history(:,1); % x(1) values
y2 = history(:,2); % x(2) values
t = 1:length(y1); % iterations 1 to niter
plot(t,y1,'b-',t,y2,'r-') % plots x(1) in blue, x(2) in red
legend('x(1)','x(2)')
Alan Weiss
MATLAB mathematical toolbox documentation