-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEulers-method-to-screen.cpp
77 lines (66 loc) · 1.55 KB
/
Eulers-method-to-screen.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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
/* EulersMethod.cpp
*
* A simple implementation of Euler's method
* to find a numerical solution to a
* first order ODE that has the form
*
* dy/dx = f(x,y), y(a) = y0
*
* on the interval [a,b].
*
* We start at y0, and then use
*
* y[n] = y[n-1] + h f(x[n-1],y[n-1])
*
* where h is the stepsize.
*
* Author: cmhughes
* Date: November 11th, 2016
*
* Reference: pgs 126 -- 127 of Practical C++ programming
*
*/
#include <iostream>
int main() {
/* declare the right hand side function */
double f(double x, double y);
/* specify the interval [a,b] */
double a = 1;
double b = 4;
/* and the initial value y0 and h */
double y0 = 1;
double h = 0.1;
/* x and y will be updated in the following loop */
double x=a;
double y=y0;
/* this loop implements the algorithm, and outputs to screen */
while(x<=b){
// y[n] = y[n-1] + h*f(x[n-1],y[n-1])
y += h*f(x,y);
// add h to x
x += h;
// output to screen
std::cout << "x is " << x << " and y is " << y << "\n";
}
/* output to screen */
return (0);
}
/* f : provides the value of the right hand side function f(x,y)
* in the differential equation
*
* dy/dx = f(x,y)
*
* Parameters (inputs):
* x (double precision)
* y (double precision)
*
* Returns (output):
* f(x,y): function evaluated at (x,y)
*
* Sample use:
* f(42, 5)
*/
double f(double x, double y){
// return the right hand side value
return (2*y-1);
}