Advertisement
Guest User

Untitled

a guest
Feb 19th, 2018
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. # include <stdio.h>
  2. # include <stdlib.h>
  3. # include <math.h>
  4.  
  5. double function(double x)
  6. {
  7. x = sin(x);
  8. return x;
  9. }
  10.  
  11. double U(double x)
  12. {
  13. double pi=4*atan(1.);
  14. x = -sin(x)+(x/pi);
  15. return x;
  16. }
  17.  
  18. int main (void)
  19. {
  20. int i,j, n = 1000;
  21. double x = 0.,pi=4*atan(1.), h;
  22. h=pi/n;
  23.  
  24. double * v;
  25. double *chis;
  26. double * koef;
  27.  
  28. FILE *A;
  29. A = fopen("out.txt","w");
  30.  
  31. v = (double *)malloc((n+1)*sizeof(double));
  32. chis = (double *)malloc(n*sizeof(double));
  33. koef = (double *)malloc(n*n*sizeof(double));
  34. for(i=0;i<(n*n);i++)
  35. {
  36. koef[i]=0;
  37. }
  38.  
  39. chis[0]=U(x);
  40. chis[n-1]=U(pi);
  41. for(i=1;i<=(n-1);i++)
  42. {
  43. koef[i]=0.;
  44. }
  45. koef[0]=1;
  46. for(i=0;i<(n-1);i++)
  47. {
  48. koef[n*n + i -1 ]=0;
  49. }
  50. koef[n*n -1]=1;
  51. x=h;
  52.  
  53. for (i=1;i<(n-1);i++)
  54. {
  55. koef[i*n + i -1] = 1;
  56. koef[i*n + i] = -2;
  57. koef[i*n + i + 1] =1;
  58. chis[i] = function(x)*h*h;
  59. x += h;
  60.  
  61. }
  62.  
  63. chis[1]=(chis[1] - chis[0]*koef[n])/(-2);
  64. koef[n]=0;
  65. koef[n+1] = koef[n+1]*(-0.5);
  66. koef[n+2] = koef[n+2]*(-0.5);
  67. for(i=2;i<(n-1);i++)
  68. {
  69. chis[i]=chis[i]-chis[i-1];
  70. koef[i*n+i-1]-=koef[(i-1)*n+i-1];
  71. koef[i*n+i]-=koef[(i-1)*n+i];
  72. koef[i*n+i+1]-=koef[(i-1)*n+i+1];
  73. chis[i]=chis[i]/koef[i*n+i];
  74. koef[i*n+i+1]=koef[i*n+i+1]/(koef[i*n+i]);
  75. koef[i*n+i]=1;
  76.  
  77. }
  78.  
  79. v[n]=chis[n];
  80. v[0]=chis[0];
  81.  
  82. v[n-1]=chis[n-1]-koef[n*n-1-n]*v[n];
  83. for(i=n-2;i>=1;i--)
  84. {
  85. v[i]=chis[i]-(v[i+1]*koef[i*n+i+1]);
  86. }
  87.  
  88.  
  89. x=0.;
  90. for(i=0;i<=n;i++)
  91. {
  92. fprintf(A,"%lf %lf %lf\n",x,v[i],U(x));
  93. x=x+h;
  94. }
  95. return 0;
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement