Commands-based MATLAB Plotting
Cheng-Liang Chen
PSE
LABORATORY
Department of Chemical Engineering National TAIWAN University
Chen CL
1
One Typical 2D Plot
Use of commands
Use plot properties editor
Chen CL
2
One Typical 2D Plot t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02])
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2 0
1
2
3
4
5
6
7
Chen CL
3
One Typical 2D Plot t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1,’r-o’) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02])
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2 0
1
2
3
4
5
6
7
Chen CL
4
One Typical 2D Plot t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’) ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02])
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2 0
1
2
3
4
5
6
7
Chen CL
5
One Typical 2D Plot t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02])
2.5
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2
−2.5 0
1
2
3
4
5
6
Chen CL
6
One Typical 2D Plot t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02])
2.5
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2
−2.5 0
1
2
3
4
5
6
Chen CL
7
One Typical 2D Plot hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02]) t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1) plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),... axis([0 2*pi -2.5 2.5]); 2.5
2
1.5
1
0.5
0
−0.5
−1
−1.5
−2
−2.5 0
1
2
3
4
5
6
Chen CL
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... ’FontSize’,18,... 2 ’Color’,[0,0,0]) 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); text(2.6,2*sin(2.5),... ’\leftarrow\it start !’,... −1 ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... ’\it check this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13])
8
6
Chen CL
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... 2 ’FontSize’,18,... ’Color’,[0,0,0]) 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); text(2.6,2*sin(2.5),... −1 ’\leftarrow\it start !’,... ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... Time ’\it check this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13])
9
6
Chen CL
10
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... 2 ’FontSize’,18,... ’Color’,[0,0,0]) 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); text(2.6,2*sin(2.5),... −1 ’\leftarrow\it start !’,... ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... Time ’\it check this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13]) n o i t a c o L
6
Chen CL
11
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... 2 ’FontSize’,18,... ’Color’,[0,0,0]) start! 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); text(2.6,2*sin(2.5),... −1 ’\leftarrow\it start !’,... ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... Time ’\it check this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13]) ←
n o i t a c o L
6
Chen CL
12
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... 2 ’FontSize’,18,... ’Color’,[0,0,0]) start! 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); text(2.6,2*sin(2.5),... −1 ’\leftarrow\it start !’,... check this point ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... Time ’\it check this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13]) ←
n o i t a c o L
→
6
Chen CL
13
set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) XLabel(’\it\bf Time’,... 2 ’FontSize’,18,... ’Color’,[0,0,0]) start! 2sin(t) 1 h_y=YLabel(’\bf\it Location’); set(h_y,’FontSize’,18,... 0 ’Color’,[1,0,.5]); .5cos(t) text(2.6,2*sin(2.5),... −1 ’\leftarrow\it start !’,... check this point ’FontSize’,14,... −2 ’Color’,[.18,.5,.02]); 0 2 4 text(3.95,2*sin(4), ... Time ’\it c\rhoheck this point \rm\rightarrow’,... ’FontSize’,14,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13]) ←
n o i t a c o L
→
6
Chen CL
14
Use Property Editor Use Figure Editor 2
! r t a t s
←
1 n o i t a 0 c o L
2sin(t)
.5cos(t)
−1
check this point → −2
0
2
4
Time (min)
6
Chen CL
One Typical 2D Plot: MATLAB Code t_1 = 0:0.25:2*pi; t_2=0:.5:2*pi; y_1 = 2*sin(t_1); y_2 = .5*cos(t_2); plot(t_1, y_1)%, ’r-o’) %%%%%%% plot(t_1, y_1, ’r-o’) %%%%%%% plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’) %%%%%%% plot(t_1,y_1,’Color’,[1,0,0], ... ’LineStyle’,’-’, ... ’LineWidth’,3, ... ’Marker’,’o’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.21,.12,1],... ’MarkerFaceColor’,[.21,.12,1]),...
15
Chen CL
axis([0 2*pi -2.5 2.5]); %%%%%%%%% hold on plot(t_2,y_2,’Color’,[.6,.2,.6],... ’LineStyle’,’:’, ... ’LineWidth’,3, ... ’Marker’,’^’, ... ’MarkerSize’,6, ... ’MarkerEdgeColor’,[.18,.5,.02],... ’MarkerFaceColor’,[.18,.5,.02]) %%%%%%%% set(gca, ’XTick’,[0,2,4,6], ... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTick’,[-2,-1,0,1,2], ... ’YTickLabel’,{’-2’,’-1’,’0’,’1’,’2’},... ’FontSize’,16,’LineWidth’,3) %%%%%%%% XLabel(’\it\bf Time’,... ’FontSize’,18,... ’Color’,[0,0,0]) %%%%%%%% h_y=YLabel(’\bf\it Location’);
16
Chen CL
set(h_y,’FontSize’,18,... ’Color’,[1,0,.5]); %%%%%%%% text(2.6,2*sin(2.5),... ’\it{\leftarrow start!}’,... ’FontSize’,16,... ’Color’,[.18,.5,.02]); %%%%%%%% text(3.95,2*sin(4), ... ’\it{check this point \rm\rightarrow}’,... ’FontSize’,16,’Color’,[.5,.25,.25],... ’HorizontalAlignment’,’right’); %%%%%%%% gtext(’\bf 2sin(t)’,’FontSize’,16,’Color’,’magenta’) gtext(’\bf .5cos(t)’,’FontSize’,16,’Color’,[1,.39,.13])
17
Chen CL
18
Graphical Optimization One Profit Maximization Problem A company manufactures two machines, A and B . Using available resources either 28 A or 14 B machines can be manufactured each day. The sales department can sell up to 14 A machines or 24 B machines. The shipping facility can handle no more than 16 machines each day. The company makes a profit of 400 on each A machine and 600 on each B machine. How many A and B machines should the company manufacture every day to maximize profit ?
Chen CL
19
x1
= # of A machine manufactured each day
= # of B machine manufactured each day profit = 400x1 + 600x2 f (x) = −(400x1 + 600x2) x1 + x2 ≤ 16 (shipping and handling constraint) 1 + 142 ≤ 1 (manufacturing constraint) 28 1 + 242 ≤ 1 (limitation on sales department) 14 −x1 ≤ 0; g5 : − x2 ≤ 0 x2
⇒
g1 : g2 : g3 : g4 : ⇒
x
x
x
x
∗
x
= (4, 12);
f (x ) = − 8800 ∗
Chen CL
20
% ProfitMax.m % for Profit Maximization Problem % [x1,x2] = meshgrid(0:0.5:25, 0:0.5:25); prof = 400*x1+600*x2; g1 = x1+x2-16; g2 = x1/28+x2/14-1; g3 = x1/14+x2/24-1; g4 = -x1; g5 = -x2; cla reset; axis([0 25 0 25]) set(gca,’XTick’,[0,5,10,15,20,25],... ’YTick’,[0,5,10,15,20,25],... ’XTickLabel’,{’0’,’5’,’10’,’15’,’20’,’25’},... ’YTickLabel’,{’0’,’5’,’10’,’15’,’20’,’25’},... ’FontSize’,16,’LineWidth’,3)
Chen CL
xLabel(’\bf x_1’,’FontSize’,16,’Color’,[0,0,0]) yLabel(’\bf x_2’,’FontSize’,16,’Color’,[0,0,0]) title(’\bf Profit Maximization Problem’,... ’FontSize’,16,’Color’,[0,0,0]) hold on cv = [0 0]; profv = [2000, 5000, 8800, 12000, 15000]; const1 = contour(x1,x2,g1,cv,’r’,’LineWidth’,3); const2 = contour(x1,x2,g2,cv,’m’,’LineWidth’,3); const3 = contour(x1,x2,g3,cv,’b’,’LineWidth’,3); const4 = contour(x1,x2,g4,cv,’k’,’LineWidth’,3); const5 = contour(x1,x2,g5,cv,’k’,’LineWidth’,3); profs = contour(x1,x2,prof,profv,’k-’); clabel(profs)
21
Chen CL
22
text( 1.1, 15,’\bf g_1’,’FontSize’,12,’Color’,[1,0,0]) text( 15, 7,’\bf g_2’,’FontSize’,12,’Color’,[1,0,1]) text( 1.1, 22,’\bf g_3’,’FontSize’,12,’Color’,[0,0,1]) text(-0.05,20.75,’\bf g_4’,’FontSize’,12) text( 18, 0.85,’\bf g_5’,’FontSize’,12) plot(4,12,’g’,’Marker’,’o’,’MarkerSize’,6) text( 1, 5,’\it\bf Feasible Region’,’FontSize’,14) text( 15, 20,’\it\bf Infeasible Region’,’FontSize’,14) hold off
Chen CL
23
Linear Programming: Simple Examples
Case 1:
unique solution Min f = −4x1 − x2 + 50 s.t. x1 − x2 ≤ 2 x1 + 2x2 ≤ 8 x1, x2 ≥ 0
Chen CL
24
MATLAB Code for Case 1 % LP: Case 1 - Single solution [x1,x2] = meshgrid(0:0.1:6, 0:0.1:6); f = -4*x1-2*x2+50; g1 = x1-x2-2; g2 = x1+2*x2-8; g3 = -x1; g4 = -x2; cla reset; set(gca,’XTick’,[0,2,4,6],... ’YTick’,[0,2,4,6],... ’XTickLabel’,{’0’,’2’,’4’,’6’},... ’YTickLabel’,{’0’,’2’,’4’,’6’},... ’FontSize’,16,’LineWidth’,3) xLabel(’\bf x_1’,’FontSize’,16,’Color’,[0,0,0]) yLabel(’\bf x_2’,’FontSize’,16,’Color’,[0,0,0]) title(’\bf LP Case 1: unique solution’,... ’FontSize’,16,’Color’,[0,0,0])
Chen CL
hold on cv = fv = const1 = const2 = const3 = const4 = fs =
25
[0 0]; [25, 30, 35, 40, 45]; contour(x1,x2,g1,cv,’r’,’LineWidth’,3); contour(x1,x2,g2,cv,’b’,’LineWidth’,3); contour(x1,x2,g3,cv,’k’,’LineWidth’,3); contour(x1,x2,g4,cv,’k’,’LineWidth’,3); contour(x1,x2,f,fv,’k-’); clabel(fs) text(3.1, 1,’\bf\leftarrow g_1:x_1- x_2=2’,’FontSize’,12, text(1.3,3.5,’\bf\leftarrow g_2:x_1+2x_2=8’,’FontSize’,12, text( 1,0.2,’\bf g_3’,’FontSize’,12) text(0.1, 2,’\bf g_4’,’FontSize’,12) plot( 4, 2,’g’,’Marker’,’o’,’MarkerSize’,6) text( 1, 2,’\it\bf FR’, ’FontSize’,14) text(3.75,2.75,’\it\bf Infeasible Region’,’FontSize’,14) hold off
Chen CL
26
Thank You for Your Attention Questions Are Welcome