hiddenGem

Animations vs. Movie

Jun 29th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 2.50 KB | None | 0 0
  1. %% Animations vs. Movies
  2. %% Animation (1 of 2)
  3. x = 0:0.01:10;                  % x-values
  4. n = 0:0.25:25;                  % exponents
  5. y = x.^n(1);                    % first plot: y = x^0
  6. h = plot(x,y);                  % plot first command
  7. title('n = 0')                  % create a title
  8. grid on                         % grid lines on
  9. z = 1;                          % counter for elements of n
  10.  
  11. for k = n(2:end)                % loop until out of data
  12.     z = z + 1;                  % exponent counter
  13.     y = x.^k;                   % new value of y = x^n
  14.     set(h, 'XData', ...         % overwrite the values of
  15.         x, 'YData', y)          %   x and y on the graph
  16.     drawnow                     % draw the new data points
  17.     str = sprintf('N = %s', ... % store variable containing
  18.           num2str(n(z)));       %  the string for the title
  19.     title(str)                  % title
  20.     pause(0.1)                  % pause for view-ability
  21. end % for
  22. %% Animation (2 of 2)
  23.  
  24. clc, clear
  25.  
  26. h = animatedline;               % create our handle
  27. axis([-10,10,-1000,1000]);      % constrain the axes
  28.  
  29. x = linspace(-10,10,500);       % x data points
  30. y = x.^3;                       % g(x) data points
  31. for k = 1:length(x)             % loop to add data
  32.     addpoints(h,x(k), y(k));    % add data points to the animation
  33.     drawnow                     % update the animation
  34. end % end loop
  35. %% Movies
  36.  
  37. x = 0:0.01:10;                  % x-values
  38. n = 0:0.25:25;                  % exponents
  39. y = x.^n(1)                     % firest plot: y = x^0
  40. h = plot(x,y);                  % plot first exponent
  41. title('n = 0')                  % create a title
  42. grid on                         % add grid lines
  43. z = 1;                          % counter for elements of n
  44.  
  45. my_movie(1) = getframe;         % store 1st frame of movie
  46. for k = n(2:end)                % loop until out of data
  47.     z = z + 1;                  % exponent counter
  48.     y = x.^k;                   % new value of y = x^n
  49.     set(h, 'XData', ...         % Overwrite the values of
  50.         x, 'YData', y)          %  x and y on the graph
  51.     str = sprintf('N = %s', ... % store variable containing
  52.           num2str(n(z)));        %  the string for the title
  53.     title(str)                  % title
  54.     pause(0.1)                 % pause for veiw-ability
  55.     my_movie(z) = getframe;     % store next frame of movie
  56. end % for loop
  57. pause;                          % needs user to press button
  58. movie(my_movie)                 % play back the movie
Add Comment
Please, Sign In to add comment