%%%%%%%%%%%%%% %% GRAPHS %% %%%%%%%%%%%%%% ================= === 2D graphs === ================= ============================== === simple graphs (ez...) ==== ============================== ezplot('x*sin(x)') % on interval < -2*pi, 2*pi > or: f = 'x*sin(x)'; ezplot(f) ezplot(f, [-2, 10]) % on interval < -2, 10 > % or ezplot(f, -2, 10) ======= for 2 variables ============ ezplot('x^2 + y^2 - 4') % curve given by x^2+y^2-4 = 0 ezsurf('x^2 + y^2 - 4') colorbar ezmesh('x^2 + y^2 - 4') ezcontour('x^2 + y^2 - 4') f = 'x^2 - y^2-5'; ezplot(f, [-10, 10]) % in a square ezplot(f, [-10, 10, -5, 5]) % in a rectangle ----- graphical solution of equations --- ezplot('x^2 + 1') % the first curve: x^2 + 1 = 0 hold on % next plot to the same figure ezplot('x^2 + y^2 - 4') % the second curve hold off % next plot wipe away the previous one axis([-2 2 0 3]) % zoom in or: xlim([-1.5 , 1.5]) % interval on x axis ylim([1.5 , 2]) % interval on y axis axis equal % units of the same length axis tight % axis limits = range of the data ezsurf('x^2 + y^2 - 4') hold on ezsurf('-x^2 + y + 8') ============================= ==== command plot =========== ============================= x = -pi:0.3:pi; % points on x axis (from -pi to pi, step 0.3) y = x.*sin(x); % points on y axis (function values) plot(x, y) % points connected by blue lines plot(x,y, 'r') % red line plot(x,y, 'r', 'linewidth', 2) % thicker red line plot(x,y, 'r*') % points marked as red asterisks plot(x,y, 'k--o', 'linewidth', 2) % dashed black line, circles hold on plot(x,y, 'mo', 'linewidth', 6) % bigger magenta circles hold off ----------------------- help plot % other possibilities =============================== ==== figure properties ======== =============================== grid on grid off grid minor on xlabel('x: (-\pi, \pi)') % text for x axis xlabel('x: (-\pi, \pi)', 'FontSize',16) ylabel('x*sin(x)') % text for y axis title('Plot of the function x*sin(x)') set(gca,'fontsize', 16) % size of tick labels text(x,y,'text') % text at position [x,y] with regard to axes legend('legend') --- or: h = legend('legend') set (h, "fontsize", 16); === properties: == Rotation, FontSize, FontWeight, Color == \bf, \it, \rm == \alpha, \beta, ... == x_{ij}, x^{pt} title('\bf The values of x_{ij}', 'FontSize',20) --- logarithmic scales x=linspace(1,10,40); y = x; semilogy(x,y) semilogx(x,y) y=x.^2; loglog(x,y) x = logspace(0,3,40); % from 1 to 10^3 y = linspace(0,3,40); % from 0 to 3 plot(x,y) --- more functions in one graph --- x = pi*[-1:0.1:1]; y1 = sin(x); y2 = cos(x).^2; y3 = exp(x); plot(x, y1, x, y2, 'r', x, y3, 'k') legend('sin(x)','cos(x)^2', 'e^x') legend('sin(x)','cos(x)^2', 'e^x', 'location', 'northwest') --- or : plot(x, [y1; y2; y3]) % rows of 2. matrix plot(x, [y1' y2' y3']) % columns of 2. matrix --- or one after another --- plot(x, y1) hold on % continue on the same picture plot(x, y2) plot(x, y3) hold off % next time opens a new picture ------- more plots in one figure ---- subplot(3,1,1); plot(x,y1) % division 3 rows, 1 columns, 1-st fig. subplot(3,1,2); plot(x,y2) subplot(3,1,3); plot(x,y3) --- managing more figures --- f1 = figure; % 1-st figure window f2 = figure; % 2-nd figure window f3 = figure; % 3-rd fig. wind. plot(x, y1) % draws to actual (i.e. 3.) fig. wind. figure(f1); % switch to 1. fig. plot(x, y1) % draws to 1. fig. figure(f2); % switch to 2. fig. plot(x, y2) % draws to 2. fig. close(f2) % close 2. fig. close all % close all fig. --- parametric curve --- t = 0 : 0.2 : 2*pi; x = sin(t); y = cos(t); plot(x, y) axis equal axis normal =========================== ======= 3D graphs ========== =========================== t = 0 : pi/50 : 10* pi; plot3(sin(t), cos(t), t, 'r*') zlabel('z') ============ graph on a rectangle mesh ================ x = 0 : 0.3 : 10; % mesh on x axis y = -5 : 0.2 :5; % mesh on y axis [xx,yy] = meshgrid(x,y); % rectangular mesh on xy plane z = xx.^2 + yy.^3; % z-coordinate at mesh nodes mesh(xx,yy,z) % mesh graph colorbar % adds bar of colors mesh(xx,yy,z,'EdgeColor','black') meshc(xx,yy,z) % mesh graph with isolines (contours) in xy plane surf(xx,yy,z) % surface graph surfc(xx,yy,z) % surface graph with isolines contour(z); % contours contour(xx,yy,z); contour(xx,yy,z,'ShowText','on'); contourf(z); contourf(z,20); [C, h] = contour(xx,yy,z); clabel(C,h) set(h,'ShowText','on','TextStep',get(h,'LevelStep')*2) ============ graph on an irregular mesh ================ n = 50; % number of points (nodes) x = rand(1,n); % coordinates of nodes (here random numbers) y = rand(1,n); z = y + sin(3*x); % function values at nodes tri = delaunay(x,y); % triangulation trisurf(tri, x, y, z) % surface graph trimesh(tri, x, y, z) % mesh graph =========================== ======= other plots ======= =========================== x = 2:6; y = x.^2; bar(y) % vertical bars bar(x,y) bar(x,y, 0.7) % vertical bars, width 0.7 (default 0.8) barh(x,y,'g') % horizontal bars, green z = [x' y' (y-x)']; bar(x,z) % triplets bar(x,z,'stacked') errorbar(x,y, ones(size(y))) % errors as vert. bars counts=[2 6 4 9 1]; pie(counts) pie3(counts) expl = [1 1 0 0 0]; pie(counts, expl) =============================== === matrix representation ===== =============================== A = round(10*rand(12)); B = A==3; spy(B) % nonzero elements marked by * A = magic(10); % "magic square" imagesc(A); % values repesented by colors colormap(gray); imagesc(A); % by grey hues, from black to white imagesc(-A); % ... , from white to black colormap('default'); % returns colors back ========================== ======= print plot ======= ========================== print -djpg pict1.jpg % format jpg print -deps pict1.eps % format eps ==========================