2019年1月1日 星期二

習題EX4-1 利用梯行法解

習題EX4-1 利用梯行法解

n=11  a=1  b=3.2
 x     y
======
1.0  4.9
1.2  5.4
1.4  5.8
1.6  6.2
1.8  6.7
2.0  7.0
2.2  7.3
2.4  7.5
2.6  8.0
2.8  8.2
3.0  8.3
3.2  8.3


/* p4-1.c based on Trapezoidol Rule is
 * used for computing definite integral with
 * domain [a,b] with n even-grids.
 */
#include <stdio.h>
#include <math.h>
#define PI   3.141593
void main()
{
   int n,i;
   double  a,b,x[50],y[50],tn,h ,sum=0.0;
   scanf("n=%d a=%lf b=%lf",&n,&a,&b);
   for(i=1;i<=n+1;i++)
      scanf("%lf %lf",&x[i],&y[i]);
     
   h=(b-a)/n;
   for(i=2;i<=n;i++)
   {
    sum=sum+y[i];
    tn=(h/2.0)*(y[1]+y[n+1]+2.0*sum);
    printf("T%d=%10.6lf\n",n,tn);
   }
 
   //tn=(h/2.0)*(y[1]+y[n+1]+2.0*sum);
   //printf("T%d=%10.6lf\n",n,tn);
   return;
}

T11=  2.400000
T11=  3.560000
T11=  4.800000
T11=  6.140000
T11=  7.540000
T11=  9.000000
T11= 10.500000
T11= 12.100000
T11= 13.740000
T11= 15.400000

沒有留言:

張貼留言

WOKWI LED + MQTT Node-Red SQLite

WOKWI LED + MQTT Node-Red SQLite const char *mqtt_broker = "broker.mqtt-dashboard.com" ; const char *topic1 = "alex9ufo/e...