EDUCBA

EDUCBA

MENUMENU
  • Explore
    • Lifetime Membership
    • All in One Bundles
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign up
Home Data Science Data Science Tutorials Matlab Tutorial Differentiation in Matlab

Differentiation in Matlab

Priya Pedamkar
Article byPriya Pedamkar

Updated March 4, 2023

Differentiation in Matlab

Introduction to Differentiation in Matlab

Differentiation in Matlab is used to find the rate of change of a quantity w.r.t the other. For example, differentiation can be used to calculate the rate at which velocity changes with time (which is acceleration). Using differentiation, we can also find the rate at which ‘x’ changes w.r.t ‘y’. This change in ‘x’ w.r.t ‘y’, when represented using a graph, will give gradient for the curve.

ADVERTISEMENT
Popular Course in this category
MATLAB Course Bundle - 5 Courses in 1 | 3 Mock Tests

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Syntax

Below is the syntax for Differentiation in Matlab:

diff (A)
diff (A, var)
diff (A, n)

Explanation: diff (A) will calculate the differentiation of A w.r.t variable provided by symvar (A, 1). diff (A, var) can be used to calculate the differentiation of A w.r.t the desired variable, i.e. the variable passed as an argument. diff (A, n) can be used to get the ‘nth’ derivative of the function. By default, ‘diff (A)’ provides us with 1st derivative of the input function.

Examples to Implement Differentiation in Matlab

Let us now understand the code to calculate the differentiation in MATLAB using ‘diff (A) function’:

Example #1

In this example, we will use a polynomial function of degree 4 and will differentiate it w.r.t the default variable. We will follow the following 2 steps:

Step 1: Create the function of degree 4 in MATLAB

Step 2: Calculate the differentiation using ‘diff function’

Code:

syms x
A = x.^5 + 7*x.^4 - 2*x.^2
diff (A)

Output:

differentiation in Matlab1

Explanation: Initializing the variable ‘x’. Creating the polynomial function of degree 4. Passing input function ‘A’ to the diff function. Mathematically, the differentiation of x.^5 + 7*x.^4 – 2*x.^2 w.r.t ‘x’ is 5*x^4 + 28*x^3 – 4*x. As we can see in the output, we have obtained differentiation of our input function ‘A’ as 5*x^4 + 28*x^3 – 4*x using ‘diff (A) function’, which is the same as expected by us.

Example #2

In this example, we will use a polynomial function of variables ‘x’ and ‘t’ and will differentiate it w.r.t ‘t’. We will follow the following 2 steps:

Step 1: Create the function of variables ‘x’ and ‘t’

Step 2: Calculate the differentiation using ‘diff (A, var) function’

Code:

syms x t
A = sin(x*t^2) - cos(x*t^3)
diff (A, t)

Output:

differentiation in Matlab2

Explanation: Initializing the variable ‘x’ & ‘t’. Creating the polynomial function with variables ‘x’ and ‘t’. Passing input function ‘A’ to the diff function]. Please note that we have passed ‘t’ as 2nd argument. Our input ‘A’ will be differentiated w.r.t this variable ‘t’. Mathematically, the differentiation of sin (x*t^2) – cos (x*t^3) w.r.t ‘t’ is 2*t*x*cos (t^2*x) + 3*t^2*x*sin (t^3*x). As we can see in the output, we have obtained differentiation of our input function ‘A’ as 2*t*x*cos (t^2*x) + 3*t^2*x*sin (t^3*x) using ‘diff (A, var) function’, which is same as expected by us.

Example #3

In this example, we will use a polynomial function of degree 6 and will find its 3rd derivative. We will follow the following 2 steps:

Step 1: Create the function of degree 6

Step 2: Calculate the differentiation using ‘diff (A, n) function’

Code:

syms x
A = 5*x.^6 + 4*x.^5 - 2*x.^2
diff (A, 3)

Output:

polynomial function

Explanation: Initializing the variable ‘x’. Creating the polynomial function of degree 6. Passing input function ‘A’ to the diff function. Please note that we have passed ‘3’ as 2nd argument. This will give us the 3rd derivative of our input function. Mathematically, the 3rd derivative of 5 * x.^6 + 4 * x.^5 – 2 * x.^2 is 600 * x^3 + 240 * x^2. As we can see in the output, we have obtained the 3rd derivative of our input function ‘A’ as 600*x^3 + 240*x^2 using ‘diff (A, n) function’, which is the same as expected by us.

Conclusion

‘Differentiation’ can be done in MATLAB using diff function. By default, the differentiation is done w.r.t the variable identified by ‘symvar’. If we need differentiation w.r.t the variable of our choice, we can use diff (A, var). diff (A, n) can be used to get the ‘nth’ derivative in MATLAB.

Recommended Articles

This is a guide to Differentiation in Matlab. Here we discuss an introduction to Differentiation in Matlab, syntax with programming examples. You can also go through our other related articles to learn more –

  1. MATLAB Derivative
  2. MATLAB Derivative of Function
  3. Matlab Alternatives
  4. Polynomial in Matlab
ADVERTISEMENT
SPSS Course Bundle - 14 Courses in 1 | 5 Mock Tests
34+ Hours of HD Videos
14 Courses
5 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
MICROSOFT AZURE Course Bundle - 15 Courses in 1 | 12 Mock Tests
63+ Hour of HD Videos
15 Courses
12 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
HADOOP Course Bundle - 32 Courses in 1 | 4 Mock Tests
125+ Hour of HD Videos
32 Courses
4 Mock Tests & Quizzes
Verifiable Certificate of Completion
Lifetime Access
4.5
ADVERTISEMENT
INFORMATICA Course Bundle - 7 Courses in 1
47+ Hours of HD Videos
7 Courses
Verifiable Certificate of Completion
Lifetime Access
4.5
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
  • Blog as Guest
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

© 2023 - 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

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

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

*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 Login

Forgot Password?

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Cyber Monday Reloaded Price Drop! All in One Universal Bundle (3700+ Courses) @ 🎁 90% OFF - Ends in ENROLL NOW