How do I differentiate in MATLAB?

  • Context: MATLAB 
  • Thread starter Thread starter Urmi Roy
  • Start date Start date
  • Tags Tags
    Differentiate Matlab
Click For Summary

Discussion Overview

The discussion revolves around differentiating functions in MATLAB, particularly in the absence of the symbolic toolbox. Participants explore numerical differentiation methods and the use of built-in functions.

Discussion Character

  • Technical explanation, Homework-related

Main Points Raised

  • One participant notes the lack of a symbolic toolbox and inquires about numerical differentiation methods available in MATLAB.
  • Another participant points out that the 'diff' command is overloaded and provides a link to the MATLAB documentation.
  • A different participant explains how to use the 'diff' function with an appropriate step size to differentiate functions numerically, providing an example with the sine function.
  • This participant also mentions the 'gradient' function as an alternative for obtaining derivatives, highlighting its ability to return a vector of the same size as the input.
  • A later reply expresses gratitude for the information shared, indicating engagement with the discussion.

Areas of Agreement / Disagreement

Participants generally agree on the use of the 'diff' and 'gradient' functions for numerical differentiation, but there is no consensus on the best approach, as the initial inquiry remains open-ended.

Contextual Notes

Limitations include the assumption that participants are familiar with MATLAB's syntax and functions, as well as the potential for varying interpretations of numerical differentiation methods.

Urmi Roy
Messages
743
Reaction score
1
I find that I don't have the symbolic toolbox. Due to this, I'm not being able to differentiate by using the usual 'diff' command. However, just as one can integrate numerically, by using the quad and quadl functions, is there no way to differentiate numerically on MATLAB (like using the ab initio method or something)?
 
Physics news on Phys.org
You can use diff to differentiate most functions with an appropriate step size. For example,

Code:
h = 0.001;
x = -pi:h:pi;
y = sin(x);
z = diff(y)/h;

If you plot z, you will see that it is equal to cos(x). Similarly, you can use diff to calculate derivatives this way for other functions.

Just remember diff returns a vector with 1 less element.

Alternatively, you could use gradient, which returns a vector of the same size. For 1D problems this is a simple partial derivative. There are additional syntaxes available for higher dimensional problems (see http://www.mathworks.com/help/matlab/ref/gradient.html)
 
Thanks for the info, Kreil! I can't imagine why I didn't see your post in all these days!
 

Similar threads

  • · Replies 10 ·
Replies
10
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K
  • · Replies 3 ·
Replies
3
Views
4K
  • · Replies 5 ·
Replies
5
Views
4K
  • · Replies 8 ·
Replies
8
Views
3K
  • · Replies 4 ·
Replies
4
Views
7K
  • · Replies 3 ·
Replies
3
Views
5K
  • · Replies 4 ·
Replies
4
Views
2K
Replies
3
Views
3K