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 14:06] abclibnum:examples:gear [2026/03/29 13:46] (current) abc
Line 1: Line 1:
-====== Integration of Systems of Linear Differential Equations ====== +====== Integration of Systems of Linear Differential Equations. Gear's method ====== 
-Gear Method.\\+
 Let's examine the following //SLDE//: Let's examine the following //SLDE//:
   y₁′ = y₂   y₁′ = y₂
   y₂′ = y₁   y₂′ = y₁
   y₁(0)=0  and  y₂(0)=1   y₁(0)=0  and  y₂(0)=1
-  # we expect sin(x)/cos(x) as the result+NB: we expect //sin(x)/cos(x)// as the result.
  
 <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;
  
 +/*
 +    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[])
 { {
- double initValues[] = { 0, 1, 0, 0}; +  dy[0] = y[1]; 
- GearUserProc uf1(diffun1), uf2(j1);+  dy[1] = -y[0]; 
 +  return true; 
 +
 + 
 +static bool jacobian(int n, double x, const double y[], double j[]) 
 +
 +  //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; 
 +
 + 
 + 
 +int main() 
 +
 + 
 + double initValues[]={ 0,1,0,0}; 
 + GearUserProc uf1(derfun),uf2(jacobian);
  
  Gear solver;  Gear solver;
- solver.set(2, &uf1, &uf2); // task dimention is 2+ 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 
  
- GearEngineCallbackEmbed printHandler(my_print); + //    Output variants 
- solver.setCallback(printHandler);+ /*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"); 
 + 
 + solver.setCallback(print_4);
  
  try  try
  {  {
-  Message rc=solver.solve(0, 5, initValues);     // integrate for t from 0 to 5+  Message rc=solver.solve(0, 5, initValues);
  }  }
- catch(Message e) {}+ catch(Messagee) 
 + { 
 +   std::cout << "\n\n" << e.getMessageID().data(); 
 + } 
 + std::cout << "\n";
 } }
 </file> </file>
 +
 +----
 +[ [[..:classes:|Libnum methods]] ] 
  
libnum/examples/gear.1772622402.txt.gz · Last modified: by abc