Trying to plot results from fmincon
9 views (last 30 days)
Show older comments
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
0 Comments
Answers (1)
Alan Weiss
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
2 Comments
Alan Weiss
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
See Also
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!