-
Notifications
You must be signed in to change notification settings - Fork 0
/
Eulermethod.cpp
61 lines (53 loc) · 1.22 KB
/
Eulermethod.cpp
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
/*Consider below differential equation
dy/dx = (x + y + xy)
with initial condition y(0) = 1
and step size h = 0.025.
Find y(0.1).
Solution:
f(x, y) = (x + y + xy)
x0 = 0, y0 = 1, h = 0.025
Now we can calculate y1 using Euler formula
y1 = y0 + h * f(x0, y0)
y1 = 1 + 0.025 *(0 + 1 + 0 * 1)
y1 = 1.025
y(0.025) = 1.025.
Similarly we can calculate y(0.050), y(0.075), ....y(0.1).
y(0.1) = 1.11167
*/
/* C++ Program to find approximation
of a ordinary differential equation
using euler method.*/
#include <iostream>
using namespace std;
// Consider a differential equation
// dy/dx=(x + y + xy)
float func(float x, float y)
{
return (x + y + x * y);
}
// Function for Euler formula
void euler(float x0, float y, float h, float x)
{
float 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
cout << "Approximate solution at x = "
<< x << " is " << y << endl;
}
int main()
{
// Initial Values
float x0 = 0;
float y0 = 1;
float h = 0.025;
// Value of x at which we need approximation
float x = 0.1;
euler(x0, y0, h, x);
return 0;
}