I'm new to Matlab, and I am making a code to animate the tangent line of sine as it moves along the graph. I am not sure why the length of the line keeps changing as it moves along the curve or how to fix the error I keep getting an error message in line 34 (the one that reads "an.XData = x2;") that states "Invalid or deleted object." Thanks in advance!
Here is my code:
n = 1000;x = linspace(0,2*pi,n+1);r = false;hold ony = sin(x);plot(x,y)hold offxlim([0 2*pi])ylim([-1.1 1.1])x0 = 0;y0 = sin(x0); len = 0.7;m = cos(x0);theta = atan(m);x2 = [(x0 + len*cos(theta)), (x0 - len*cos(theta))];y2 = [(y0 + len*sin(theta)), (y0 - len*sin(theta))]; an = line(x2,y2);c = an.Color;an.Color = '#4DBEEE';i = 1;while (i <= n) x0 = (i/n)*2*pi; y0 = sin(x0); m = cos(x0); theta = atan(m); x2 = [(x0 + len*cos(theta)), (x0 - len*cos(theta))]; y2 = [(y0 + len*sin(theta)), (y0 - len*sin(theta))]; an.XData = x2; an.YData = y2; hold on drawnow if ~r i = i + 1; if i == n r = true; end else i = i - 1; if i == 0 r = false; end end endhold off
Best Answer