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
|
#include <iostream>
#include <math.h>
using namespace std;
double Kw = 1e-14;
int Newton_Length;
double Newton_Results [];
double pH_function (double H, double Va, double Ca, double Ka, double Vb, double Cb, double Kb)
{
double fH;
fH = Va*((Kw/H)-H+Ca*(Ka/(Ka+H)))-Vb*(H-(Kw/H)+Cb*(H/(Kb+H)));
return fH;
}
double pH_function_1d (double H, double Va, double Ca, double Ka, double Vb, double Cb, double Kb)
{
double fH;
fH = Vb*(-(Kw/pow(H,2))-1-Cb*(Kb/(pow(Kb+H,2))))-Va*((Kw/pow(H,2))+1+Ca*(Ka/(pow(Ka+H,2))));
return fH;
}
void Newton (double Newton_Va, double Newton_Ca, double Newton_Ka, double Newton_Vb, double Newton_Cb, double Newton_Kb)
{
double fx, fx_1d;
double diff = 1;
Newton_Length = 0;
Newton_Results [0] = 1e-14;
while (diff > 0.001)
{
fx = pH_function (Newton_Results [Newton_Length], Newton_Va, Newton_Ca, Newton_Ka, Newton_Vb, Newton_Cb, Newton_Kb);
fx_1d = pH_function_1d (Newton_Results [Newton_Length], Newton_Va, Newton_Ca, Newton_Ka, Newton_Vb, Newton_Cb, Newton_Kb);
Newton_Results [Newton_Length + 1] = Newton_Results [Newton_Length] - (fx/fx_1d);
Newton_Length ++;
diff = fabs((-log10(Newton_Results [Newton_Length])) - (-log10(Newton_Results [Newton_Length + 1])));
}
}
int main()
{
//Basis input
double Main_Va, Main_Ca, Main_pKa, Main_Vb, Main_Cb, Main_pKb;
cout << "Volume van het zuur? ";
cin >> Main_Va;
cout << "Concentratie van het zuur? ";
cin >> Main_Ca;
cout << "Zuurconstante (pKah) van het zuur? ";
cin >> Main_pKa;
cout << "Volume van de base? ";
cin >> Main_Vb;
cout << "Concentratie van de base? ";
cin >> Main_Cb;
cout << "Zuurconstante (pKbh+) van de base? ";
cin >> Main_pKb;
//Omvorming pKa en pKb naar Ka en Kb
double Main_Ka, Main_Kb;
Main_Ka = pow(10, -Main_pKa);
Main_Kb = pow(10, -Main_pKb);
Newton (Main_Va, Main_Ca, Main_Ka, Main_Vb, Main_Cb, Main_Kb);
int Max_Length = 0;
int cycle = 0;
if (Max_Length < Newton_Length)
{
Max_Length = Newton_Length;
}
while (cycle<Max_Length)
{
cout << "Cyclus Newton (pH) \n";
cout << cycle << " " << -log10(Newton_Results [cycle]);
}
system("PAUSE");
return 0;
}
|