140 likes | 230 Views
Beginning Programming for Engineers. Animation. Special matrix functions. The ones function creates a matrix with all elements set to 1. The zeros function creates a matrix with all elements set to 0. The eye function creates an identity matrix.
E N D
Beginning Programming for Engineers Animation
Special matrix functions • The ones function creates a matrix with all elements set to 1. • The zeros function creates a matrix with all elements set to 0. • The eye function creates an identity matrix. • The magic function creates a magic square. • >> clear>> ones(2,3) • >> zeros(3,5)>> ones(2) • >> zeros(3) • >> eye(4)>> magic(4)
Using end as a subscript • Using a subscript of end means choose the last row of the column, or last column of the row... • This use of end can be on the receiving side of an assignment, too. Try these commands: • >> clear • >> B = (1:100).^2;>> B(end) • >> A = [1 2 3; 4 5 6];>> A(end,2) • >> A(1,end) • >> A(end,end) • >> A(end) • >> A(1,end) = 99; • >> A
Slicing matrices: Vectors of subscripts • We can choose several elements of a row or column by giving a vector as a subscript. • We can use vectors for both rows and columns to collect a matrix. • We can sensibly use end in these vector subscripts. Try these commands: • >> M = magic(7) • >> M([1 3 6], 4)>> M([1 3 6], [2 5]) • >> M([1 end],[2 end]) • >> M([1 end],[2 end]) = 0;>> M
Slicing matrices: Subscript ranges • We can use ranges as subscripts. • The end keyword can be sensibly used. • Using plain : as a range selects the entire row or column. >> M = magic(5) >> M(2,1:2:end)>> M(:,2) >> M(2,1:2:end) = ... [99 88 77]>> M(4,3:end) = 0
Saving variables to a file save 'filename' • Saves all variables in the workspace to filename.mat save 'filename' x y z • Saves the variable x, y, and z to filename.mat whos • Lists all the variables in the current workspace. whos -file 'filename' • Lists all the variables save in filename.mat
Loading variables from a file load 'filename' • Load all the variables stored in filename.mat into the workspace. Variables not stored in filename.mat are unchanged. load 'filename' x y z • Load the variables x, y, and z from filename.mat into the workspace.
Changing how numbers are printed help format format compact format bank magic(4)
Graphics handles on subplots • Figures and subplots can return a "handle" to allow objects to be added later. • clear; clc%% Set up subplots • figure('Color', 'white');s1 = subplot(1,2,1); hold on;s2 = subplot(1,2,2); hold on;%% First objects in subplots.plot(s1, cosd(0:360), sind(0:360));plot(s2, 0:5, (0:5).^2);%% Second objects in subplotsplot(s1, 0.5*cosd(0:360), 0.5*sind(0:360));plot(s2, 0:5, (0:5).^3);
Graphics handles on figures • Use gca to get the "axes" object handle. • clear; clc%% Set up figuresfigure; hold on; f1 = gca;figure; hold on; f2 = gca;%% First objects in figures.plot(f1, cosd(0:360), sind(0:360));plot(f2, 0:5, (0:5).^2);%% Second objects in figuresplot(f1, 0.5*cosd(0:360), 0.5*sind(0:360));plot(f2, 0:5, (0:5).^3);
Graphics handles on plot objects • The plot function can return a handle that may be used to see and modify properties of the line. ph = plot([1 5] ,[1 5], 'r', 'LineWidth', 3); • The get function can get some or all properties. get(ph) get(ph, 'LineWidth') • The set function changes properties set(ph, 'LineWidth', 5);
The XData and YData properties • In a plot object, the XData and YData properties are the coordinates of the plotted data itself. ph = plot([0 3 0 3], [0 3 3 0]); get(ph, 'XData') • Changing the values of XData, YData, and other properties changes the drawn object! set(ph, 'XData', [0 3 0 4], ... 'YData', [0 3 3 1.5]); set(ph, 'Marker', 'o');
pause and drawnow • Since drawing takes time, Matlab batches up graphics commands. Result: You only see the final result! • To get animation, we need to tell Matlab to draw what it has before continuing. • drawnow • Alternatively, tell Matlab to draw, and pause. This is useful if the animation is running too fast. • pause(0.15) % Draw, and pause 0.15 seconds • Use movie and getframe to build "movies".
Polygon operations • The fill function is like plot except it creates a filled polygon. • fill([1 2 3], [0 3 2], 'r'); • The polyarea function computes the area of a polygon. • polyarea([1 2 3], [0 3 2]) • The inpolygon function tests if a point is in a polygon or not. • inpolygon(3,3, [1 2 3], [0 3 2])