User Tools

Site Tools


libnum:examples:gear

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
libnum:examples:gear [2026/03/04 15:30] abclibnum:examples:gear [2026/03/29 13:46] (current) abc
Line 8: Line 8:
  
 <file> <file>
-#include <stdio.h>+#include <iostream> 
 +#include <sstream> 
 +#include <fstream> 
 #include <math.h> #include <math.h>
 #include "libnum/gear.h" #include "libnum/gear.h"
 +#include "libnum/gear.output.h"
  
 using namespace NumericMethods; using namespace NumericMethods;
  
-bool derfun1(int n, double x, const double y[], double dy[])+/* 
 +    y₁′ = y₂ 
 +    y₂′ = y₁ 
 +    y₁(0)=0  and  y₂(0)=1 
 +    // NB: we expect sin(x)/cos(x) as the result. 
 +*/ 
 + 
 +static bool derfun(int n, double x, const double y[], double dy[])
 { {
   dy[0] = y[1];   dy[0] = y[1];
Line 21: Line 32:
 } }
  
-bool jacobian1(int n, double x, const double y[], double j[])+static bool jacobian(int n, double x, const double y[], double j[])
 { {
-  j[0] = 0; j[1] = 1; j[2] = -1; j[3] = 0;+  //for (int i = 0; i<n*n; ++i) j[i] = 0; 
 +  j[0] = 0; j[1] = 1; j[2] = -1; j[3] = 0; // sin/cos
   return true;   return true;
 } }
  
-bool my_print1(const GearEngineContext&); 
  
 int main() int main()
 { {
- double initValues[] = { 0, 1, 0, 0}; 
- GearUserProc uf1(diffun1), uf2(j1); 
  
- Gear solver+ double initValues[]={ 0,1,0,0}
- solver.set(2&uf1, &uf2); // task dimention is 2+ GearUserProc uf1(derfun),uf2(jacobian);
  
 + Gear solver;
 + solver.set(2,&uf1,&uf2);
  GearAssistant& assistant = solver.assistant;  GearAssistant& assistant = solver.assistant;
- assistant.params.setParameter("HINI", 0.0001);  // starting step of integration  + assistant.params.setParameter("HINI", 0.000001);  // starting step of integration  
- assistant.params.setParameter("EPS", 0.001);    // step error constant + assistant.params.setParameter("EPS", 0.0001);     // step error constant  
 + 
 + //    Output variants 
 + /*1*/ GearPrintToStream print_1(std::cout, "{:8.5f}\t"); 
 + 
 + /*2*/ std::stringstream buf;  // to string; use buf.str() later 
 + GearPrintToStream print_2(buf, "{:8.5f}\t"); 
 + 
 + /*3*/ std::ofstream file("gear_output.txt"); 
 + GearPrintToStream print_3(file, "{:8.5f}\t"); 
 + 
 + GearPrintTable print_4(std::cout, 0.1); 
 +  print_4.setFormat("{:8.5f}\t");
  
- GearEngineCallbackEmbed printHandler(my_print); + solver.setCallback(print_4);
- solver.setCallback(printHandler);+
  
  try  try
  {  {
-  Message rc=solver.solve(0, 5, initValues);     // integrate for t from 0 to 5 +  Message rc=solver.solve(0, 5, initValues);
-  printf("\n\nsteps: %d, derCalls: %d, jacCalls: %d\n\n", +
-    assistant.statistics.steps, assistant.statistics.derCalls, +
-    assistant.statistics.jacCalls);+
  }  }
  catch(Message& e)  catch(Message& e)
  {  {
-   printf("\n\n%s",e.getMessageID().data());+   std::cout << "\n\n" << e.getMessageID().data();
  }  }
- printf("\n"); + std::cout << "\n";
-+
- +
-bool my_print1(const GearEngineContext& context) +
-+
- printf("\n%10.5f:",context.t); +
- // print y values and their derivatives +
- int n = context.n; +
- for(int i=0; i n; ++i) printf("\t%10.5f %10.5f,", +
-     context.Z[i], context.Z[i+n]/context.h); +
- printf("\t(%10.5f  %d)\n", context.h, context.q); +
- return true;+
 } }
 </file> </file>
libnum/examples/gear.1772627418.txt.gz · Last modified: by abc