2019年3月17日 星期日

C語言例題2-2 子彈垂直向上射擊實驗, 子彈的終端速度(二分法bisection method)

C語言例題2-2 子彈垂直向上射擊實驗, 子彈的終端速度(二分法bisection method)

子彈垂直向上射擊實驗, 子彈的終端速度為
 f(x) = 1.15*10^-5*X^2 + 1.4*10^-5*X -1.962*10^-2
請使用 二分法 計算v的值 誤差=0.001


程式
// C program for implementation of Bisection Method for 
// solving equations 
#include <stdio.h>
#define EPSILON 0.001 
  
// An example function whose solution is determined using 
// Bisection Method. The function is x^3 - 4x  + 2 
double func(double x) 
{ 
    return 0.0000115*x*x +0.000014* pow(x,1.5) -0.01962; 
} 
  
// Prints root of func(x) with error of EPSILON 
void bisection(double a, double b) 
{ 
    if (func(a) * func(b) >= 0) 
    { 
        printf("You have not assumed right a and b\n"); 
        return; 
    } 
  
    double c = a; 
    while ((b-a) >= EPSILON) 
    { 
        // Find middle point 
        c = (a+b)/2; 
  
        // Check if middle point is root 
        if (func(c) == 0.0) 
            break; 
  
        // Decide the side to repeat the steps 
        else if (func(c)*func(a) < 0) 
            b = c; 
        else
            a = c; 
    } 
    printf("The value of root is :%.6lf\n" ,c); 
} 
  
// Driver program to test above function 
int main() 
{ 
    // Initial values assumed 
    double a =0.0, b =50.0 , x=a;
    while ((b-x) >= -1.0)
    {
        printf("%0.1lf <---> %0.4lf\n",x,func(x));
        x=x+1;
    }
    
    a =37.0;
    b =38.0 ;
    printf("\n%0.1lf to %0.1lf , the roots --> \n",a,b);
    bisection(a, b); 
    
    return 0; 
} 
輸出畫面

0.0 <---> -0.0196                                                                                                                                                
1.0 <---> -0.0196                                                                                                                                                
2.0 <---> -0.0195                                                                                                                                                
3.0 <---> -0.0194                                                                                                                                                
4.0 <---> -0.0193                                                                                                                                                
5.0 <---> -0.0192                                                                                                                                                
6.0 <---> -0.0190                                                                                                                                                
7.0 <---> -0.0188                                                                                                                                                
8.0 <---> -0.0186                                                                                                                                                
9.0 <---> -0.0183                                                                                                                                                
10.0 <---> -0.0180                                                                                                                                               
11.0 <---> -0.0177                                                                                                                                               
12.0 <---> -0.0174                                                                                                                                               
13.0 <---> -0.0170                                                                                                                                               
14.0 <---> -0.0166                                                                                                                                               
15.0 <---> -0.0162                                                                                                                                               
16.0 <---> -0.0158                                                                                                                                               
17.0 <---> -0.0153                                                                                                                                               
18.0 <---> -0.0148                                                                                                                                               
19.0 <---> -0.0143                                                                                                                                               
20.0 <---> -0.0138                                                                                                                                               
21.0 <---> -0.0132                                                                                                                                               
22.0 <---> -0.0126                                                                                                                                               
23.0 <---> -0.0120                                                                                                                                               
24.0 <---> -0.0113                                                                                                                                               
25.0 <---> -0.0107                                                                                                                                               
26.0 <---> -0.0100                                                                                                                                               
27.0 <---> -0.0093                                                                                                                                               
28.0 <---> -0.0085                                                                                                                                               
29.0 <---> -0.0078                                                                                                                                               
30.0 <---> -0.0070     
31.0 <---> -0.0062                                                                                                                                               
32.0 <---> -0.0053                                                                                                                                               
33.0 <---> -0.0044                                                                                                                                               
34.0 <---> -0.0036                                                                                                                                               
35.0 <---> -0.0026                                                                                                                                               
36.0 <---> -0.0017                                                                                                                                               
37.0 <---> -0.0007                                                                                                                                               
38.0 <---> 0.0003                                                                                                                                                
39.0 <---> 0.0013                                                                                                                                                
40.0 <---> 0.0023                                                                                                                                                
41.0 <---> 0.0034                                                                                                                                                
42.0 <---> 0.0045                                                                                                                                                
43.0 <---> 0.0056                                                                                                                                                
44.0 <---> 0.0067                                                                                                                                                
45.0 <---> 0.0079                                                                                                                                                
46.0 <---> 0.0091                                                                                                                                                
47.0 <---> 0.0103                                                                                                                                                
48.0 <---> 0.0115                                                                                                                                                
49.0 <---> 0.0128                                                                                                                                                
50.0 <---> 0.0141                                                                                                                                                
51.0 <---> 0.0154                                                                                                                                                
                                                                                                                                                                 
37.0 to 38.0 , the roots -->                                                                                                                                     
The value of root is :37.735352                                                                                                                                  
                                                                                                                                                                 
                                                                                                                                                                 
...Program finished with exit code 0                                                                                                                             
Press ENTER to exit console.     

沒有留言:

張貼留言

Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3

  Node-Red Dashboard UI Template + AngularJS 參考 AngularJS教學 --3 AngularJS 實例 <!DOCTYPE html> <html> <head> <meta charse...