How to I animate a tangent line across a sin function?

I am completely new to MATLAB so I'm trying to learn the ropes. Anything I say will most likely sound like it's coming from a child asking what 1+1 is.
Firstly, the graph should look something like this: http://i.imgur.com/09S9AH7.png?1 The tangent line should be animating as it progresses through the x-axis.
Here what my code is:
%%Clear
clear;clc;close all
%%Given
xstart = -1;
xend = 3;
n = 150;
x = linspace(xstart,xend,n);
y = x.*sin(x.^2)+1;
% Plot Animated
for c = 1:n;
dy = (x.*(sin(x(c))+(2.*cos(x(c)))))+1-(2.*(cos(x(c)))); % 1st derivative of y
y2 = (dy.*x(c))+y(1); % Equation of tangent line
plot(x(c:c+10),y2(c:c+10)) % Animating tangent line
hold on
plot(x,y); % sine graph
plot(x(c),y(c),'o') % Animating ball
hold off
axis([-2 4 -2 6])
pause(0.01)
drawnow;
end
I'm not completely sure how to have the tangent line follow along the sin graph line, I have something similar but it is way off.
If you're going to help me can you * explain* how you got the script to work? I don't just want an answer I want to learn how to use MATLAB.

Answers (0)

Categories

Products

Asked:

on 15 Nov 2015

Edited:

on 15 Nov 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!