forked from Zura1101/NUMERICAL-METHODS
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathEulers_Method .py
More file actions
77 lines (66 loc) · 1.57 KB
/
Copy pathEulers_Method .py
File metadata and controls
77 lines (66 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
import math
import sympy as sym
def f(x, y):
fx= x**3 - 6*x**2 + 11*x - 6 # Function Input---------
return fx
# or fx = lambda x: x+y
# Enter initial conditions
x0 = 0.5
y0 = 0.5
xn = 1
h = 0.2
# h = (xn - x0) / n
# n =
step = ((xn - x0) / h)
# Euler method
def euler(x0, y0, xn, n):
# Calculating step size
h = (xn - x0) / n
print('\n-----------SOLUTION---------------')
print('----------------------------------')
print('n\tx0\t\ty0\t\tslope\tyn')
print('----------------------------------')
for i in range(int(n)):
slope = f(x0, y0)
yn = y0 + h * slope
print(i,'\t%.3f\t%.3f\t%0.3f\t%.3f' % (x0, y0, slope, yn))
print('----------------------------------')
y0 = yn
x0 = x0 + h
print('At x=%.3f, y=%.3f' % (xn, yn))
# Euler method call
euler(x0, y0, xn, step)
# -----------------------------------------------
# # Consider a differential equation
# # dy / dx =(x + y + xy)
#
# def func(x, y):
# fx = x +2*y
# return (fx)
#
# # Driver Code
# # Initial Values
# x0 = 1
# y0 = 1
# h = 0.1
#
# # Value of x at which we need approximation
# x = 2
#
#
# # Function for euler formula
# def euler(x0, y, h, x):
# temp = -0
#
# # Iterating till the point at which we
# # need approximation
# while x0 < x:
# temp = y
# y = y + h * func(x0, y)
# x0 = x0 + h
#
# # Printing approximation
# print("Approximate solution at x = ", x, " is ", "%.6f" % y)
#
#
# euler(x0, y0, h, x)