Advertisement
tampurus

Unit 2.5 Non linear curve fitting y = a * e ^(b*x)

Jun 7th, 2022 (edited)
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.47 KB | None | 0 0
  1. c// algo y = a * e ^(b*x)
  2.  
  3. start
  4. Read number of data(n)
  5. for i=1 to n:
  6.     1. read xi and yi
  7.     2. Next I
  8. Initializ
  9.     1. sumx=0
  10.     2. sumx2 = 0
  11.     3. sumy = 0
  12.     4. sum xy =0
  13.    
  14. calculate required sum
  15. for i =1 to n:
  16.     1. sumx= sumx + (xi)
  17.     2. sumx2 = sumx2 + (xi)*(xi)
  18.     3. sumy = sumy + log(yi)
  19.     4. sumxy =sumxy + (xi) * log(yi)
  20.  
  21. Next i
  22. calculate required constant A and B of Y = A + Bx
  23. A = (sumx * sumy - n*sumxy)/(sumx*sumx - n*sumx2)
  24. B = (sumy - n*A)/sumx
  25.  
  26. a = pow(10,A)
  27. b = B/log10(e)
  28. display the value of and b
  29. stop
  30.  
  31.  
  32. // program
  33. #include <stdio.h>
  34. #include<math.h>
  35. #define e 2.718
  36. int main()
  37. {
  38.     int n;
  39.     float a,x[10],y[10],sumx=0,sumy=0,sumx2=0,sumxy=0;
  40.     printf("Enter the value of n\n");
  41.     scanf("%d",&n);
  42.     printf("Enter the value of x and y\n");
  43.     for(int i=1 ; i<=n ; i++){
  44.         scanf("%f",&x[i]);
  45.     }
  46.     for(int i=1 ; i<=n ; i++){
  47.         scanf("%f",&y[i]);
  48.     }
  49.     for(int i=1 ; i<=n ; i++){
  50.         sumx = sumx + x[i];
  51.         sumx2 = sumx2 + ((x[i])*(x[i]));
  52.         sumy = sumy + log(y[i]);
  53.         sumxy = sumxy + ((x[i]) * log(y[i]));
  54.     }
  55.    
  56.     float A = ((sumx * sumy) - (n*sumxy))/((sumx*sumx) - (n*sumx2));
  57.     float B =  (sumy - (n*A))/sumx;
  58.    
  59.     a = pow(10,A);
  60.     float b = B/log10(e);
  61.     printf("\n y= %.2fx + %.2f",a,b);
  62.     return 0;
  63. }
  64.  
  65.  
  66. /*
  67. Output
  68. Enter the value of n
  69. 5
  70. Enter the value of x and y
  71. 1
  72. 5
  73. 7
  74. 9
  75. 12
  76. 10
  77. 15
  78. 12
  79. 15
  80. 21
  81.  
  82.  y= 1.15x + 0.88
  83. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement