Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get derivative of a function in MATLAB?

Tags:

matlab

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!

like image 536
Saeid Yazdani Avatar asked Mar 20 '12 01:03

Saeid Yazdani


People also ask

How do you take the derivative of a function in MATLAB?

Df = diff( f , var , n ) computes the n th derivative of f with respect to var . Df = diff( f , var1,...,varN ) differentiates f with respect to the parameters var1,...,varN .


2 Answers

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);
like image 171
Max Avatar answered Oct 03 '22 19:10

Max


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);
like image 39
ely Avatar answered Oct 03 '22 21:10

ely