fork download
  1. //Code Entirely typed by Terence Cox
  2. #include <iostream>
  3. #include <string>
  4. #include <cstdlib>
  5. #include <math.h>
  6. #include <iomanip>
  7.  
  8. using namespace std;
  9.  
  10. string substitute(double, double, string);
  11. string simplify(bool, char, string);
  12. double** fill2Ddyrow7(int, int, double, double, double, double, double, double, double**);
  13.  
  14. double evaluator(string p_equ, bool debug)
  15. {
  16. start1:
  17. int fp = -1, lp = -1;
  18. //Search for Paraenthesis
  19. for (int c = 0; c < p_equ.size(); c++)
  20. {
  21. if (p_equ.at(c) == '(')
  22. fp = c;
  23. if (p_equ.at(c) == ')')
  24. {
  25. lp = c;
  26. break;
  27. }
  28. }
  29. //if parenthesis exist extract the expression within them
  30. if (fp < 0 && lp < 0)
  31. {
  32. if (debug == true)
  33. cout << "no more parentheses" << endl;
  34. }
  35. else
  36. {
  37. //extract from parenthesis and start the evauluation with the rest, then replaces solution in primary equ
  38. string s_equ(p_equ, fp + 1, (lp - 1) - fp);
  39. //solves what's in the parenthesis and eliminates the ()'s
  40. p_equ.replace(fp, (lp + 1) - fp, to_string(evaluator(s_equ, debug)));
  41. goto start1;
  42. }
  43. //perform PEMDAS
  44. p_equ = simplify(debug, '-', simplify(debug, '+', simplify(debug, '*', simplify(debug, '/', simplify(debug, '^', p_equ)))));
  45.  
  46. //string fsresult(p_equ, fp + 1, lp - fp);
  47. double fresult = stod(p_equ);
  48. return fresult;
  49. }
  50.  
  51.  
  52. string simplify(bool debug, char oper, string expr)
  53. {
  54. start2:
  55. int p, fnfp = -1, snlp = -1, reader = 1, runs = 0;
  56. double num1, num2, result;
  57. bool single_neg = false;
  58.  
  59. p = -1;
  60. for (int c = 0; c < expr.size(); c++)
  61. {
  62. if (expr.at(c) == oper)
  63. {
  64. if (c > 0) //prevents error when a negative number is first in the expression
  65. {
  66. //assigns the position of the operator to p
  67. p = c;
  68. break;
  69. }
  70.  
  71. }
  72. }
  73. if (p < 0)
  74. {
  75. if (debug == true)
  76. cout << "no " << oper << endl;
  77. }
  78. else
  79. {
  80. runs++;
  81. while (expr.at(p - reader) == '-' || expr.at(p - reader) == '.' || isdigit(expr.at(p - reader)))
  82. {
  83. fnfp = p - reader;
  84. reader++;
  85. if (p - reader < 0) //handles exception where this reads outside of memory
  86. break;
  87. if (expr.at(fnfp) == '-')//hnadles a runtime error which causes negative numbers to be omitted
  88. {
  89. break;
  90. }
  91. }
  92. reader = 1;
  93. while (expr.at(p + reader) == '-' || expr.at(p + reader) == '.' || isdigit(expr.at(p + reader)))
  94. {
  95. snlp = p + reader;
  96. //This code checks to make sure only one negative symbol is allowed, will break if another is found.
  97. if (expr.at(snlp) == '-')
  98. {
  99. if (single_neg == true)
  100. break;
  101. else
  102. single_neg = true;
  103. }
  104. reader++;
  105. if (p + reader > expr.size() - 1) //handles exception where this reads outside of memory
  106. break;
  107. }
  108. string strnum1(expr, fnfp, p - fnfp);
  109. string strnum2(expr, p + 1, snlp - p);
  110. double num1 = stod(strnum1);
  111. double num2 = stod(strnum2);
  112.  
  113.  
  114. switch (oper)
  115. {
  116. case '^':
  117. result = pow(num1, num2);
  118. break;
  119. case '/':
  120. result = num1 / num2;
  121. break;
  122. case '*':
  123. result = num1 * num2;
  124. break;
  125. case '+':
  126. result = num1 + num2;
  127. break;
  128. case '-':
  129. result = num1 - num2;
  130. break;
  131. }
  132. //result = (int)(result * 10000.0) / 10000.0;
  133. if (debug == true)
  134. cout << "after performing " << oper << " the result is " << result << endl;
  135. //replace the solve part of the expression into the entire expression
  136. expr.replace(fnfp, (snlp + 1) - fnfp, to_string(result));
  137. goto start2;
  138. }
  139. if (debug == true)
  140. cout << "solved " << runs << " " << oper << " operations" << endl;
  141. return expr;
  142. }
  143.  
  144. string substitute(double X, double Y, string equ)
  145. {
  146. //checks each character for X or Y to replace them with actual values
  147. for (int c = 0; c < equ.size(); c++)
  148. {
  149. if (equ.at(c) == 'X' || equ.at(c) == 'x')
  150. equ.replace(c, 1, to_string(X));
  151. if (equ.at(c) == 'Y' || equ.at(c) == 'y')
  152. equ.replace(c, 1, to_string(Y));
  153. }
  154. return equ;
  155. }
  156.  
  157. double** fill2Ddyrow7(int row, int step, double X, double Y, double K1, double K2, double K3, double K4, double** tablef)
  158. {
  159. tablef[row][0] = step;
  160. tablef[row][1] = X;
  161. tablef[row][2] = Y;
  162. tablef[row][3] = K1;
  163. tablef[row][4] = K2;
  164. tablef[row][5] = K3;
  165. tablef[row][6] = K4;
  166. return tablef;
  167. }
  168.  
  169.  
  170.  
  171. int main()
  172. {
  173. Begin:
  174. cout.precision(5);
  175. string equation;
  176. int steps;
  177. float X1, X2, Y1, K1, K2, K3, K4, Z2, Z3, Z4, h, newY;
  178. double change;
  179. bool debug = false, use_table = true, userres;
  180. cout << "What is the differential equation you want to use Runge Kutta's method on?\ny'= ";
  181. cin >> equation;
  182. cout << "Fill this in\n (y(x) = ___)\n | \n v \nx= ";
  183. cin >> X1;
  184. cout << "and this\n (y(" << X1 << ") = __)\n |\n v\n y= ";
  185. cin >> Y1;
  186. cout << "How many steps of accuracy do you want to use: ";
  187. cin >> steps;
  188. cout << "What is the value of X you want to use to solve Y" << endl;
  189. cin >> X2;
  190. h = (X2 - X1) / steps;
  191. cout << "The size of h is " << h << "." << endl;
  192. cout << "Last Question, do you want to see the table of values(Takes longer to process) 1 or 0" << endl;
  193. cin >> use_table;
  194. //create a new 2D Dynamic Array
  195. double** table = new double*[steps + 1];
  196. for (int i = 0; i < steps + 1; ++i)
  197. table[i] = new double[6];
  198.  
  199. //fill in table with obtained information
  200. table = fill2Ddyrow7(0, 0, X1, Y1,0, 0, 0, 0, table);
  201.  
  202. //perform the Runge Kutta process for the inputted amount of steps
  203. //'t' represents current row
  204. for (int t = 0; t < steps; t++)
  205. {
  206. K1 = evaluator(substitute(table[t][1], table[t][2], equation), debug);
  207. table[t][3] = K1;
  208. Z2 = table[t][2] + 0.5*abs(h)*K1;
  209. K2 = evaluator(substitute(table[t][1] + 0.5*abs(h), Z2, equation), debug);
  210. table[t][4] = K2;
  211. Z3 = table[t][2] + 0.5*abs(h)*K2;
  212. K3 = evaluator(substitute(table[t][1] + 0.5*abs(h), Z3, equation), debug);
  213. table[t][5] = K3;
  214. Z4 = table[t][2] + abs(h)*K3;
  215. K4 = evaluator(substitute(table[t][1] + abs(h), Z4, equation), debug);
  216. table[t][6] = K4;
  217. newY = table[t][2] + (abs(h)*(K1+(2*K2)+(2*K3)+K4))/6;
  218.  
  219. table = fill2Ddyrow7(t + 1, t + 1, X1 + (h*(t + 1)), newY, 0, 0, 0, 0, table);
  220. }
  221.  
  222. //outputs the values in the table into the console
  223. if (use_table == true || use_table == 't' || use_table == 'T')
  224. {
  225. cout << left << setw(11) << "step i" << setw(11) << "X1" << setw(11) << "Y1" << setw(11) << "K1" << setw(11) << "K2" << setw(11) << "K3" << setw(11) << "K4" << endl;
  226. for (int row = 0; row < steps + 1; row++)
  227. {
  228. cout << endl;
  229. for (int column = 0; column < 7; column++)
  230. {
  231. cout << setw(11) << table[row][column];
  232. }
  233. }
  234. }
  235. cout << endl;
  236. cout << "The Final Answer is " << table[steps][2] << endl;
  237. cout << "Done" << endl;
  238. cout << "Do you want to enter another Diffy-Q?, 1(True) or 0(False)" << endl;
  239. cin >> userres;
  240. if (userres == 1)
  241. goto Begin;
  242. system("pause");
  243. }
  244.  
  245.  
  246.  
Success #stdin #stdout #stderr 0s 15248KB
stdin
(x^2)/(y-1)
-1
-.5
20
2
1
0
stdout
What is the differential equation you want to use Runge Kutta's method on?
y'= Fill this in
 (y(x) = ___)
    |     
    v     
x= and this
 (y(-1) = __)
           |
           v
        y= How many steps of accuracy do you want to use: What is the value of X you want to use to solve Y
The size of h is 0.15.
Last Question, do you want to see the table of values(Takes longer to process) 1 or 0
step i     X1         Y1         K1         K2         K3         K4         

0          -1         -0.5       -0.66667   -0.55202   -0.5551    -0.45634   
1          -0.85      -0.58343   -0.45629   -0.37129   -0.37276   -0.2989    
2          -0.7       -0.63951   -0.29887   -0.23504   -0.23572   -0.18061   
3          -0.55      -0.67504   -0.18059   -0.13362   -0.1339    -0.094388  
4          -0.4       -0.69529   -0.094379  -0.062046  -0.062134  -0.036665  
5          -0.25      -0.70477   -0.036662  -0.017935  -0.01795   -0.005857  
6          -0.1       -0.70763   -0.005856  -0.000366  -0.000366  -0.001464  
7          0.05       -0.70785   -0.001464  -0.009148  -0.009145  -0.023402  
8          0.2        -0.70939   -0.0234    -0.044196  -0.044155  -0.071387  
9          0.35       -0.71617   -0.07138   -0.10492   -0.10477   -0.14435   
10         0.5        -0.73205   -0.14434   -0.1897    -0.18933   -0.24      
11         0.65       -0.76061   -0.23997   -0.29553   -0.29484   -0.3546    
12         0.8        -0.80499   -0.35457   -0.41801   -0.41693   -0.48326   
13         0.95       -0.86769   -0.48322   -0.55182   -0.55033   -0.62044   
14         1.1        -0.95039   -0.62039   -0.69138   -0.68954   -0.76078   
15         1.25       -1.054     -0.76073   -0.83165   -0.82956   -0.89974   
16         1.4        -1.1785    -0.89969   -0.96866   -0.96644   -1.034     
17         1.55       -1.3236    -1.0339    -1.0997    -1.0975    -1.1615    
18         1.7        -1.4884    -1.1614    -1.2233    -1.2211    -1.2811    
19         1.85       -1.6717    -1.281     -1.3389    -1.3368    -1.3927    
20         2          -1.8723    0          0          0          0          
The Final Answer is -1.8723
Done
Do you want to enter another Diffy-Q?, 1(True) or 0(False)
stderr
sh: 1: pause: not found