math - evaluating function only defined by table, C++ -


i'd evaluate function in c++, have values defined in table such following:

entry #  en(ev)   effective qm - defined in introduction(1e-16*cm^2)     1        0     4.96    2    0.001     4.98    3    0.002     5.02    4    0.003     5.07    5    0.005     5.12    6    0.007     5.15    7   0.0085     5.18 

i'd have function, returns effective qm each energy. ideally interpolated value, i'll happy rounding closest energy qm known , giving result. have no idea how should done. know easy in mathematica, rest of code faster in c++.

suppose create struct data:

struct energyentry {     double energy;     double qm; }; 

i'm going assume can load data vector. first-level approach you're asking load data , find closest entry. keep track of closest entry , update iterate through data. @ end you'll have closest item.

std::vector<energyentry> entries; loadentries(entries); // assuming can  double targetenergy = 0.0015; // value want  int best = 0; (int x = 1; x < entries.size(); x++) {     if (fabs(entries[x].energy-targetenergy) < fabs(entries[best].energy-targetenergy))         best = x; }  cout << "qm " << entries[best].qm << "\n"; 

some caveats here. make sure pass vector loadentries reference. might need floating point compares if precision matters. want code in function. but, gets started.

if want more, can regression try model data (linear, logistic, or neural network). or, can interpolate if fall between 2 points. that's separate question.


Comments

Popular posts from this blog

javascript - how to protect a flash video from refresh? -

android - Associate same looper with different threads -

visual studio 2010 - Connect to informix database windows form application -