程序应首先从键盘读取 a 和 b 的值,然后使用 Simpson 的 1/3 规则估计区间 [a, b] 中 f(x) 下的面积,并显示估计面积和估计误差。其中 f(x)=sqrt(x+1)+0.25,我在数据数组和这个函数中有语法错误:
float f(float data[])
{
return(sqrt(data[]+1)+0.25);
}
我的其余代码是:
#include<iostream>
#include<cmath>
using namespace std;
//prototype function
float GenerateData(float a ,float b , int n, float data[]);
float f(float data[]);
float Simpson(float a, float b, int n,float data[],float I, float J,float simpson);
float g(float data[]);
//area function
float f(float data[])
{
return(sqrt(data[]+1)+0.25);
}
// function after ingreation
//float g(float data[])
//{
// return((2/3)*sqrt(pow(data[]+1),3)+(0.25*data[])+n);
//}
int main()
{
//decleration
float a,b,simpson;
float data[100], I =0, J=0 ;
int n;
//input a b and interval
cout<<"given f(x)=sqrt(x+1)+0.25 "<<endl;
if (a>b)
{
cout<<"error";
}
cout<<"please enter lower limit ";
cin>>a;
cout<<"please enter upper limit ";
cin>>b;
cout<<"please enter the number of intervals ,even number";
cin>>n;
if ( n%2!=0)
{
cout<<"error";
}
//output the estimating area
cout<< "the estimating area using simpson's rule "<<Simpson( a, b, n, data[], I, J,simpson);
//output the exact area
//cout<<"the exact area "<<
//output the error
//cout<<"The Total Error is : "<<endl;
return 0;
}
//function generate data
float GenerateData(float a ,float b , int n,float data[])
{
float xi;
//using loop
for(int i=0;i<n+1;i++)
{
data[i]=xi=a+i*(b-a/n);
}
return data[];
}
// function simpson's rule
float Simpson(float a, float b, int n,float data[],float I, float J,float simpson)
{
float A;
//loop
for(int i=1;i<n;i++)
{
if (i%2!=0)
{
I=I+f(data[i]);
}
}
//loop
for(int i=2;i<n-1;i++)
{
if (i%2==0)
{
J=J+f(data[]);
}
}
simpson=(b-a/n*3)*(f(a)+(4*I)+(2*J)+f(b));
cout<<"The Value of integral under the enterd limits is by using simpson's rule: "<<endl;
cout<<simpson<<endl;
return A;
}