Plik:Van der Pol oscillator solution and phase space generated with GNU Scientific Library.png

Rozmiar pierwotny(1024 × 1024 pikseli, rozmiar pliku: 23 KB, typ MIME: image/png)

Ten plik znajduje się w Wikimedia Commons i może być używany w innych projektach. Poniżej znajdują się informacje ze strony opisu tego pliku.

Opis

Opis
English: Solve an ordinary differential equation and output the solution to stdout:
   ./ode.out

Plot with gnuplot:

   ./ode.out > ode.dat
   ./ode.gnuplot
   xdg-open ode.png

To also plot the phase space on a plplot output window use an argument:

   ./ode.out 1

An ODE of any order can be reduced to a system of first order ODEs: therefore, GSL only offers an API to solve systems of first order ODEs.

In this particular example we, solve the second order autonomous ODE equation:

   g : R -> R
   g(t) = k * g'(t) * (1 - g(t)^2) - g(t) = f(g(t), g'(t), t)

which is equivalent to solving the first order system with two functions y_0 and y_1:

   y_0'(t) = y_1(t)
   y_1'(t) = y_1(t) * (1 - y_0(t)^2) - y_0(t)

with:

   y_0(t) = g(t)
   y_1(t) = g'(t)
This function was chosen because it quickly converges to a limit cycle, which is fun to watch. Generated with: https://github.com/cirosantilli/cpp-cheat/blob/890c065b15a350b61b0780f5e47721b05df1f2f6/gsl/ode.md
Data
Źródło Praca własna
Autor Cirosantilli2

Licencja

Ja, właściciel praw autorskich do tego dzieła, udostępniam je na poniższej licencji
w:pl:Licencje Creative Commons
uznanie autorstwa na tych samych warunkach
Wolno:
  • dzielić się – kopiować, rozpowszechniać, odtwarzać i wykonywać utwór
  • modyfikować – tworzyć utwory zależne
Na następujących warunkach:
  • uznanie autorstwa – musisz określić autorstwo utworu, podać link do licencji, a także wskazać czy utwór został zmieniony. Możesz to zrobić w każdy rozsądny sposób, o ile nie będzie to sugerować, że licencjodawca popiera Ciebie lub Twoje użycie utworu.
  • na tych samych warunkach – Jeśli zmienia się lub przekształca niniejszy utwór, lub tworzy inny na jego podstawie, można rozpowszechniać powstały w ten sposób nowy utwór tylko na podstawie tej samej lub podobnej licencji.

C src code

ode.c using GNU GSL and PLplot libraries

/*

gcc o.c -Wall -Wextra -lm -lplplot -lgsl -lgslcblas


*/

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <gsl/gsl_errno.h>
#include <gsl/gsl_matrix.h>
#include <gsl/gsl_odeiv2.h>
#include <gsl/gsl_sf_bessel.h>
#include <gsl/gsl_statistics.h>
#include <plplot/plplot.h>






int ode_func(double t, const double y[], double f[], void *params) {
    double k = *((double*)params);
    (void)t;
    f[0] = y[1];
    f[1] = k * y[1] * (1.0 - y[0] * y[0]) - y[0];
    return GSL_SUCCESS;
}

/* Some methods ask for you to give the Jacobian of it too
 * for better performance. You have to calculate this by hand.
 */
int ode_jac(double t, const double y[], double *dfdy, double dfdt[], void *params) {
    double k = *((double*)params);
    (void)t;
    gsl_matrix_view dfdy_mat = gsl_matrix_view_array(dfdy, 2, 2);
    gsl_matrix *m = &dfdy_mat.matrix;
    gsl_matrix_set(m, 0, 0, 0.0);
    gsl_matrix_set(m, 0, 1, 1.0);
    gsl_matrix_set(m, 1, 0, -(1.0 + 2.0 * k * y[0] * y[1]));
    gsl_matrix_set(m, 1, 1, k * (1.0 * y[0] * y[0]));
    /* Autonomous. */
    dfdt[0] = 0.0;
    dfdt[1] = 0.0;
    return GSL_SUCCESS;
}

int main(int argc, char **argv) {
    enum Constexpr {n_points = 1000};
    double mu = 1.0;
    gsl_odeiv2_system sys = {ode_func, ode_jac, 2, &mu};
    gsl_odeiv2_driver * d= gsl_odeiv2_driver_alloc_y_new(
        &sys,
        gsl_odeiv2_step_rk8pd,
        1e-6,
        1e-6,
        0.0
    );
    int i;
    double t = 0.0;
    double t1 = 100.0;
    /* Initial condition: f = 0 with speed 0. */
    double y[2] = {1.0, 0.0};
    double dt = t1 / n_points;
    double datax[n_points];
    double datay[n_points];

    for (i = 0; i < n_points; i++) {
        double ti = i * dt;
        int status = gsl_odeiv2_driver_apply(d, &t, ti, y);
        if (status != GSL_SUCCESS) {
            fprintf(stderr, "error, return value=%d\n", status);
            break;
        }

        /* Get output. */
        printf("%.5e %.5e %.5e\n", t, y[0], y[1]);
        datax[i] = y[0];
        datay[i] = y[1];
    }

    /* Cleanup. */
    gsl_odeiv2_driver_free(d);

    /* Plot. */
    if (argc > 1 && argv[1][0] == '1') {
        plsdev("xwin");
        plinit();
        plenv(
            gsl_stats_min(datax, 1, n_points),
            gsl_stats_max(datax, 1, n_points),
            gsl_stats_min(datay, 1, n_points),
            gsl_stats_max(datay, 1, n_points),
            0,
            1
        );
        plstring(n_points, datax, datay, "*");
        plend();
    }

    return EXIT_SUCCESS;
}

Gnuplot code: ode.gnuplot

#!/usr/bin/env gnuplot
set terminal png size 1024, 1024
set output "ode.png"
set multiplot layout 3,1 title "ODE solution"

set title "Solution: t x g(t) "
set xlabel "t"
set ylabel "g(t)"
plot "ode.dat" using 1:2 notitle

set title "Derivative plot: t x g'(t)"
set xlabel "t"
set ylabel "g'(t)"
plot "ode.dat" using 1:3 notitle

set title "Phase space: g(t) x g(t)"
set xlabel "g'(t)"
set ylabel "g(t)"
plot "ode.dat" using 2:3 notitle


./ode.out > ode.dat
./ode.gnuplot
xdg-open ode.png

Podpisy

Dodaj jednolinijkowe objaśnienie tego, co ten plik pokazuje

Obiekty przedstawione na tym zdjęciu

przedstawia

Historia pliku

Kliknij na datę/czas, aby zobaczyć, jak plik wyglądał w tym czasie.

Data i czasMiniaturaWymiaryUżytkownikOpis
aktualny10:48, 21 sie 2019Miniatura wersji z 10:48, 21 sie 20191024 × 1024 (23 KB)Cirosantilli2User created page with UploadWizard

Następujące strony korzystają z tego pliku:

Metadane