示例#1
0
// Create, Draw and fit a TGraph2DErrors
void macro4(){
   gStyle->SetPalette(57);
   const double e = 0.3;
   const int nd = 500;

   TRandom3 my_random_generator;
   TF2 f2("f2",
          "1000*(([0]*sin(x)/x)*([1]*sin(y)/y))+200",
          -6,6,-6,6);
   f2.SetParameters(1,1);
   TGraph2DErrors dte(nd);
   // Fill the 2D graph
   double rnd, x, y, z, ex, ey, ez;
   for (Int_t i=0; i<nd; i++) {
      f2.GetRandom2(x,y);
      // A random number in [-e,e]
      rnd = my_random_generator.Uniform(-e,e);
      z = f2.Eval(x,y)*(1+rnd);
      dte.SetPoint(i,x,y,z);
      ex = 0.05*my_random_generator.Uniform();
      ey = 0.05*my_random_generator.Uniform();
      ez = fabs(z*rnd);
      dte.SetPointError(i,ex,ey,ez);
   }
   // Fit function to generated data
   f2.SetParameters(0.7,1.5);  // set initial values for fit
   f2.SetTitle("Fitted 2D function");
   dte.Fit(&f2);
   // Plot the result
   auto c1 = new TCanvas();
   f2.SetLineWidth(1);
   f2.SetLineColor(kBlue-5);
   TF2   *f2c = (TF2*)f2.DrawClone("Surf1");
   TAxis *Xaxis = f2c->GetXaxis();
   TAxis *Yaxis = f2c->GetYaxis();
   TAxis *Zaxis = f2c->GetZaxis();
   Xaxis->SetTitle("X Title"); Xaxis->SetTitleOffset(1.5);
   Yaxis->SetTitle("Y Title"); Yaxis->SetTitleOffset(1.5);
   Zaxis->SetTitle("Z Title"); Zaxis->SetTitleOffset(1.5);
   dte.DrawClone("P0 Same");
   // Make the x and y projections
   auto c_p= new TCanvas("ProjCan",
                         "The Projections",1000,400);
   c_p->Divide(2,1);
   c_p->cd(1);
   dte.Project("x")->Draw();
   c_p->cd(2);
   dte.Project("y")->Draw();
}
    void derivative_test()
    {
      curve_type cst(7);
      control_point_type cp[8];
      data_type dte(0.01), t[6];
      point_type pt, xp, xp_ref, xpp, xpp_ref;
      index_type i;

      // set the control points
      cp[0] << static_cast<data_type>(0.170987592880629);
      cp[1] << static_cast<data_type>(0.157286894410384);
      cp[2] << static_cast<data_type>(0.162311658384540);
      cp[3] << static_cast<data_type>(0.143623187913493);
      cp[4] << static_cast<data_type>(0.149218456400780);
      cp[5] << static_cast<data_type>(0.137218405082418);
      cp[6] << static_cast<data_type>(0.140720628655908);
      cp[7] << static_cast<data_type>(0.141104769355436);
      for (i=0; i<=cst.upper_degree(); ++i)
      {
        cst.set_upper_control_point(cp[i], i);
        cst.set_lower_control_point(-cp[i], i);
      }

      // set the trailing edge thickness of CST airfoil
      cst.set_trailing_edge_thickness(dte);

      // set the parameters to evaluate the tests
      t[0] = static_cast<data_type>(0.1);
      t[1] = static_cast<data_type>(0.24);
      t[2] = static_cast<data_type>(0.42);
      t[3] = static_cast<data_type>(0.5);
      t[4] = static_cast<data_type>(0.73);
      t[5] = static_cast<data_type>(0.95);

      data_type x[3], y[3], dt(static_cast<data_type>(1e2)*std::sqrt(std::numeric_limits<data_type>::epsilon()));
      eli::mutil::fd::d1o2<data_type> d1_calc;
      eli::mutil::fd::d2o2<data_type> d2_calc;

      // evaluate points on the lower surface
      for (i=0; i<6; ++i)
      {
        pt=cst.f(-t[i]-dt); x[0]=pt(0); y[0]=pt(1);
        pt=cst.f(-t[i]);    x[1]=pt(0); y[1]=pt(1);
        pt=cst.f(-t[i]+dt); x[2]=pt(0); y[2]=pt(1);
        d1_calc.evaluate(xp_ref(0), x, dt);
        d1_calc.evaluate(xp_ref(1), y, dt);
        d2_calc.evaluate(xpp_ref(0), x, dt);
        d2_calc.evaluate(xpp_ref(1), y, dt);
        xp=cst.fp(-t[i]);
        xpp=cst.fpp(-t[i]);
        if (typeid(data_type)==typeid(float))
        {
          TEST_ASSERT((xp-xp_ref).norm()<6e-3);
          if (i==0)
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<7e-2);
          }
          else if (i==1)
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<4e-3);
          }
          else
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<3e-3);
          }
        }
        else
        {
          TEST_ASSERT(tol.approximately_equal(xp, xp_ref));
          TEST_ASSERT((xpp-xpp_ref).norm()<8e-5);
        }
      }

      // evaluate points on the upper surface
      for (i=0; i<6; ++i)
      {
        pt=cst.f(t[i]-dt); x[0]=pt(0); y[0]=pt(1);
        pt=cst.f(t[i]);    x[1]=pt(0); y[1]=pt(1);
        pt=cst.f(t[i]+dt); x[2]=pt(0); y[2]=pt(1);
        d1_calc.evaluate(xp_ref(0), x, dt);
        d1_calc.evaluate(xp_ref(1), y, dt);
        d2_calc.evaluate(xpp_ref(0), x, dt);
        d2_calc.evaluate(xpp_ref(1), y, dt);
        xp=cst.fp(t[i]);
        xpp=cst.fpp(t[i]);
        if (typeid(data_type)==typeid(float))
        {
          TEST_ASSERT((xp-xp_ref).norm()<6e-3);
          if (i==0)
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<7e-2);
          }
          else if (i==1)
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<4e-3);
          }
          else
          {
            TEST_ASSERT((xpp-xpp_ref).norm()<3e-3);
          }
        }
        else
        {
          TEST_ASSERT(tol.approximately_equal(xp, xp_ref));
          TEST_ASSERT((xpp-xpp_ref).norm()<8e-5);
        }
      }
    }
    void evaluation_test()
    {
      typedef eli::geom::curve::pseudo::explicit_bezier<data_type> explicit_bezier_curve_type;

      explicit_bezier_curve_type ebcu(7), ebcl(7);
      curve_type cst(7);
      control_point_type cp[8];
      data_type dte(0.01), t[6];
      point_type pt_out, pt_ref;
      index_type i;

      // set the control points
      cp[0] << static_cast<data_type>(0.170987592880629);
      cp[1] << static_cast<data_type>(0.157286894410384);
      cp[2] << static_cast<data_type>(0.162311658384540);
      cp[3] << static_cast<data_type>(0.143623187913493);
      cp[4] << static_cast<data_type>(0.149218456400780);
      cp[5] << static_cast<data_type>(0.137218405082418);
      cp[6] << static_cast<data_type>(0.140720628655908);
      cp[7] << static_cast<data_type>(0.141104769355436);
      for (i=0; i<=cst.upper_degree(); ++i)
      {
        ebcu.set_control_point(cp[i], i);
        cst.set_upper_control_point(cp[i], i);
        ebcl.set_control_point(-cp[i], i);
        cst.set_lower_control_point(-cp[i], i);
      }

      // set the trailing edge thickness of CST airfoil
      cst.set_trailing_edge_thickness(dte);

      // set the parameters to evaluate the tests
      t[0] = static_cast<data_type>(0);
      t[1] = static_cast<data_type>(0.1);
      t[2] = static_cast<data_type>(0.27);
      t[3] = static_cast<data_type>(0.5);
      t[4] = static_cast<data_type>(0.73);
      t[5] = static_cast<data_type>(1);

      // evaluate the points on lower surface
      i=1;
      pt_out = cst.f(-t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = -(std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2);
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=2;
      pt_out = cst.f(-t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = -(std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2);
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=3;
      pt_out = cst.f(-t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = -(std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2);
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=4;
      pt_out = cst.f(-t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = -(std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2);
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=5;
      pt_out = cst.f(-t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = -(std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2);
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));

      // evaluate the points on upper surface
      i=0;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=1;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=2;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=3;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=4;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
      i=5;
      pt_out = cst.f(t[i]);
      pt_ref = ebcu.f(t[i]);
      pt_ref(1) = std::sqrt(t[i])*(1-t[i])*pt_ref(1)+t[i]*dte/2;
      TEST_ASSERT(tol.approximately_equal(pt_out, pt_ref));
    }