Guest User

Untitled

a guest
Mar 22nd, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. clear
  2. clc
  3.  
  4. hold('on');
  5. x = 0:0.01:5;
  6. y = x.^2;
  7. p = plot(x, y, 'LineWidth', 2, 'Color', [.2, .5, 1]);
  8. axis([0, 5, 0, 25])
  9.  
  10. tangent = 2*x-1;
  11. t = plot(x, tangent, 'LineWidth', 2, 'Color', [0.9, .54, .3]);
  12.  
  13. % l1 = plot([1 ; 1 ],[0 ; 0 ], 'LineWidth', 1, 'Color', 'r');
  14. [m, yidx] = min(abs(x-1)) % grab the index where x = 1
  15. l2 = plot([1 x(end) 1; 1 x(end) x(end) ], [0 0 y(yidx); y(yidx) y(end) y(end)], 'LineWidth', 1, 'Color', 'r');
  16. s = size(x);
  17. s = s(end); % get the size of the array, use that to determine x values to use when creating the animation.
  18.  
  19. % add text to match the assignment video
  20. x1 = 0.5;
  21. y1 = 15;
  22. txt1 = 'lim f(1+h)/h as h->0 approaches';
  23. text(x1,y1,txt1);
  24. txt2 = 'the slope of the tangent line which is';
  25. text(x1,y1-1,txt2);
  26. txt3 = 'equal to the derivativeat that point.';
  27. text(x1,y1-2,txt3);
  28.  
  29. x2 = 3.4;
  30. y2 = 10;
  31. text(x2, y2, 'f(x) = x^2');
  32. text(3, 3, 'tangent line: t(x) = 2x-1');
  33. text(3, 2, 'slope: df/dx(1) = 2');
  34.  
  35. for i = s:-3:(yidx+1) % modify middle value to change step size/speed
  36.  
  37. % g'ddam it matlab, why
  38. x1 = [1 x(i) 1];
  39. x2 = [ 1 x(i) x(i)];
  40. y1 = [0 0 y(yidx)];
  41. y2 = [y(yidx) y(i) y(i)];
  42. for ii = 1:numel(l2)
  43. l2(ii).XData = [x1(ii) x2(ii)];
  44. l2(ii).YData = [y1(ii) y2(ii)];
  45. end
  46. % set(l2,'XData',[1 x(i) 1; 1 x(i) x(i) ], 'YData',[0 0 y(yidx); y(yidx) y(i) y(i)]);
  47.  
  48. title(['slope = ', int2str((y2(end) - y1(end))/(x2(end) - x1(end)))])
  49. drawnow()
  50. end
Add Comment
Please, Sign In to add comment