fork download
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cmath>
  4.  
  5. double analitica(double x) {
  6. // Corregir la expresión para evitar divisiones enteras
  7. double y = -1 / ((x * x / 4 + 0.5 * x - 1));
  8. return y;
  9. }
  10.  
  11. double d_f(double x, double y) {
  12. return 0.5 * (1 + x) * pow(y, 2);
  13. }
  14.  
  15. double d_2_f(double x, double y) {
  16. return 0.5 * pow(y, 2) + (1 + x) * y * d_f(x, y);
  17. }
  18.  
  19. double d_3_f(double x, double y) {
  20. return 2 * y * d_f(x, y) + (1 + x) * pow(d_f(x, y), 2) + (1 + x) * y * d_2_f(x, y);
  21. }
  22.  
  23. double sol_taylor(double x0, double y0, double h) {
  24. double y_i;
  25. y_i = y0 + h * d_f(x0, y0) + 0.5 * pow(h, 2) * d_2_f(x0, y0) + (1.0 / 6) * pow(h, 3) * d_3_f(x0, y0);
  26. return y_i;
  27. }
  28.  
  29. int main() {
  30. double x0 = 0.0, y0 = -1.0; // Condiciones iniciales
  31. double h_values[] = {0.01, 0.001}; // Valores de h
  32. int n = sizeof(h_values) / sizeof(h_values[0]);
  33.  
  34. std::ofstream file("soluciones.txt"); // Archivo para almacenar resultados
  35.  
  36. for (int i = 0; i < n; ++i) {
  37. double h = h_values[i];
  38. int num_steps = static_cast<int>(1.0 / h); // Número de pasos
  39. double x = x0, y = y0;
  40.  
  41. file << "h = " << h << "\n";
  42. file << "x\ty\tSol. Analitica\n";
  43.  
  44. for (int j = 0; j <= num_steps; ++j) {
  45. file << x << "\t" << y << "\t" << analitica(x) << "\n";
  46. y = sol_taylor(x, y, h);
  47. x += h;
  48. }
  49. file << "\n";
  50. }
  51. file.close();
  52.  
  53. // Script para graficar
  54. std::ofstream script("grafica.gp");
  55. script << "set term png\n";
  56. script << "set output 'grafica.png'\n";
  57. script << "set xlabel 'x'\n";
  58. script << "set ylabel 'y'\n";
  59. script << "set title 'Solución de EDO con Método de Taylor'\n";
  60. script << "plot 'soluciones.txt' using 1:2 with lines title 'h=0.01',\\\n";
  61. script << " 'soluciones.txt' using 1:3 with lines title 'Sol. Analitica',\\\n";
  62. script << " 'soluciones.txt' using 1:2 with points title 'Datos h=0.01'\n";
  63. script << "replot 'soluciones.txt' using 1:4 with lines title 'h=0.001',\\\n";
  64. script << " 'soluciones.txt' using 1:3 with lines title 'Sol. Analitica',\\\n";
  65. script << " 'soluciones.txt' using 1:4 with points title 'Datos h=0.001'\n";
  66. script << "set key bottom right\n";
  67. script << "set grid\n";
  68. script << "set style fill transparent solid 0.5 noborder\n";
  69. script << "set object 1 rect from graph 0,0 to graph 1,1 behind fc rgb 'rainbow' \n";
  70. script << "set label 1 'Fisica computacional 1, autor Jorge Garzón' at graph 0.5, graph 0.1 center front\n";
  71. script << "set tics out\n";
  72. script << "replot\n";
  73. script.close();
  74.  
  75. // Ejecutar gnuplot
  76. system("gnuplot grafica.gp");
  77.  
  78. return 0;
  79. }
  80.  
Success #stdin #stdout #stderr 0.01s 5288KB
stdin
Standard input is empty
stdout
Standard output is empty
stderr
sh: 1: gnuplot: not found