Math with Python : Euler's Method

in #sndbox7 years ago (edited)

  • What is the Euler's Method?


There are many different methods that can be used to approximate solutions to a differential equation and in fact whole classes can be taught just dealing with the various methods. We are going to look at one of the oldest and easiest to use here. This method was originally devised by Euler and is called, oddly enough, Euler’s Method.

e^x = 1+ x+ x^2/2! + x^3/3! +x^4/4! + …

The equation e ^ iπ + 1 = 0 is called the Euler equation. e, i and π have different values and usage areas. But at the end of the Euler equation they are all reduced to a common integer. I guess this harmony is good for some people of your mathematics.


source | Euler's Method


source | Taylor Series

But instead of referring to Euler's beauty or mathematical solution-proof in this article, we will discuss how to combine it with Python, how to solve an Euler equation with Python. As we know, all numerical methods for solving differential equations rely on the idea of discretizing time into small steps. In fact, programming logic is similar. Use time better and solve problems as quickly as possible. Mathematics and programming are therefore always linked to one another.

The idea behind Euler’s method is very simple. We start at time t = 0 and x(t) is given as the initial condition; x(0) = x0 and now consider what must be the value a little time later, x(∆t). What a differential equation. It says is that the change in x, or ∆x, during a small time increment ∆t is given by ∆x = f (x)∆t. So, our equation is;

x(∆t) = x(0)+ f (x(0))∆t

Then we iterate this process and after that we have this equation x(t +∆t) = x(t)+ f (x(t))∆t. We repeat this iteration process until we reach the final time T .

  • Euler's Solution with Python


As you can see in our code example, we used the standard Python library for the first calculation when calculating the number of e, and used the decimal module for the second calculation. As a result of all this we got two different output, which are:

2.7182818284590455
2.7182818284590452

How did we get this output? Let's examine it step by step exactly what the code we wrote did.

We started by importing the factorial method from the math library. Then we created our variables named n, e, e1 and assigned their initial values. These values are of course not random, they are constant initial step values for the operations we will perform. Then we are coding mathematical operations in accordance with the Euler formula to find the output to be obtained in an iteration.

from math import factorial
n,e,e1 = 0,1,0

Every time we do n = n + 1 and get its 1/factorial. This process continues until e and e1 are equal to each other. When equality is provided, we exit the while loop. We then import the decimal library and repeat the same process. The only difference is that we are doing e1 + = Decimal(1/factorial(n)) instead of e1 + = 1/factorial(n). When we go out of the second while loop, we are no longer able to show output from each iteration. We do this with a simple for loop.

e = 0
for n in range(20):
e += Decimal(1/factorial(n))
print(e)

In other words, the way we work with the code we write is generally this way. We find each iteration one by one and then print them on the screen.

Info Source



@monomyth

Sort:  

This is a test comment, notify @kryzsec on discord if there are any errors please.


GuidelinesProject Update

Being A SteemStem Member

Awesome post. Admittedly it lost me a couple times, but I'll go back and reread it. Long live pythonistas!

I accidentally discovered this post, and so far looks so cool to me. I know nothing about python or coding in general, but i do enjoy maths in any shaper :D ! I can understand a bit of factorial and delta function and i can generally understand the values of sin and cos hehe. Lovely post :D !

Haha thank's man, you can learn Python it is realy cool programming language, just try it :)

I concur Python is really really cool programming language. Crisp & clean ;)

Hi! How can one learn python from scratch the easiest way mate? Can you recommend something ? Cheers :D !

Hi man, you start with this https://docs.python.org/3/tutorial/ Cheers :)

Hi Hookah,

Please go to coursera .org & check out Dr Charles Severance's "Python for Everybody Specialization"
You will learn a lot from Dr Charles Severance & he is kinda jovial & makes the subject very enjoyable.
Here is the link ... Go sign up & it's free if you don't the certificate ;)

https://www.coursera.org/specializations/python

Coin Marketplace

STEEM 0.17
TRX 0.16
JST 0.029
BTC 60661.72
ETH 2395.32
USDT 1.00
SBD 2.56