CSharp 中点欧拉法 源代码
时间: 2023-09-30 08:11:42 浏览: 51
以下是C#中点欧拉法的源代码示例:
```csharp
using System;
namespace EulerMethod
{
class Program
{
static double Function(double x, double y)
{
return x + y; // Example function: dy/dx = x + y
}
static void Main(string[] args)
{
double x0 = 0, y0 = 1, h = 0.1, xn = 1;
int n = (int)((xn - x0) / h); // Calculate number of iterations
for (int i = 0; i < n; i++)
{
double x = x0 + i * h;
double y = y0 + h * Function(x, y0 + (h / 2) * Function(x, y0));
y0 = y; // Update y0 for next iteration
Console.WriteLine("x = {0:F2} y = {1:F4}", x, y);
}
}
}
}
```