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