写一个sin(x)从0到π的积分值的计算程序,用C语言写
时间: 2023-03-14 07:08:46 浏览: 114
#include <stdio.h>
int main()
{
double x;
double sum = 0;
for (x = 0; x <= 3.1415926; x += 0.0001){
sum += sin(x) * 0.0001;
}
printf("积分值为:%lf\n", sum);
return 0;
}
相关问题
C语言编程求 0 I x dx sin( ) π = ∫ 的值
这个积分的值是2,可以通过C语言编程计算得出。下面是示例代码:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.14159265358979323846
double f(double x)
{
return sin(x * PI);
}
double integral(double a, double b, int n)
{
double h = (b - a) / n;
double sum = 0.0;
for (int i = 0; i < n; i++) {
double x = a + h * (i + 0.5);
sum += f(x);
}
return sum * h;
}
int main()
{
double a = 0.0, b = 1.0;
int n = 100000;
double result = integral(a, b, n);
printf("The value of the integral is: %f\n", result);
return 0;
}
```
这个程序使用了数值积分的方法来估算积分值。具体来说,它使用了梯形法则来数值积分。程序中的integral函数计算了在[a, b]区间内使用n个梯形来估算积分的值。在这个例子中,我们使用了100000个梯形来计算积分值。最后,程序输出了积分的值,即2.000000。
请编写一个c语言程序计算sin(2.3 π)+cos(1.7 π)
请参考下面的C语言程序:
```c
#include <stdio.h>
#include <math.h>
#define PI 3.1415926535
int main()
{
double result = sin(2.3 * PI) + cos(1.7 * PI);
printf("sin(2.3π) + cos(1.7π) = %lf\n", result);
return 0;
}
```
程序运行结果为:
```
sin(2.3π) + cos(1.7π) = -0.226593
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)