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 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
|
#include <math.h>
#include <vector>
#include <string>
#include <iostream.h>
using namespace std;
double Get_f_of_y_values(int i, vector<double>& t_values, vector<double>& y_values)
{
double Get_f_of_y_values;
Get_f_of_y_values = ((- 8.0 * t_values[i]) / (1 + (t_values[i] * t_values[i]))) * y_values[i];
return Get_f_of_y_values;
}
double Get_f_of_x_values(int i, int j, vector<double>& t_values, vector<double>& x_values)
{
double Get_f_of_x_values;
Get_f_of_x_values = ((-8.0 * t_values[i + 1]) / (1 + (t_values[i + 1] * t_values[i + 1]))) * x_values[j];
return Get_f_of_x_values;
}
double Get_g_of_x_values(int i, int j, vector<double>& f_of_y_values, vector<double>& f_of_x_values, vector<double>& t_values,
vector<double>& x_values, vector<double>& y_values, double h)
{
double Get_g_of_x_values;
Get_g_of_x_values = (0.5 * h * (f_of_y_values[i] + f_of_x_values[j])) + y_values[i] - x_values[j];
return Get_g_of_x_values;
}
double Get_g_primed_of_x_values(int i, vector<double>& t_values, double h)
{
double Get_g_primed_of_x_values;
Get_g_primed_of_x_values = (0.5 * h * (( - 8.0 * t_values[i + 1])/( 1 + (t_values[ i + 1 ] * t_values[i + 1])))) - 1;
return Get_g_primed_of_x_values;
}
double Newton_Raphson(int j, vector<double>& g_values, vector<double>& g_primed_values, vector<double>& x_values)
{
double Newton_Raphson;
Newton_Raphson = x_values[j] - (g_values[j]/g_primed_values[j]);
return Newton_Raphson;
}
double Newton_Raphson_Error(int j, vector<double>& x_values)
{
double Newton_Raphson_Error;
Newton_Raphson_Error = fabs(x_values[j + 1] - x_values[j]);
return Newton_Raphson_Error;
}
int main(){
//define vectors
vector<double> f_of_x_values;
vector<double> f_of_y_values;
vector<double> g_values;
vector<double> g_primed_values;
vector<double> t_values;
vector<double> x_values;
vector<double> y_values;
//define stepsize
double newton_raphson_error;
double N = 10.0;
int n = 20;
int i, j;
double h = N / n;
//define vector size
f_of_x_values.reserve(n+1);
f_of_y_values.reserve(n+1);
g_values.resize(j+1);
g_primed_values.resize(j+1);
t_values.reserve(n+1);
x_values.resize(j+1);
y_values.reserve(n+1);
//initialise vectors
f_of_y_values[0]=0;
f_of_x_values[0]=0;
g_values[0]=0;
g_primed_values[0]=0;
t_values[0]=0;
x_values[0]=1;
y_values[0]=1;
//begin computation
for ( i = 0; i < n; i++ ){
t_values[i] = i * h;
f_of_y_values[i] = Get_f_of_y_values(i, t_values, y_values);
for ( j = 0; Newton_Raphson_Error(j, x_values) < 0.01; j++ ){
f_of_x_values[j] = Get_f_of_x_values(i, j, t_values, x_values);
g_values[j] = Get_g_of_x_values(i, j, f_of_y_values, f_of_x_values, t_values, x_values, y_values, h);
g_primed_values[j] = Get_g_primed_of_x_values(j, t_values, h);
x_values[j+1] = Newton_Raphson(j, g_values, g_primed_values, x_values);
newton_raphson_error = Newton_Raphson_Error(j, x_values);
cout << x_values[j+1] << endl;
}
}
return 0;
}
|