Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %% Animations vs. Movies
- %% Animation (1 of 2)
- x = 0:0.01:10; % x-values
- n = 0:0.25:25; % exponents
- y = x.^n(1); % first plot: y = x^0
- h = plot(x,y); % plot first command
- title('n = 0') % create a title
- grid on % grid lines on
- z = 1; % counter for elements of n
- for k = n(2:end) % loop until out of data
- z = z + 1; % exponent counter
- y = x.^k; % new value of y = x^n
- set(h, 'XData', ... % overwrite the values of
- x, 'YData', y) % x and y on the graph
- drawnow % draw the new data points
- str = sprintf('N = %s', ... % store variable containing
- num2str(n(z))); % the string for the title
- title(str) % title
- pause(0.1) % pause for view-ability
- end % for
- %% Animation (2 of 2)
- clc, clear
- h = animatedline; % create our handle
- axis([-10,10,-1000,1000]); % constrain the axes
- x = linspace(-10,10,500); % x data points
- y = x.^3; % g(x) data points
- for k = 1:length(x) % loop to add data
- addpoints(h,x(k), y(k)); % add data points to the animation
- drawnow % update the animation
- end % end loop
- %% Movies
- x = 0:0.01:10; % x-values
- n = 0:0.25:25; % exponents
- y = x.^n(1) % firest plot: y = x^0
- h = plot(x,y); % plot first exponent
- title('n = 0') % create a title
- grid on % add grid lines
- z = 1; % counter for elements of n
- my_movie(1) = getframe; % store 1st frame of movie
- for k = n(2:end) % loop until out of data
- z = z + 1; % exponent counter
- y = x.^k; % new value of y = x^n
- set(h, 'XData', ... % Overwrite the values of
- x, 'YData', y) % x and y on the graph
- str = sprintf('N = %s', ... % store variable containing
- num2str(n(z))); % the string for the title
- title(str) % title
- pause(0.1) % pause for veiw-ability
- my_movie(z) = getframe; % store next frame of movie
- end % for loop
- pause; % needs user to press button
- movie(my_movie) % play back the movie
Add Comment
Please, Sign In to add comment