-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathforwardEuler.cpp
More file actions
42 lines (34 loc) · 846 Bytes
/
Copy pathforwardEuler.cpp
File metadata and controls
42 lines (34 loc) · 846 Bytes
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
// Author: Joshua McCarville-Schueths
// Class: Math 303
// File: forwardEuler.cpp
#include "forwardEuler.h"
forwardEuler::forwardEuler(const int n, const double delta)
{
m_y = new double[n+1];
m_t = new double[n+1];
m_n = n;
m_delta_t = delta;
}
forwardEuler::~forwardEuler()
{
delete [] m_y;
delete [] m_t;
}
// Applies the Forward Euler method of solving:
void forwardEuler::solve(double (*function)(const double y, const double t), const double y_init)
{
m_y[0] = y_init;
for(int i = 0; i < m_n; i++)
{
m_t[i] = i * m_delta_t;
m_y[i+1] = m_y[i] + (m_delta_t * function(m_y[i], m_t[i]));
}
m_t[m_n] = m_n * m_delta_t;
return;
}
ostream& operator<<(ostream &out, const forwardEuler &data)
{
for(int i = 0; i <= data.m_n; i++)
out << data.m_t[i] << "\t" << data.m_y[i] << endl;
return out;
}