-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathEulers-method-to-file.cpp
87 lines (74 loc) · 1.96 KB
/
Eulers-method-to-file.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
78
79
80
81
82
83
84
85
86
87
/* 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>
#include <fstream>
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;
/* create a file handle for the output */
std::ofstream out_file("euler.dat");
/* heading in the output file */
out_file << "# This file is called euler.dat\n";
out_file << "# and was created by Eulers-method-to-file.cpp\n";
out_file << "# Do not edit directly! Edit using Eulers-method-to-file.cpp\n";
out_file << "# x\ty\n";
/* this loop implements the algorithm, and outputs to file */
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 file, see pg 250 of Practical C++ Programming
out_file << x << "\t" << 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){
// the right hand side function can be any function of x or y
return (2*y-1);
}