C语言编制常微分方程初值问题的欧拉法、改进的欧拉法和经典的R—K法的程序;
时间: 2023-12-20 20:05:00 浏览: 101
欧拉法,改进欧拉法,R-K法解微积分方程
欧拉法:
```
#include <stdio.h>
double f(double t, double y);
int main()
{
double h = 0.1; // 步长
double t = 0; // 初始时间
double y = 1; // 初始值
double tf = 1; // 终止时间
printf("t = %f, y = %f\n", t, y);
while (t < tf)
{
y = y + h * f(t, y);
t = t + h;
printf("t = %f, y = %f\n", t, y);
}
return 0;
}
double f(double t, double y)
{
return y - t * t + 1;
}
```
改进的欧拉法:
```
#include <stdio.h>
double f(double t, double y);
int main()
{
double h = 0.1; // 步长
double t = 0; // 初始时间
double y = 1; // 初始值
double tf = 1; // 终止时间
printf("t = %f, y = %f\n", t, y);
while (t < tf)
{
double k1 = f(t, y);
double k2 = f(t + h, y + h * k1);
y = y + h * (k1 + k2) / 2;
t = t + h;
printf("t = %f, y = %f\n", t, y);
}
return 0;
}
double f(double t, double y)
{
return y - t * t + 1;
}
```
经典的R-K法:
```
#include <stdio.h>
double f(double t, double y);
int main()
{
double h = 0.1; // 步长
double t = 0; // 初始时间
double y = 1; // 初始值
double tf = 1; // 终止时间
printf("t = %f, y = %f\n", t, y);
while (t < tf)
{
double k1 = f(t, y);
double k2 = f(t + h / 2, y + h * k1 / 2);
double k3 = f(t + h / 2, y + h * k2 / 2);
double k4 = f(t + h, y + h * k3);
y = y + h * (k1 + 2 * k2 + 2 * k3 + k4) / 6;
t = t + h;
printf("t = %f, y = %f\n", t, y);
}
return 0;
}
double f(double t, double y)
{
return y - t * t + 1;
}
```
阅读全文