Charles_Su 2016-06-29 10:15 采纳率: 21.4%
浏览 1565

c#利用复合梯形公式求积分,为什么计算结果是0

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
int a, b, select;
double result,step;
Console.WriteLine("请选择要积分的函数,1:f(x)=1+x^2 2:f(x)=log10(x) 3:1/(1+x^2)");
select = Console.Read();
Console.WriteLine("请输入积分上下限及步长");
a = Console.Read();
b = Console.Read();
step = Console.Read();
Integration func = new Integration();
FunctionDelegate f1 = new FunctionDelegate(func.fun_1);
FunctionDelegate f2 = new FunctionDelegate(func.fun_2);
FunctionDelegate f3 = new FunctionDelegate(func.fun_3);
if (select == 1)
{
result = func.Integral(a, b,step, f1);
}
else if (select == 2)
{
result = func.Integral(a, b, step, f2);
}
else
{
result = func.Integral(a, b,step, f3);
}
Console.WriteLine("The result is {0}", result);
System.Threading.Thread.Sleep(1000 * 1000);
}
//定义委托
public delegate double FunctionDelegate(double x);
public partial class Integration
{
public double fun_1(double x)
{ return 1 + x * x; }
public double fun_2(double x)
{ return Math.Log10(x); }
public double fun_3(double x)
{ return 1 / (1 + x * x); }
public double Integral(double a, double b,double s, FunctionDelegate f)
{//梯形法求积分的值
double sum = 0;
for(double i=a; i<=b; i+=s)
{
sum += f(i);
}
sum *= s;
return sum;
}

    }
}

}

图片说明

  • 写回答

2条回答 默认 最新

  • threenewbee 2016-06-29 10:30
    关注

    步长太大了,换成0.001看看

    评论

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退