Advertisement
Guest User

Untitled

a guest
Apr 28th, 2015
196
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.70 KB | None | 0 0
  1. // сплайн.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include <iostream>
  6. #include <math.h>
  7. #include <fstream>
  8. #define Pi 3.1415926535897932384626433832795
  9. #define a 0.0
  10. #define b 2.0
  11. #define n 20
  12. using namespace std;
  13.  
  14. //пишем функцию тесттовую
  15. double f(double x)
  16. {
  17. return 3*x*x*x+2*x*x+x+11;//sin(x)+cos(x);
  18. }
  19.  
  20. int main()
  21. {
  22. int i;
  23. //массив сетки неравномерной
  24. //объявление
  25. double *h, *x, *y, *g, *z, *d, *c, *e, h_max=0;
  26. //создаем
  27. h=new double [n+1];
  28. h[0]=0;//h[0] не будем использовать-для удобства нумерации
  29. x=new double [n+1];
  30. y=new double [n+1];
  31. z=new double [n];// массив правой части
  32. e=new double [n];// верхние диагональные элементы, 0й элемент не будем использовать для удобства нумерации
  33. //массив неизвестных коэффициентов c
  34. c=new double [n+1];
  35. g=new double [n+1];
  36. d=new double [n+1];
  37.  
  38.  
  39. //cчитываем сетку с файла
  40. ifstream ff("in.txt");
  41. for(i=0;i<=n;i++) ff>>x[i];
  42. ff.close();
  43.  
  44.  
  45. for(i=1;i<=n; i++)
  46. h[i]=x[i]-x[i-1];
  47.  
  48.  
  49. /* //задаем равномерную сетку
  50. h_max=(b-a)/(n+0.0);
  51. for(i=1;i<=n;i++) h[i]=h_max;
  52. for(i=0;i<=n;i++) x[i]=a+i*h_max;
  53.  
  54.  
  55. */
  56.  
  57. for (i=0; i<=n; i++)
  58. y[i]=f(x[i]);
  59.  
  60. //вычисляем вторую производную
  61.  
  62. if(h[1]<h[2]) h_max=h[2];
  63. else h_max=h[1];
  64. c[0]=(y[0]-2*y[1]+y[2])/h_max;
  65.  
  66. if(h[n-1]<h[n]) h_max=h[n];
  67. else h_max=h[n-1];
  68. c[n]=(y[n]-2*y[n-1]+y[n-2])/h_max;
  69.  
  70. c[0]=4;
  71. c[n]=40;
  72. //найдем коэффициенты c с помощью метода прогонки
  73.  
  74. //зададим правый столбец системы
  75. for (i=1; i<n; i++)
  76. z[i]=6.0*(y[i+1]-y[i])/h[i+1]-6.0*(y[i]-y[i-1])/h[i];
  77.  
  78. z[1]-=h[1]*c[0];
  79. z[n-1]-=h[n]*c[n];
  80.  
  81. // вычисление верхних диагональных элементов
  82. e[1]=h[2]/(2.0*(h[1]+h[2]));
  83.  
  84. for (i=2; i<n; i++)
  85. e[i]=h[i+1]/(2.0*(h[i]+h[i+1])-h[i]*e[i-1]);
  86.  
  87. //пересчитываем правый столбец
  88. z[1]=z[1]/(2.0*(h[1]+h[2]));
  89. for (i=2; i<n; i++)
  90. z[i]=(z[i]-h[i]*z[i-1])/(2.0*(h[i]+h[i+1])-h[i]*e[i-1]);
  91.  
  92. //обратный ход
  93. c[n-1]=z[n-1];
  94.  
  95. for(i=n-2; i>=1; i--)
  96. c[i]=z[i]-e[i]*c[i+1];
  97.  
  98. //УРА!!!! нашли коэффициенты с, теперь находим коэффициенты d and g
  99. for(i=1;i<=n;i++)
  100. {
  101. d[i]=(c[i]-c[i-1])/h[i];
  102. g[i]=h[i]*c[i]/2.0-h[i]*h[i]*d[i]/6.0+(y[i]-y[i-1])/h[i];
  103. }
  104. double xo=0.2, s;//s - значение сплайна в точке хо
  105.  
  106. //найдем интервал, которому принадлежит точка хо
  107. for(i=1;i<=n;i++)
  108. if(x[i]-xo>0) break;
  109.  
  110. //s=y[i-1]*(x[i]-xo)/h[i]+y[i]*(xo-x[i-1])/h[i]+g[i-1]*(pow((x[i]-xo),3)-h[i]*h[i]*(x[i]-xo))/6*h[i]+g[i]*(pow((xo-x[i-1]),3)-h[i]*h[i]*(xo-x[i]))/6*h[i];
  111. s=y[i]+g[i]*(xo-x[i])+c[i]*(xo-x[i])*(xo-x[i])/2.0+d[i]*(xo-x[i])*(xo-x[i])*(xo-x[i])/6.0;
  112. cout<<s<<'\n';
  113. cout<<f(xo)<<'\n';
  114.  
  115. //записать в файл для того, что бы затем построить график
  116.  
  117. ofstream off("out.txt");
  118. //off<<"with(plots);"<<endl<<endl;
  119. off<<"p1:=plot(function, x="<<a<<".."<<b<<", color=blue):"<<endl<<endl;
  120. for(i=1; i<=n;i++)
  121. off<<"s"<<i<<":=plot("<<y[i]<<"+("<<g[i]<<")*(x-"<<x[i]<<")+("<<c[i]/2.0<<")*(x-"<<x[i]<<")^2+("<<d[i]/6.0<<")*(x-"<<x[i]<<")^3, x="<<x[i-1]<<".."<<x[i]<<"):"<<endl<<endl;
  122. off<<"display([ p1, ";
  123. for(i=1;i<=n;i++)
  124. off<<"s"<<i<<", ";
  125. off<<"]);";
  126.  
  127. off.close();
  128.  
  129.  
  130.  
  131. return 0;
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement