Python Programming Tutorials - Mathematical Functions

in python •  7 years ago 

So this is going to be the real tutorial that we do on a Python in mathematics and we're going to talk about mathematical functions which hopefully at this point you're familiar with and then Python functions so let's get started.

python-logo.png

First thing I'm going to talk about of course is something that you should probably be familiar with and I guess if you're not this is going to be a crash course on that.

So this reads f of X is equal to and I'm going to say x squared plus 2 all right so this is this f is the name of the function and this is a mathematical function.

So if I plug in F of 0 into our function this is 0 squared plus 2 and it is equal to 2 so we could say then that F of 0 is equal to 2.

We could also write this as an ordered pair 0 comma 2.

You could also graph this if we're on the real number line and our domain is from negative infinity to positive infinity all right this is called a continuous function it's continuous there's no breaks or holes or asymptotes in the function and of course if you were to graph this.

Now in Python we're going to hold off on the graphing for just a little bit. So we have x squared plus 2 so this is a parabola and it's been shifted up two units.

Alright so what I'm gonna do in Python is we're going to have an output that looks like the picture above. And of course we can plug in any number we want and for X.

And okay well let's just get started so when I started at Python the shell comes up and then you'll probably need to come up here and click file and hit and hit new window.

Alright the first thing you're going to want to do is import we have to import the math module and we're going to define a function we're going to call it f of X so just follow my lead we're going to say Y is equal to math dot this is for the power the function is X and we're going to raise it to the second power and then plus three okay I'm going to add a comment out here and that's with a pound sign so writing this out mathematically okay, look like this x squared plus three, okay.

Import math

def f(x):
    y = math.pow(x, 2)+3                #y = x^2 + 3
    return y

and then what we're going to do is we're going to let's see what am I going to do let's just do return we're going to return y.

And we're going to run this and see what we get. So we want to hit run module we have to so when we do that we have to save that so this little screen right here saves of course I've already gotten mine saved.

>>> f(0)
3.0
>>> 

Now I'm going to type in F of 0 and there we go 3.

>>> f(10)
103.0
>>> 

So 0 squared plus 3 is or we could say F of 10 and prints out on the screen 103 point 0.

>>> f(-1)
4.0
>>> 

Or how about F of negative one and we get four so let's look at this negative one squared plus three is four okay that works out.

We're gonna expand on this idea so we're going to do composition of functions.

I'm going to have a function f of X is equal to x squared plus three and then we'll have another function we'll call it G of X is equal to X plus one and we want to find G composed of F and sometimes our notation looks like this of X which is also can be written as G of f of X.

We're going to use Python to do our work for us we're going to set up these two functions in Python then we're going to do a composite file a composite function and what we're going to first do is find G of f of two.

So real quick refresher here right f of 2 is equal to 2 squared plus 3 which is 7 and then we take that and plug it in to G of X which is going to be 7 plus 1 which is going to equal 8 so G of f of 2 is equal to 8.

So let's check that out in Python.

So the first thing I'm going to do here of course is I think actually I use the same function in the last tutorial this is actually what it looks like right x squared plus 3 so we set this up so when we plug a number and we're going to return out Y which is when this is calculated right and which was going to be 7 and then

we are constructing our G of X and which is X plus 1 and it's going to return out our Y.

And then I'm using this capital C here as the composed operator the dot or though it's actually a little circle but so this just reads G composed of f of X ok and look at here when we set this up correctly we can write it out just like a mathematical function so just like I wrote this was G composed of f of X and then out spits our Y so let's go check it out and see how it works out but first remember we go up to run and then we run the module and they ask to save it and of course we'll have to save because I've already ran this earlier and now what we got to do is remember

>>> gCf(2)
8.0
>>> 

what that was it was g capital C we could have called it anything we want but I try to keep it as close to the mathematical part as possible and we're gonna plug a to end and there it is out spits an eight.

>>> gCf(10)
104.0
>>> 

so let's plug in a 10 and 104 alright.

>>> gCf(1)
5.0
>>> 

Let's plug in a 1 let's see what we get we get a 5

>>> gCf(-2)
8.0
>>> 

about a negative 2 we get 8 again so plug in 2 you get 8.

>>> gCf(-1)
5.0
>>> 

Happens when we plug in negative one will get five again so it looks like there's some symmetry in this problem

>>> gCf(0)
4.0
>>> 

plug in zero and you get four out.

Okay so we demonstrated mathematical functions and of course these are known as Python for calling up a Python function here that we constructed so we get we got a field we're going to try to stay as close to the mathematical notation as we possibly can.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!