Last active
March 15, 2018 17:38
-
-
Save alecGraves/90f0840f2dfa9f0793cfaa587111e9f3 to your computer and use it in GitHub Desktop.
software of programming, graphing numerical derivative
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
clear | |
clc | |
hold('on'); | |
x = 0:0.01:5; | |
y = x.^2; | |
p = plot(x, y, 'LineWidth', 2, 'Color', [.2, .5, 1]); | |
axis([0, 5, 0, 25]) | |
tangent = 2*x-1; | |
t = plot(x, tangent, 'LineWidth', 2, 'Color', [0.9, .54, .3]); | |
% l1 = plot([1 ; 1 ],[0 ; 0 ], 'LineWidth', 1, 'Color', 'r'); | |
[m, yidx] = min(abs(x-1)) % grab the index where x = 1 | |
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'); | |
s = size(x); | |
s = s(end); % get the size of the array, use that to determine x values to use when creating the animation. | |
% add text to match the assignment video | |
x1 = 0.5; | |
y1 = 15; | |
txt1 = 'lim f(1+h)/h as h->0 approaches'; | |
text(x1,y1,txt1); | |
txt2 = 'the slope of the tangent line which is'; | |
text(x1,y1-1,txt2); | |
txt3 = 'equal to the derivativeat that point.'; | |
text(x1,y1-2,txt3); | |
x2 = 3.4; | |
y2 = 10; | |
text(x2, y2, 'f(x) = x^2'); | |
text(3, 3, 'tangent line: t(x) = 2x-1'); | |
text(3, 2, 'slope: df/dx(1) = 2'); | |
for i = s:-3:(yidx+1) % modify middle value to change step size/speed | |
% g'ddam it matlab, why | |
x1 = [1 x(i) 1]; | |
x2 = [ 1 x(i) x(i)]; | |
y1 = [0 0 y(yidx)]; | |
y2 = [y(yidx) y(i) y(i)]; | |
for ii = 1:numel(l2) | |
l2(ii).XData = [x1(ii) x2(ii)]; | |
l2(ii).YData = [y1(ii) y2(ii)]; | |
end | |
% set(l2,'XData',[1 x(i) 1; 1 x(i) x(i) ], 'YData',[0 0 y(yidx); y(yidx) y(i) y(i)]); | |
title(['slope = ', int2str((y2(end) - y1(end))/(x2(end) - x1(end)))]) | |
drawnow() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment