EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • All Courses
    • All Specializations
  • Blog
  • Enterprise
  • Free Courses
  • All Courses
  • All Specializations
  • Log in
  • Sign Up
Home Data Science Data Science Tutorials Matlab Tutorial MATLAB Derivative
 

MATLAB Derivative

Priya Pedamkar
Article byPriya Pedamkar

Updated February 27, 2023

MATLAB Derivative

 

 

Introduction to MATLAB Derivative

MATLAB is a programming environment that is interactive and is used in scientific computing. It is extensively used in a lot of technical fields where problem-solving, data analysis, algorithm development, and experimentation is required. The discipline-specific software is extensively written using MATLAB. MATLAB provides us the ability to perform numerous mathematical operations, In this topic, we are going to learn about MATLAB Derivative.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

How does MATLAB Derivative work?

Differentiation is a calculus tool that calculates small changes in a function. Differentiation or derivative is the rate of change of a function w.r.t. some variable. It calculates how sensitive our output is concerning any change in our input.

Mathematically, it can be represented as f(x)/dx, where dx represents the change in the value of function f (x), concerning the variable x.

Let us now understand how derivative works in MATLAB.

Syntax of derivative:

  • diff (f)
  • diff (f, n)
  • diff (f, var)
  • diff (f, var, n)

Examples of MATLAB Derivative

Here are the following examples mention below

Example #1 – diff (f)

This function will differentiate ‘f’ with the variable x

Here is an example where we compute the differentiation of a function using diff (f):

Lets us take a polynomial function defined as:

x ^ 4 + 3 x ^ 3 + 4 x ^ 2 + 5 x + 1

Syntax

df = diff (f, x)

The function diff will return derivative value of the functionx ^ 4 + 3 x ^ 3 + 4 x ^ 2 + 5 x + 1:

4x^3 + 9x^2 + 8x + 5

Input:

syms f(x)
f(x) = x ^ 4 + 3*x ^ 3 + 4*x ^ 2 + 5*x + 1
df = diff (f, x)

Output:

matlab derivative output 1

Example #2 – diff (f, n)

This function is used to calculate the nth derivative of the input function. ‘n’ here is passed as an argument.

Here is an example where we compute ‘nth’ derivative of a function using diff (f, n):

Lets us take another polynomial function defined as:

3 x ^ 3 + 2 x ^ 2 + 7 x + 5

For our example, we will calculate the 2nd derivative.

Mathematically, our output should be:

18x + 4

Input:

syms f(x)
f(x) = 3*x^3 + 2*x^2 + 7*x + 5
df = diff (f, 2)

Output:

matlab derivative output 2

As we can see, diff (f, n) has calculated the 2nd derivative of our input function.

Example #3 – diff (f, var)

This function will find the derivative of ‘f’ w.r.t the variable in the argument.

Here is an example where we compute differentiation of a function using diff (f, var):

Lets us take a sine function defined as:

Sin(x*t^2)

For our example, we will calculate the derivative w.r.t ‘t’

Mathematically, our output should be:

2txcos(t^2x)

Input:

syms x t
diff(sin(x*t^2),t)

Output:

output 3

As we can see, the derivative is calculated w.r.t.  ‘t’ as expected by us.

[Notice that we have passed ‘t’ as an argument. Diff function will now take the derivative w.r.t ‘t’]

Example #4 – diff (f, var, n)

This function will calculate the ‘nth’ derivative of the input function w.r.t the variable we pass as an argument.

Below is the example where we calculate the derivative of a function using diff (f, var, n):

Lets us take a sine function defined as:

Sin(x*t^4)

For our example, we will calculate the ‘2nd’ derivative w.r.t ‘t’

Mathematically, our output should be:

12t^2xcos(t^4x) - 16t^6x^2sin(t^4x)

Input:

syms x t
diff(sin(x*t^4),t,2)

Output:

output 4

As we can see, we have got the 2nd derivative of our input function w.r.t ‘t’, as expected.

[Notice the arguments ‘t’ and ‘2’, for derivative w.r.t and nth derivative respectively]

Conclusion

‘diff’ function is used in MATLAB to calculate the differentiation or derivative of a function. We can calculate the derivative w.r.t the default variable or the variable we pass as an argument. Also, the degree of differentiation or derivative can be controlled using the argument.

Recommended Articles

This is a guide to MATLAB Derivative. Here we discuss how does MATLAB Derivative work along with appropriate syntax and respective examples. You may also look at the following articles to learn more –

  1. Matlab Plot Circle
  2. Matlab stem()
  3. Linear Fit Matlab
  4. Covariance in Matlab
Primary Sidebar
Footer
Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

🚀 Limited Time Offer! - 🎁 ENROLL NOW