Given your particular case, you want to provide a function and find the analytical derivative of it. Once you find this, you wish to determine what the derivative at this point is, then plot the tangent line coincident to the original function.
Best way to do that is to certainly use syms
to define a symbolic variable, then differentiate with diff
as you did. Now, what's left is that we want to use this to substitute in real values. You can use the very well crafted matlabFunction
function to do that for you. This will take in a symbolic mathematically defined equation and return a function handle that can accept numerical inputs. I'd like to add that all operators returned from this handle are element-wise.
Once you have the analytical derivative, you can very easily find the slope of the tangent line by substituting the point into the newly created function from matlabFunction
and then you'd have to use matlabFunction
again to convert your original function into a function handle so that you can find a point on this tangent line. You'd then have point and slope and you can easily find the equation of the tangent line via:
y -y0 = m*(x - x0)
x0
is the desired point you want, y0
is the output seen with the original function and m
is the slope found using the derivative.
The steps I would perform to achieve what you want are the following:
- Define a symbolic variable
x
, t
, whatever
- Define the original function
- Compute the derivative via
diff
- Convert both 2 and 3 into function handles via
matlabFunction
- Define a good domain that is representative of the function
- Plot the original function
- Choose a point you want to find the slope of
- Compute the equation of the tangent line and determine the appropriate domain for this line
- Plot this line and emphasize the point of interest
Here's what the code would look like at each step... let's use y = x^3
and let's analyze the slope at x = 2
as an example:
%// Step #1
syms t;
%// Step #2
y = t^3;
%// Step #3
dy = diff(y, t);
%// Step #4
yh = matlabFunction(y);
dyh = matlabFunction(dy);
%// Step #5
x = -4:0.01:4;
%// Step #6
figure;
hold on;
plot(x, yh(x));
%// Step #7
x0 = 2;
slope = dyh(x0);
%// Step #8
%// y - y0 = m*(x-x0)
%// y = m*(x - x0) + y0
tol = 1;
x2 = x0-tol:0.01:x0+tol;
y0 = yh(x0);
out = slope*(x2 - x0) + y0;
%// Step #9
plot(x0, y0, 'r.');
plot(x2, out, 'g');
Let's go through each step slowly.
Step #1
Very straight forward. I'm using t
as the variable here of interest.
Step #2
I define a function that's y = t^3
using the symbolic variable defined earlier. Again, very easy.
Step #3
We compute the analytical derivative of the function y
defined in Step #2 with respect to t
.... easy.
Step #4
We create numerical function handles for both the original function and the derivative so that we can substitute points... whether they're stored in arrays or matrices... so that we can evaluate what each value in this array is with respect to the functions they represent.
Basically, you need to use matlabFunction
so you can use these to substitute whatever numbers you want in.
Step #5
I defined the domain of -4 <= x <= 4
but this will depend on what function you're plotting. You'll have to change this depending on what the proper domain would be. I also chose a step size as 0.01
because when you plot things in MATLAB, you plot an array of points. This allows me to generate a list of values from -4 to 4 in steps of 0.01.
Step #6
I spawn a new figure, use hold on
so that when we call plot
multiple times, it will append to the new figure. We then go ahead and plot the original curve. Take notice that I'm using the function handle created by MATLAB for the original curve stored in yh
.
Step #7
I choose the point of interest I want, which is x0 = 2
, then I determine what the output value would be at this point... stored in y0
.
Step #8
Once I compute the desired point, I compute the slope seen at this point with the derivative function, then I re-arrange the equation of the tangent line to produce output values given the original domain we created, as well as the (x0,y0)
created previously. The output of this would be the output of the tangent line points. Take note that I'm centering the line to be around where the point of interest (x0,y0)
is and I define a variable called tol
that will plot the line +/- tol
centered around (x0,y0)
. Specifically, it will plot the line between x0 - tol
and x0 + tol
. I set tol = 1
here, but change it according to what is suitable for you.
Step #9
Plot the point of interest in red, and the equation of the tangent line in green. Keep in mind that the number of points of the tangent line don't match the number of points for the original domain, so make sure you use the right arrays for the right points.
Here's what I get:
