11#include <assert.h>
22#include <math.h>
33#include <stdio.h>
4+ #include <stdlib.h>
45
56#include <gsl/gsl_errno.h>
67#include <gsl/gsl_matrix.h>
@@ -19,49 +20,46 @@ where
1920
2021$Y(t) = ( y(t), y'(t), y''(t), ... )$
2122*/
22- int ode_func ( double t , const double y [], double f [], void * params )
23- {
23+ int ode_func (double t , const double y [], double f [], void * params ) {
2424 double mu = * ((double * )params );
2525 f [0 ] = y [1 ];
2626 f [1 ] = - y [0 ] - mu * y [1 ]* (y [0 ]* y [0 ] - 1 );
2727 return GSL_SUCCESS ;
2828}
2929
3030/*
31- some methods ask for you to give the jacobian of f too
31+ Some methods ask for you to give the jacobian of it too
3232for better performance.
3333
34- you have to calculate this by hand.
34+ You have to calculate this by hand.
3535*/
36- int ode_jac ( double t , const double y [], double * dfdy , double dfdt [], void * params )
37- {
36+ int ode_jac (double t , const double y [], double * dfdy , double dfdt [], void * params ) {
3837 double mu = * ((double * )params );
3938 gsl_matrix_view dfdy_mat = gsl_matrix_view_array (dfdy , 2 , 2 );
40- gsl_matrix * m = & dfdy_mat .matrix ;
41- gsl_matrix_set (m , 0 , 0 , 0.0 );
42- gsl_matrix_set (m , 0 , 1 , 1.0 );
43- gsl_matrix_set (m , 1 , 0 , -2.0 * mu * y [0 ]* y [1 ] - 1.0 );
44- gsl_matrix_set (m , 1 , 1 , - mu * (y [0 ]* y [0 ] - 1.0 ));
39+ gsl_matrix * m = & dfdy_mat .matrix ;
40+ gsl_matrix_set (m , 0 , 0 , 0.0 );
41+ gsl_matrix_set (m , 0 , 1 , 1.0 );
42+ gsl_matrix_set (m , 1 , 0 , -2.0 * mu * y [0 ]* y [1 ] - 1.0 );
43+ gsl_matrix_set (m , 1 , 1 , - mu * (y [0 ]* y [0 ] - 1.0 ));
4544 dfdt [0 ] = 0.0 ;
4645 dfdt [1 ] = 0.0 ;
4746 return GSL_SUCCESS ;
4847}
4948
50- int main (void )
51- {
49+ int main (void ) {
5250 double x = 5.0 ;
53- double y = gsl_sf_bessel_J0 ( x );
54- printf ( "J0(%g) = %.18e\n" , x , y );
51+ double y = gsl_sf_bessel_J0 (x );
52+ printf ("J0(%g) = %.18e\n" , x , y );
5553
56- // #eqdifs
54+ /* #eqdifs */
5755 {
58- // system param
56+ /* system param */
5957 double mu = 1.0 ;
6058
61- // complete system description:
59+ /* complete system description: */
6260 gsl_odeiv2_system sys = { ode_func , ode_jac , 2 , & mu };
6361
64- // system + solver description:
62+ /* system + solver description: */
6563 gsl_odeiv2_driver * d = gsl_odeiv2_driver_alloc_y_new (
6664 & sys ,
6765 gsl_odeiv2_step_rk8pd , //stepping function (given current state and step size, calculate new state)
@@ -72,80 +70,76 @@ int main (void)
7270
7371 int i ;
7472
75- // initial time:
73+ /* initial time: */
7674 double t = 0.0 ;
7775
78- // max desired time:
76+ /* max desired time: */
7977 double t1 = 100.0 ;
8078
81- // initial condition
79+ /* initial condition */
8280 double y [2 ] = { 1.0 , 0.0 };
8381
84- // number of points to print
82+ /* number of points to print */
8583 const int nPts = 1000 ;
8684
87- // time interval between each print
85+ /* time interval between each print */
8886 double dt = t1 / nPts ;
89-
90- // prepare to get the output
87+
88+ /* prepare to get the output */
9189 FILE * fp = fopen ( "ode.tmp" , "w" );
9290 double datax [nPts ];
9391 double datay [nPts ];
9492
95- for (i = 0 ; i < nPts ; i ++ )
96- {
93+ for (i = 0 ; i < nPts ; i ++ ) {
9794 double ti = i * dt ;
9895
99- //advance time from t to at leaast ti.
100- //t will contain new last time value.
96+ /*
97+ Advance time from t to at leaast is
98+ will contain new last time value.
99+ */
101100 int status = gsl_odeiv2_driver_apply (d , & t , ti , y );
102101
103- if (status != GSL_SUCCESS )
104- {
102+ if (status != GSL_SUCCESS ) {
105103 fprintf ( stderr , "error, return value=%d\n" , status );
106104 break ;
107105 }
108106
109- //#get output
110-
111- //to a file:
112-
113- fprintf ( fp , "%.5e %.5e %.5e\n" , t , y [0 ], y [1 ] );
107+ /* #get output */
114108
115- //to stdout:
109+ /* to a file: */
110+ fprintf ( fp , "%.5e %.5e %.5e\n" , t , y [0 ], y [1 ] );
116111
117- printf ( "%.5e %.5e %.5e\n" , t , y [0 ], y [1 ] );
112+ /* to stdout: */
113+ printf ( "%.5e %.5e %.5e\n" , t , y [0 ], y [1 ] );
118114
119- //to array
120-
121- datax [i ] = y [0 ];
122- datay [i ] = y [1 ];
115+ /* to array */
116+ datax [i ] = y [0 ];
117+ datay [i ] = y [1 ];
123118 }
124-
125- //finalize
126- fclose (fp );
127- gsl_odeiv2_driver_free (d );
128-
129- //plot
130-
131- //#plplot
132-
133- plsdev ("xwin" );
134- plinit ();
135- plenv (
136- gsl_stats_min ( datax , 1 , nPts ),
137- gsl_stats_max ( datax , 1 , nPts ),
138- gsl_stats_min ( datay , 1 , nPts ),
139- gsl_stats_max ( datay , 1 , nPts ),
140- 0 ,
141- 1
142- );
143- plstring ( nPts , datax , datay , "*" );
144- plend ();
145-
146- //#gnuplot
147- //system( "gnuplot -e 'p \"ode.tmp\" u 2:3 w lines; pause -1'" );
119+
120+ /* finalize */
121+ fclose (fp );
122+ gsl_odeiv2_driver_free (d );
123+
124+ /* plot */
125+
126+ /* #plplot */
127+ plsdev ("xwin" );
128+ plinit ();
129+ plenv (
130+ gsl_stats_min ( datax , 1 , nPts ),
131+ gsl_stats_max ( datax , 1 , nPts ),
132+ gsl_stats_min ( datay , 1 , nPts ),
133+ gsl_stats_max ( datay , 1 , nPts ),
134+ 0 ,
135+ 1
136+ );
137+ plstring ( nPts , datax , datay , "*" );
138+ plend ();
139+
140+ /* #gnuplot */
141+ /*system( "gnuplot -e 'p \"ode.tmp\" u 2:3 w lines; pause -1'" );*/
148142 }
149-
150- return 0 ;
143+
144+ return EXIT_SUCCESS ;
151145}
0 commit comments