 So I hope you've watched the pencil and paper lecture on how to solve for X to get an equation and solve for one of the variables. Now, this video is going to be about how to do it with Sympy. It is just so easy to solve equations using Python. I'm in my Google Drive and I've clicked on new, more and created new Google Colab document. You see, it's already there, lecture number six. Once again, I don't want you to waste your time watching me type. So let's have a look at the document. There you go. I have called it lecture six already renamed it on the top left corner. I can go to the top right corner there and click on connect. Remember, that's going to start an instance of Python on Google server side. So nothing really happens on your own computer. You don't have to install anything. As always, my first cell is a title cell. Let's double click on that. It's a text cell and you see, I've created it with a single hashtag symbol, then a space indicating that I want the largest font size. My next section, let's click on that. Also a text cell and you can see the two hashtag symbols, then a space indicating that I want the second largest font size. That is very good for creating sections or subtitles. There is my first line of code. I've used a code comment. That's a single hashtag symbol there followed by a space. Now, remember, this is a different hashtag or pound symbol because in a code cell, that's going to tell the interpreter to ignore this line of code. So I'm just leaving a message to myself or to someone else is really my code. And I'm saying adding the required functionality from the SMPI package. The type of importing that I'm going to do is to only select specific functions and keywords from the SMPI package. I'm going to say from SMPI import and I have the init underscore printing function, the symbols function, the EQ function. That's an uppercase E, the solve function, the rational function. That's uppercase R and the pie symbol. Let's execute this line of code. Great. And the first function that I'm going to call is the init underscore printing function. Remember, that's going to allow for mathematical type setting. When I use SMPI code, the result of that code will look like my mathematics textbook. So let's execute that function. Again, do remember there's open closing parentheses because this is a function. But when we import, we don't put the open close parentheses. So our next section solving for X. So we're going to create some equations and we're going to solve for X. Our first task is create the equation 2X equals 16 using SMPI. So here's my code cell. Let's look at my code comment. Create the mathematical variable X. That's an element of the set of real numbers and assign it to the computer variable X. So do remember, I'm going to make use of the assignment operator. In most computer languages, that is not a mathematical equal symbol, but an assignment operator. It assigns what is to its right, which I've highlighted there, to what it's to its left. To its right, I'm creating a mathematical variable X. The symbol X, I'm using the symbols function. I've got two arguments separated by a comma. The first one is a string. And inside of that string, you can see the quotation marks. I put the symbol that I would like. That's X comma. Then I have a keyword argument. That's an argument that has a name, real and a value two. So I'm saying real equals true. So once I've created this mathematical symbol, which is a mathematical variable X, which can be any real number. I'm assigning that with assignment operator to this computer variable name, the name that I choose. Now it makes sense to use X. That reserves a little part of my computer memory named X. And that's where that symbol is stored. So this is the computer variable X. And this is the symbol X. Let's execute that. And unlike most other computer languages, I now have a mathematical symbol that's great to work with. Now look at my next code comment. Use the equation function. That's EQ uppercase E to create the equation and print it to the screen. And this is how we'll do it. There's EQ. It's a function. You can see the open, closed parentheses. And then I have a left-hand side as my first argument comma. And then the second argument is the right-hand side of my equation. Let's execute that. And we see the beautiful mathematical type setting, which is the result of my code 2x equals 16. Now that we've created an equation using the EQ function, let's solve for X. And this is the way that I'm going to go about it. I have the solve function. Now remember, if I go up, I've imported the solve function so I can use it as if it's part of base Python, I can just use the name itself. So there we have the solve function, open, close parentheses. You can see that. And now I have two arguments again. My first argument is the actual equation. So that's just a copy and paste from where we created the equation in the first place comma. And then the variable that I want my solution to be in. I want to solve for X. So I'm going to say comma X. Now we can all see X should be 8, because 2 times 8 is 16. Let's see what SimPy thinks. And there we go. The solution is 8. And it's going to put that solution inside of square brackets. But clearly the solution is 8. Now do remember that I can manipulate algebraic equations. So I could say 2x equals 16. I can subtract 16 from both sides, leaving me with 2x. Minus 16 equals 0. So why would I want to do that? Well, there's another way to use the solve function. Look at this. I'm using the solve function. And now I'm putting the left-hand side 2 times x minus 16 comma the variable that I want the solution to be in. So as long as you've set up your equation such that the right-hand side equals 0, as we can see there, you can use the left-hand side. There we go. Only use the left-hand side comma x. If you do not have a 0 on the right-hand side, then you will have to use this eq function so that you can have a left-hand side and a right-hand side. So let's execute this and you'll see the results going to be exactly the same. The solution is 8. So let's have a look at the following problem. Solve this equation for x. x divided by 11 equals 33. Let's print that to the screen. And remember, division, that's just a 4th slash. So I'm saying x divided by 11. That's my left-hand side. Comma 33 is my right-hand side. Those are the two arguments of the eq function. And we see the lovely representation there on the screen. Nice mathematical type setting. So now I'm going to solve for x, my new code cell. Remember, there's my code comment to myself. And I'm using the solve function and I'm using the eq function as my first argument to set up that equation, a left-hand side and right-hand side. And then comma my second argument being the variable that I want the solution to be in. I'm solving for x. And then we see the solution is 363. Let's do one more. Solve the following equation for x. One over x equals 10. Let's remind ourselves of a little bit of algebra. There's one value that x cannot take that can't be zero. We at least know that because we can't divide by zero. Let's set up that equation. I'm printing it to the screen. Now remember, you don't have to do this to solve your problem. I just want to see that everything is correct. I want to see that equation so that when I copy and paste that into my solve function, I know that I've imported the correct thing. So there we go. I'm using the solve function. There's my eq one over x comma 10. And then my second argument is x. I want to solve for x. And I see my solution is one over 10. Now let's solve the following equation for x. Four over five times x is equal to 12. Now let's set this up where we just have four divided by five. That's four, four slash five. And then the star symbol for multiplication. So four divided by five times x and then comma 12. Now if I print that to the screen, you can see four divided by five was expressed as a decimal value, zero comma eight times x equals 12. But what if I just want to maintain that nice little rational. So do remember, we do have the rational function. You can see it right there. It takes two arguments, the numerator comma the denominator. Now we've imported the rational function from some pi so we can just use the keyword as is. And so there we have rational four comma five times x comma 12, the left hand side and the right hand side. Let's print this to the screen. And now you can see it looks a bit better. It's four x over five equals 12. Now let's solve for that. Now once again as my first argument, I have the equation and then my second argument is the variable that I want the solution to be in. And we see the solution is simple, that's 15. Let's do one that's slightly more difficult. Look at this three times x plus two equals four times x plus three. Let's print that equation to the screen. This is really just an exercise in making sure that you don't make any errors when you enter. The left hand side and the right hand side of the equation, let's execute that. Now look what Python has done. It's done the distributing for me. It said three times x is three x, three times two is six and then four times x is four x and four times three is 12. So it's done that for me. Now I'm gonna copy and paste. There's my equation. I've made sure that everything is correct. I am passing that as first argument to the solve function comma I want to solve for x. And again we see the solution is negative six. Now this following one once again, it's an exercise in typing the correct code. Not to make any errors because we have parentheses inside of parentheses. Again, look at this equation and try and do this one on your own just to make sure that everything is okay. And now it's gonna do the simplification for me that distributing it's gonna have 27x minus 24 equals four x minus 33. Now I'm gonna pass that as my first argument to the solve function and then solve for x and we see the result negative nine over 23. If you had to do this by hand, it would have taken you quite some time. Let's solve the following. We have three x minus five over x plus three that equals 24 over 15. Again, I'm going to use my rational function there, rational 24 comma 15, 24 being the numerator, 15 being the denominator. Now you can only do that if you have these real numbers. Now you can only do that if you have these two integers, numerator and denominator, both of them being integers. Then you can use the rational function. I could not use the rational function on the left hand side because I have a polynomial divided by a polynomial. So I'm just going to use the division symbol right there. And there we go. Now look on the right hand side, it saw 24 over 15 and Simpa decided, well I can simplify that. I can divide both the numerator and denominator by three. And so I'd simplified 24 over 15, it becomes eight over five. Now again I can just pass that to the solve function, solve for x and I see my result very easy seven. On to the next one. Let's have x squared equals two. Let's set up that equation. Remember the power is double star symbols, x star star two, that's x squared. I printed to the screen and I see indeed x squared equals two. Let's pass that to the solve function and look at that. Now this is fantastic. We see that the result is positive and negative square root of two. Because if I take either of those two solutions and I square that, I am going to get the value two. Negative square root of two all squared, that's two. Square root of two all squared, well that's two. So we see both the positive and negative solution there. Now have a look at this. It says constrain x such that x is an element of this interval, closed interval on the left hand side, open interval on the right hand side. Do remember that means anything from zero to infinity on the left hand side zero is included. Because infinity is not a real number, we don't put a square bracket on the right hand side because infinity is not a specific value that we want to include in the interval. It just means go on forever and ever. So what we're trying to say here, x cannot be negative. What if I want to do the solution to x squared equals two, but x is constrained to be anything from zero and higher. So look what I've done here. I've reassigned x to be non-negative. So I said x equals symbols, x, but now I've used this keyword argument negative. And I said negative equals false. And of course if a number is not negative, it's got to be zero or more. Now that we've done that constrain, let's try and solve this equation x squared equals two again. And now you see I'm only gonna get the positive result. The positive square root of two. That negative square root of two is gone because I've constrained my solution to x not being negative. Indeed, it can only hold values from zero and any positive real value. So that's a very neat way to constrain your solutions given a problem that you might be dealing with. Let's have a look at this one. Solve the equation for the area of a circle given below for the radius r. And the equation's been given uppercase a equals pi r squared. So what would I have to do to solve this? Well, both a and r, they are two variables. Now let's create them and remember with the symbols argument, I can do more than one assignment at a time. So there I have a and r inside of quotation marks. This time I've put a comma. You can just put a space, that doesn't matter. On the left hand side though, you do need that comma. It's a comma r, the choice of the letter a and the choice of the letter r, that was up to me. But on the right hand side, I do want those two symbols. Now this time I've constrained them to be positive. So zero is now not included. It's anything more than zero because I'm not really interested in a circle that has a radius of zero. So let's create those two symbols a and r. And now let's set up the equation. I've got my equation on the left hand side, I have a. That's my first argument to the equation function, the eq function on the right hand side. I've got pi pi. Now remember I did import that from some pi. So I can just say pi times r squared. Let's print that to the screen. Look how beautiful that is. A equals pi r squared. Now let's solve for r. Now imagine I'm given the area of the circle and I wanna see what would the radius be of such a circle. I wanna solve for r. And so I put in my equation comma, and this time I want to solve for r. And isn't that fantastic? I see the square root of a divided by the square root of pi. I'm not gonna see any negatives there because I've constrained all my solutions to be positive. And now you can imagine you can set up any equation and you can decide which one of your variables do you wanna solve for. Really solving any equation for a specific variable is just so easy to do in Python using symbolic Python SMPI.