1 / 14

Beginning Programming for Engineers

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.

reuben
Download Presentation

Beginning Programming for Engineers

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Beginning Programming for Engineers Animation

  2. 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)

  3. 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

  4. 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

  5. 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

  6. 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

  7. 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.

  8. Changing how numbers are printed help format format compact format bank magic(4)

  9. 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);

  10. 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);

  11. 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);

  12. 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');

  13. 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".

  14. 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])

More Related