Advertisement
Guest User

Untitled

a guest
Apr 17th, 2014
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.23 KB | None | 0 0
  1.  
  2.  
  3. #include <iostream>
  4. #include <math.h>
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7.  
  8. using namespace std;
  9.  
  10. // Erstellt einen Pointer einen double Wert!
  11. typedef double (*func)(double);
  12.  
  13. // Die verwendete Funktion
  14. double f(double x){
  15. return sin(x);
  16. }
  17. double g(double x)
  18. {
  19. return x*x;
  20. }
  21.  
  22.  
  23. double trapez (double a, double b,int n,int antin,func f)
  24. {
  25. double ergebnis=0;
  26. double h;
  27. double ueberpruefer;
  28. double antih;
  29.  
  30.  
  31. h = (b-a)/n;
  32.  
  33. for (int i = 1; i <=n; i++) {
  34. ergebnis += f(a+h*(2*i-1)/2);
  35. }
  36. ergebnis*=h;
  37.  
  38. // Anti Ergebniss
  39. antih= (b-a)/antin;
  40.  
  41. for (int x = 1; x <=antin; x++) {
  42. ueberpruefer+= f(a+antih*(2*x-1)/2);
  43. }
  44. ueberpruefer*=antih;
  45.  
  46. if (fabs((ueberpruefer-ergebnis)/ergebnis)>=0.000001) {
  47.  
  48. n=n*2;
  49. antin=antin*2;
  50. cout << antin << endl;
  51.  
  52.  
  53.  
  54. ueberpruefer= trapez(a,b,n,antin,f);
  55.  
  56. }
  57.  
  58. return ueberpruefer;
  59. }
  60.  
  61. double integral (double a, double b, int n,func f)
  62. {
  63.  
  64.  
  65. double h= (b-a)/n;
  66.  
  67. double x;
  68. double qf;
  69.  
  70. double sum;
  71.  
  72. qf=f(a)+f(b);
  73.  
  74. sum=0.;
  75. for (int k =1; k <=n-1; k++) {
  76. x=a+k*h;
  77. sum+=f(x);
  78.  
  79. }
  80.  
  81. qf+=2*sum;
  82.  
  83. sum=0;
  84.  
  85. for (int k=1; k<=n; k++) {
  86.  
  87. x=((a+(k-1)*h)+(a+k*h))/2;
  88.  
  89. sum+=f(x);
  90.  
  91. }
  92. qf+=4*sum;
  93.  
  94. return qf*h/6;
  95.  
  96. }
  97.  
  98. double* faltungsintegral(double a,double b, int n,func f,func g){
  99. double ergebniss=0;
  100. double qf=0;
  101. double h;
  102. int k,i=0;
  103. double *ary;
  104. ary = (double*)malloc(10000*sizeof(double));
  105. h=(b-a)/n;
  106.  
  107. for(double t = a; t<=b; t=t+h){
  108.  
  109. qf=(f(a)*g(a-t))+(f(b)*g(b-t));
  110. for(k=1;k<n;k++){
  111. qf+=2*((f(a+(h*k)))*(g(a+(h*k)-t)));
  112. }
  113. //cout << qf<< endl;
  114. for(k=1;k<=n;k++){
  115.  
  116. qf+=4*((f(((a+h*k)+(a+(h*(k-1))))/2))*(g(-t+((a+h*k)+(a+(h*(k-1))))/2)));
  117.  
  118. }
  119. //cout << qf << endl;
  120.  
  121. ergebniss = (qf * h)/6;
  122. ary[i]=ergebniss;
  123. i++;
  124. }
  125. // for (int tau = 0; tau<n;tau++){
  126. // printf("%lf\n", ary[tau]);
  127. // }
  128.  
  129. return ary;
  130. }
  131.  
  132.  
  133.  
  134. int main()
  135. {
  136. double* d ;
  137. double a=0;
  138. double b=2;
  139. double n=20;
  140.  
  141. // double h = (b-a)/n;
  142. /*
  143. for(double t = a; t<=b;t=t+h){
  144. arrays[arraystelle]=faltungsintegral(a,b,n,arraystelle,t,&g,&g);
  145. arraystelle++;
  146. }
  147. cout << arrays[0] << endl;
  148. */
  149.  
  150. d=faltungsintegral(a,b,n,&f,&g) ;
  151. for (int tau = 0; tau<=n;tau++){
  152. cout << d[tau] << endl;
  153. }
  154.  
  155. // for (int x=0; x<=10;x++){
  156. // cout << d << endl;
  157. //}
  158.  
  159. free(d);
  160. return 0;
  161. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement