Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.17 KB | None | 0 0
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <fstream>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. double f(double i){
  9. return i*sin(i*i);
  10. }
  11.  
  12. double bisect(double bis1 , double bis2, double dx){
  13. double bis3 = (bis1+bis2)/2;
  14. while ((fabs(bis1-bis2))>dx){
  15. if (f(bis1)*f(bis3)<0){
  16. bis2 = bis3;
  17. }
  18.  
  19. else if (f(bis1)*f(bis3)>0){
  20. bis1 = bis3;
  21. }
  22. bis3 = (bis1+bis2)/2;
  23. }
  24. return bis3 ;
  25. }
  26.  
  27. bool searcher(double down, double up, double a){
  28. return (f(down)-a)*(f(up)-a)<0 ;
  29. }
  30.  
  31. int main()
  32. {
  33. FILE *FL;
  34. FL = fopen("data.txt","w");
  35. double dx= 0.001;
  36. double err= 0.0001;
  37. double a = 0.001;
  38. double i=0;
  39. for (double a = 0.001 ; a<5 ; a+=dx){
  40. double i=0;
  41. int n = 0;
  42. while(true){
  43. if (searcher(i, i+dx, a)){
  44. if (n==0){
  45. fprintf(FL, "%f %f ", a, bisect(i, i+dx, err));
  46. n++;
  47. }
  48. else{
  49. fprintf(FL, "%f \n", bisect(i, i+dx, err));
  50. break;
  51. }
  52. }
  53. i+=dx;
  54. }
  55. }
  56. fclose(FL);
  57.  
  58. return 0;
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement