5

In real life it is piece of cake, but how you get derivative of a quadratic or cubic function in matlab?

For example, A*x^3 + B*x^2 + C*x + D will be 3*Ax^2 + 2*b*x + C

I want to get this in matlab, but I can't figure out how :(

for example I tried this code but I get stupid result (maybe I am the one who should be blamed!):

>> x = [6 3 2 1]

x =

     6     3     2     1

>> xPrime = diff(x)

xPrime =

    -3    -1    -1

Normally it should give [18 6 2] ?? Also I Want to know how yo do this for range of numbers. For example I want derivative of each point for the example above for n = linspace(0,10,1000)

update of course I can do this manually, but I really like to know how to do it with matlab itself.

What I am doing now is getting the tangent line of the above example and I am doing like this and it works:

x = linspace(0,10,1000);
y=A*x.^3+B*x.^2+C*x + D;
plot(x,y);
hold on;
slop=3*A.*(Location^2)+2*B.*Location+C;
b=(A.*Location.^3)+(B.*Location.^2)+(C.*Location)+D;
y2=slop*(x-Location)+b;
plot(x,y2,'--r');
legend('Graph of the function','Tangent Line');
hold off;

What I mean is what do I should use instead of hand calculated derivative in this line:

slop=3*A.*(Location^2)+2*B.*Location+C;

Thanks!

2
  • 1
    Did you try polyder(p) haven't used matlab in a while don't remember exactly but I think this works.
    – Jeff
    Mar 20, 2012 at 1:20
  • Have you taken a look at the documentation for diff()? The docs say: "Y = diff(X) calculates differences between adjacent elements of X." That doesn't seem to be what you want it to do. Mar 20, 2012 at 1:29

4 Answers 4

3

To obtain the derivative of a polynomial, which is itself a polynomial, use Matlab's polyder() function. This takes the standard representation of the polynomial coefficients as a vector, and returns its derivative as a second coefiicient vector. You can evaluate the derivative of a polynomial p at some value x like this:

slop = polyval(polyder(p), x);
4
  • Thanks, but in my case I have x as a range like 0:1000:10 and the point I want the tangent line slope is named location. I tried to use the line you provided but it does not care of that. any idea how can I use it in my code?!
    – Dumbo
    Mar 20, 2012 at 11:29
  • Do you mean you want to plot a straight line, being the tangent to the cubic at the point location, over the range x? If so, then you want slop = polyval(polyder(p), location); tangent = [slop polyval(p3, location) - location*slop]; plot(x, polval(tangent, x);
    – Max
    Mar 20, 2012 at 16:02
  • Yeah exactly... but what is taht p in polyder(p) ? and also p3?
    – Dumbo
    Mar 20, 2012 at 18:29
  • Oops - my bad! I should have used p in both places: the vector of coefficients of the cubic polynomial that you want the tangent line to, in decreasing order (the normal Matlab convention)
    – Max
    Mar 21, 2012 at 9:13
3

You want to check out Matlab's symbolic library (based on the Maple engine). The basic idea is that you'll want to create symbolic variables ('syms'), and then differentiate those expressions symbolically. Then you can convert between your symbolic expression and a function handle that will evaluate your symbolic expression at some coordinate values. See here for instructions on the syntax, the 'syms' library, etc.

In real applications, though, you normally need to write your own software-function for the different mathematical-functions that you deal with. Then, only in special cases will you be able to analytically compute derivatives, and in those cases you'll want to write another, separate software-function for the mathematical-function that is the derivative. Symbolic libraries are usually very slow and they (at least currently) are an inefficient way to generate actual functions through handles.

If all you'll ever work with are polynomials, however, this is a special enough case that you should be able to write a general Matlab function that takes in a coefficient list and a range of values as input, and outputs the derivative coefficient list plus the derivative function evaluated at those values. Here's an example:

 function [d_coeffs, d_vals] = compute_poly_derivative(in_coeffs, in_values)
 num_terms = length(in_coeffs)-1;
 max_power = num_terms;

 for ii=1:num_terms
     d_coeffs[ii] = in_coeffs[ii]*max_power;
     max_power = max_power - 1;
 end

 d_vals = polyval(d_coeffs,in_values);
2

You can do this numerically using finite differences. MATLAB has a function gradient that incorporates a 2nd order accurate scheme - see here.

If you were looking for higher accuracy you could potential implement your own higher order scheme.

Hope this helps.

1

diff gives the difference between successive elements in the list: 3 - 6 is -3, 2 - 3 is -1, etc.

You can use the symbolic toolbox if you have it. Or if you only need polynomials, it's not that hard to write it yourself:

ds = poly .* fliplr(0:length(poly)-1);
ds = [0 ds(1:end-1)];

This just multiplies each coefficient by its exponent (fliplr reverses a list), and then shifts the exponents down by one (moving the list elements one to the right).

This gives you a new representation of a polynomial. To evaluate one of these at a given point x, try

sum(poly .* x .^ fliplr(0:length(poly)-1))
2
  • Maybe I was not clear enough, would you check the updated question please?
    – Dumbo
    Mar 20, 2012 at 1:26
  • Thanks, I tried to make a connection between the code you wrote and the code I did, but I am a bit confused to where to replace your code into mine?
    – Dumbo
    Mar 20, 2012 at 2:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.