问题描述
我访问了gnu gsl网站,但在那里找不到用于求解微分方程的例子,这一点都非常直观(特别是因为它使用的是二阶微分方程). https://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html#ODE-Example-programs 有人可以指导您在哪里找到描述性指南,以解决一个非常简单的一阶微分方程.
I visited the gnu gsl website and i dont find the example there to solve a differential equation to be intuitive at all (especially because it is using 2nd order differential equation). https://www.gnu.org/software/gsl/manual/html_node/ODE-Example-programs.html#ODE-Example-programsCan somebody guide about where to find a descriptive guide to how solve a very simple first order differetial equation.
例如,假设我的函数是y'= x + 2y(或任何此类函数),那么我该如何在gsl中编写代码以在给定的固定步长和初始条件下解决该问题.
For example, supoose my function is y'=x+2y (or any such function) then how do i write code in gsl to solve it with a given fixed step size and initial condition.
推荐答案
对于 y'= f(x,y)= x + 2y
,数组的维数均为1,通常需要避免,但这是指导性的.对于显式求解器,即名称中不包含 imp
的求解器,不需要雅可比行列式:
For y'=f(x,y)=x+2y
the arrays have all dimension 1, which normally is something to avoid, but here it is instructional. For the explicit solvers, i.e., those not containing imp
in the name, you do not need the Jacobian:
#include <stdio.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>
int odefunc (double x, const double y[], double f[], void *params)
{
f[0] = x+2*y[0];
return GSL_SUCCESS;
}
int * jac;
int main ()
{
int dim = 1;
gsl_odeiv2_system sys = {odefunc, NULL, dim, NULL};
gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (&sys, gsl_odeiv2_step_rkf45, 1e-6, 1e-6, 0.0);
int i;
double x0 = 0.0, xf = 100.0; /* start and end of integration interval */
double x = x0;
double y[1] = { 1.0 }; /* initial value */
for (i = 1; i <= 100; i++)
{
double xi = x0 + i * (xf-x0) / 100.0;
int status = gsl_odeiv2_driver_apply (d, &x, xi, y);
if (status != GSL_SUCCESS)
{
printf ("error, return value=%d\n", status);
break;
}
printf ("%.8e %.8e\n", x, y[0]);
}
gsl_odeiv2_driver_free (d);
return 0;
}
您可能需要查阅Jose M. Garrido的著作使用C和开源工具进行计算建模简介" .
You may want to look up the book "Introduction to Computational Modeling Using C and Open-Source Tools" by Jose M. Garrido.
这篇关于gsl gnu求解一阶常微分方程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!