0

I am new to Matlab and I am looking for your advice on what is a suitable or best way(s) to initialize a function ( = vs =@ or other options) to be able to differentiate it, find a value at a specific argument and plot it as well as tangent lines at specific points.

Things I discovered:

f = my function(x) % but needs x initalized; returns values
diff(f, x)
plot(x, f)

f = @(x) my function
diff(f, x) % doesn't work
fplot(f, x)

And symbolically

syms x
f = my function

but I can't get values, differentiate or plot f yet in the last 2 cases.

user10853
  • 302
  • 1
  • 4
  • 17
  • Do you want to plot the derivative, or just the normal curve? Please correct me if I'm wrong but with what I gathered, I assume you want to plot the normal curve, determine what the derivative is at a certain point, then plot the tangent line at this point... am I correct? – rayryeng Sep 30 '15 at 20:34
  • Yes you are right. I already have the function describing the tangent at any point so I can use this. But I would also like to know how to get it in Matlab. – user10853 Sep 30 '15 at 20:37
  • Wicked. Give me one moment. I'm writing up an answer. – rayryeng Sep 30 '15 at 20:38

1 Answers1

1

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:

  1. Define a symbolic variable x, t, whatever
  2. Define the original function
  3. Compute the derivative via diff
  4. Convert both 2 and 3 into function handles via matlabFunction
  5. Define a good domain that is representative of the function
  6. Plot the original function
  7. Choose a point you want to find the slope of
  8. Compute the equation of the tangent line and determine the appropriate domain for this line
  9. 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:

enter image description here

rayryeng
  • 102,964
  • 22
  • 184
  • 193
  • 1
    Excellent. Exactly what I want. Thank you very much for the detailed explanation. Totally appreciated. – user10853 Sep 30 '15 at 21:08
  • @user10853 - My pleasure! Good luck! – rayryeng Sep 30 '15 at 21:10
  • thanks. Two more questions: 1) is it possible to set the range of x for the tangent to be plotted for? 2) are jacobian & gradient any different than diff? – user10853 Sep 30 '15 at 21:12
  • @user10853 - (1) yes that's possible. However, the size of the plot's domain will have to be the largest of the two domains: the original plot and the tangent. That will automatically be resized when using `plot`. All you have to do is change step #8 so that `x` is in some other domain. (2) Gradient and Jacobian are **two-dimensional**. This is for 1D only. If you want to do that, then that's a completely different problem because you'd want to find the **surface** tangent to a point in 3D. This code doesn't do that. – rayryeng Sep 30 '15 at 21:19
  • I am setting another domain for the tangent but the tangent is no longer a tangent. It's a different line starting from the same point. – user10853 Oct 01 '15 at 15:07
  • 1
    Don't think you're doing it right. I'll update my post so you can physically see the difference. – rayryeng Oct 01 '15 at 15:29
  • Sure, I knew I was doing something wrong but couldn't find it. Asked a friend and he cleared it out and so did you. I was simply using the different dimensions (kept the full vector for the tangent). I solved it using indices. Thanks – user10853 Oct 02 '15 at 00:15