Example of function using Ipython | applied electronics engineering

Latest

Example of function using Ipython

By Applied Electronics - Wednesday, April 5, 2017 No Comments
Here is an illustration of an example of function using Ipyhon. Consider we want to implement equation of motion using function. A function in python programming language starts with the keyword def followed by the function name and argument list inside brackets.

We can start Ipython using cmd and then typing Ipython. Ipython will launch and we can write our function. The function is as follows.

def y(t):
      v0 = 4
      g = 9.81
      return v0*t - 0.5*g*t**2

Now when we want to use this function we just supply the argument which is time. For example,

y(0.5)

outputs the following,

 0.77374999999999

Similarly,

y(0.1)

outputs the following,

0.3595

See the following picture:


You can pass in multiple parameters to a function. For example the famous Einstein equation is implemented as function using Ipython as follows,


Here the function name is e and its two parameters m for mass and c for speed of light.

Similarly you can also implement lambda function in Ipython.

The following is an example of using lambda function for implementing straight line. The straight line takes two 3 inputs m, the slope, x the x-axis value and the c the intercept constant. In terms of lambda function we can do this in the following manner.

y = lambda m,x,c: m*x+c

and then we can pass the three argument m, x and c as follows,

y(1,2,3)

we get,

5

as the result

No Comment to " Example of function using Ipython "