In today’s lesson, we will be covering the following topics:
1. Finding Derivatives
2. Finding Anti-Derivatives (Integrals)
VIDEO COMING SOON
Finding Derivatives
If you’re in this course, you probably have already taken a calculus class – if not, check out the Differential and Integral Calculus courses for information on derivatives and integrals! I won’t explain the background of these mathematical concepts here because they have entire courses linked to them. Nobody really likes to do these by hand though! So that’s why we’re going to learn how to get MATLAB to do this work for us. Usually, we need to define a variable before we use it – if we just type in ‘x’ or ‘y’, MATLAB won’t know what that variable is equal to. But when we take the derivative (or integral) we are dealing with variables that don’t have numbers assigned to them – just like x and y.
So to find the derivative (aka to differentiate a function), we need to use the syntax syms variable first, where the word variable is going to be replaced with the variables that aren’t defined (the ones in your equation). Take a look at the screenshot below. In this case, the variable I haven’t defined within MATLAB is going to be x (because I didn’t explicity write x=5 or another number beforehand), so I will replace the word variable with x, making it syms x.
On the second line of code in the picture below, I typed the function I was differentiating – mine was y = 2 * x. By the way, you don’t have to use y in the syms line because you’re defining that y is equal to 2 * x.
In the third line, take the derivative by using diff(function), where you will replace the word ‘function‘ with the variable of the function you are differentiating. For me, this was y, so I have diff(y). These three lines together enable you to take the derivative of a function!
I have done an example below, highlighting my code in yellow and circling the final answer in red, where MATLAB has given the answer for the derivative:
What if you need to take the derivative of a function that has multiple variables, but you only need to differentiate with respect to one of them? This is called a partial derivative, and MATLAB allows you to do that almost in the same way we did above. The only differences are that any additional variables need to be defined with syms, and rather than syntax like diff(y) if y is the function you’re differentiting, you need to use diff(y,v), where v is the variable you’re differentiating within the function y.
Take a look at this example. The function I’m differentiating is y = 2*x*z, but only with respect to x. This is why I have used diff(y,x) rather than diff(y). Putting the x in there specifies that it’s with respect to x. If it was with respect to z, I would’ve used diff(y,z). In the first line, I also put syms x z rather than syms x as before. This is because z is also included in the function y, and it has to be defined using syms because we didn’t otherwise assign any numerical value to z, such as z=5.
Finding Anti-Derivatives (Integrals)
With integrals, we could take the definite or the indefinite integral. Again, I’m not going to go into the math of it here (you would likely have already taken an integral calculus class, or can check out that course page), but let’s take a look at the syntax:
Indefinite Integrals
Finding the indefinite integral means that we do not have set bounds that we are integrating within – that’s where you might see a constant (usually +c) added to the end of an integral in order to show this. MATLAB doesn’t add that to the end of its output though, but it still otherwise calculates the indefinite integral.
To find the indefinite integral of a function, use the same three lines of code as we did for derivatives above (in the very first screenshot), but change the diff to an int. I’ve done an example of this below, where I integrate 3*x + (2*x^2). The code is highlighted in yellow and the output is circled in red:
If there is more than one variable in the equation, and you only want to integrate with respect to a specific variable, then use the syntax int(f,v). f is the function you are integrating, v is the variable being integrated within the function. Use the same three lines of code as the method above, but now you have that extra variable v in the brackets with int. You also need to put all the variables that are not defined (aka, the variables on the right side of the equals sign in f = x*z below). f is defined because MATLAB knows it equals x*z, but x or z are not defined so we type syms x z to let MATLAB know it will treat x and z as variables.
Definite Integrals
Finding the definite integral means that we have a set of bounds a to b (an interval) that we are finding the integral over.
There are two ways to find the definite integral.
The first way is to use integral(function,a,b). You will replace the word function with an anonymous function (click here if you missed the anonymous functions lesson) that needs to be integrated (make sure to use the element by element dot operator here before multiplication, division and exponentiation), and replace a and b with the values you are integrating between. a is the minimum value and b is the maximum.
The second way is to use the same three lines of code as we did for differentiating in the first example above, but we need to change the diff to an int and add the bounds. In the example above, I integrated y = 3*x from 1 to 5. Here, I’ve done the same thing. The first line needs to be syms x to let MATLAB know that x will remain as a variable – you can change the x to whatever variable you’re using. The next line has y = 3*x, which is the function we’re integrating. The last line has int(y,1,5) meaning that we’re taking the integral of function y from 1 to 5. This also gives 36, which shows that both methods of solving the definite integral do give the same answer.
And yes, if you’re wondering – you can lower it down to two lines of code if you substitute Line 2 into Line 3. This means instead of specifying y = 3*x on a separate line, you’ll just type it directly into the last line with the int. You’d get the same answer, and it would look like this:
Lastly, if you have more than one variable in an equation and need to integrate with respect to just one variable, use the syntax int(function,variable,a,b). Replace function with the numerical function you’d like to integrate, replace variable with the variable being integrated, and replace a and b with the bounds you’re integrating between.
Here’s an example where I have y = x*z, but I only want to integrate the equation with respect to x, between the values 1 and 2. Since the syntax is int(function,variable,a,b), we will replace these values with what we have, and it becomes int(x*z, x, 1, 2). Check out the example below: