C#-委任_積分汎用方法を求める
7163 ワード
コード:
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6
7 namespace _
8 {
9 static class Program
10 {
11 // (Double )
12 delegate double Integand(double x);
13
14 //
15 static double Method1(double x)
16 {
17 return 2 * x + 1;
18 }
19
20 static double Method2(double x)
21 {
22 return x * x;
23 }
24
25 //
26 static double DefiniteIntegrate(Integand f, double a, double b)
27 {
28 const int sect = 1000;
29
30 double area = 0;
31
32 double delta = (b - a) / sect;
33
34 for (int i = 0; i < sect; i++)
35 {
36 // f Method1 Method2。
37 // double , double 。
38 // double “a + i * delta”
39 area += delta * f(a + i * delta);
40 }
41
42 return area;
43 }
44
45 static void Main(string[] args)
46 {
47 //
48 //
49 Console.WriteLine(Program.DefiniteIntegrate(Method1, 1, 5));
50 Console.WriteLine();
51 Console.WriteLine(Program.DefiniteIntegrate(Method2, 0, 1));
52
53 Console.ReadKey();
54 }
55 }
56 }
57