2018年12月31日 星期一

利用辛普森 理則 (Simpson's Rule) 計算 x=0 to 2 , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積

利用辛普森 理則 (Simpson's Rule) 計算

x=0 to 2  , 以曲線 y= (1+x^3) ^(1/3) 沿 z 軸旋轉一周的體積







/* ex4-5.c based on Simpson's Rule to compute
 * definite integral with domain [a,b] and
 * n even-grid. n must be even.
 */
#include <stdio.h>
#include <math.h>
#define  F(x)    pow(pow((1+pow(x,3)),1.0/3),2)
#define PI   3.14159
void main()
{
   int i,m,n;
   double a,b,h,x,sum1=0.0,sum2=0.0,sn;
   scanf("a=%lf  b=%lf  n=%d",&a,&b,&n);
   m=n/2;
   h=(b-a)/n;
   for(i=1;i<=2*m-1;i++)
   {
      x=a+i*h;
      if(i%2==0)
sum2=sum2+F(x);
      else
sum1=sum1+F(x);
   }
   sn=PI*(h/3.0)*(F(a)+F(b)+2.0*sum2+4.0*sum1);
   printf("S%d=%lf\n",n,sn);
   return;
}

輸入資料
a=0  b=2  n=10

輸出資料
S10=12.325068



沒有留言:

張貼留言

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

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