% IAP2007 Introduction to MATLAB: Graphics and Visualization % Instructor: Violeta Ivanova, violeta@mit.edu % EXAMPLE 3 Animation %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % A. COMPUTE CURVE THROUGH POINTS % Compute cubic curve through points with (X,Y) coordinates (2, 75), (3, 86), % (5, 82), (6, 70), and (8, 40), along interval x from 0 to 10 with step 0.1. % Hint: create vectors X and Y, then use functions POLYFIT and POLYVAL. clear all X = [2 3 5 6 8]' Y = [75 86 82 70 40]' C = polyfit(X, Y, 3) x = [0 : 0.1 : 10]; y = polyval(C, x); %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% % B. CREATE ANIMATION % B1. Create a figure window figure set(gcf, 'Name', 'Cubic Fit Animation') % B2. Plot the five points. plot (X, Y, 'ro', 'MarkerFaceColor', 'r') hold on % B3. Add an animated plot of the cucic drawn point after point (use different color). % Hint: Use functions PLOT, SET, HOLD ON and DRAWNOW inside a FOR loop. % Set x-axis limits from 0 to 10, and y-axis limit from 0 to 100. for i=1:length(x) p1 = plot(x(i), y(i), 'bo', 'MarkerSize', 4); hold on % Set the axes to be the same during plotting set(gca, 'XLim', [0 10], 'YLim', [0 100]); drawnow % Save the frame in a matrix M to play later M(i) = getframe(gcf); end hold off % movie2avi(M, 'cubic.avi', 'fps', 30, 'quality', 100);