曲线下该区域的C 程序.(上限)
C ++ program for the area under a curve. (the upper limit)?
这是我对上限的所拥有的。我对n的价值是5。但是我想我在某个地方搞砸了,因为当我手工计算出它的价值不是相同的值时。有人知道我的程序怎么了?
#include<iostream>
#include<stdlib.h>
#include<math.h>
#include<iomanip>
#include<fstream>
using namespace std;
double f(double x)
{
return (0.6*pow(x,2)) +4;
}
int main ( )
{
double a, b, delx, x, area;
int n;
cout << "Welcome. To begin, please enter a value for 'a' followed by a value for 'b'. n";
cin>>a>>b;
cout << "You have entered a = " << a<<" & b = " <<b <<". Now, input an n value n";
cin >>n;
delx = (b-a)/n;
area = 0;
for (int i=0;i<=n;i++)
{
area = area + f(a+i*delx)*delx;
}
cout << "The area under the curve of f(x) = 0.6x^2 + 4 is ";
cout << setprecision(5) << area;
system ("pause");
}
int main()
{
double a=0, b=5; //a: lower limit, b: upper limit of integration
int n=20; //the number of intervals
double dx = (b-a)/n; //step size
double area = 0; //our goal to find
for (int i=0;i<n;i++)
{
//using trapezoidal method
//the area of a trapezoid is (lower base + upper base)*height/2
//f(a+i*dx): upper base, f(a+(i+1)*dx): lower base
//dx: height of the trapezoid
area = area + (f(a+i*dx)+f(a+(i+1)*dx))*dx/2.0;
}
std::cout<<area;
}
尽管您的方法不是不正确(但不是理想的)梯形方法会更快地收敛。在您的方法中,您可以将N(间隔数)设置为比梯形更高的数字,以获得相似的准确度。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- C++,系统无法执行指定的程序
- 在C++程序中输入的文本文件将不起作用,除非文本被复制和粘贴
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- C++ Windows 驱动程序MSB3030无法复制该文件,因为它找不到
- 重载操作程序时出错>>用于类中的字符串 memebr
- 获取日期异步信号安全吗?如果在信号处理程序中使用,它会导致死锁吗
- 试图在visual studio上用C++创建一个桌面应用程序
- 模板元程序查找相似的连续类型名称
- FFmpeg:制作一个应用程序比直接使用ffmepg更好吗
- 如何通过cpp程序运行shell脚本
- 有没有什么方法可以使用一个函数中定义的常量变量,也可以由c++中同一程序中的其他函数使用
- IPC使用多个管道和分支进程来运行Python程序
- 如何将c++程序的一些输出传递给shell,以便在shell中使用
- 使用C++程序合并排序没有得到正确的输出
- 基于boost的程序的静态链接——zlib问题
- 程序崩溃并显示"std::out_of_range"错误
- 曲线下该区域的C 程序.(上限)
- 一个简短的程序,该程序采用输入下部的案例字母,它给出了上案字母的上限字母并在按下Enter键时退出
- 我如何在我的OpenGL应用程序中实现准确(但可变)的FPS限制/上限?