What Equation Draws This Graph? (and can you do better?)

Started by
9 comments, last by blizzard999 18 years, 7 months ago
A question: why do you need an analitic function ?
IMHO is more simple to use a piecewise function to do this.
Draw your function as you like on a paper then sample it in some points; when you want the value in x get the interpolation y in that point.
It seems strange but in engineering this method is widely used!

This is the function I use when I need a value from a piecewise function

template<class X, class Y>static Y Interpolate(const std::map<X,Y>& xymap,X x){	if(xymap.empty()) return (Y)0;	if(xymap.size()==1) return xymap.begin()->second;	std::map<X,Y>::const_iterator li=xymap.lower_bound(x);	std::map<X,Y>::const_iterator hi=xymap.upper_bound(x);	if(li==xymap.end()){li=hi=--xymap.end();--li;}	if(li->first>x && li!=xymap.begin())--li;	if(li->first==hi->first && hi!=--xymap.end())++hi;	return (hi->second-li->second)*	       (x-li->first)/(hi->first-li->first)+li->second;}


example

typedef std::map<double, double> FUNCTION;FUNCTION f;// describe your function here (the order is not important because// the map will sort the samples)f[0] = 1;f[1] = 0;double y = Interpolate(f, 0.5);  // y = f( 0.5 ) = 0.5


The function is continued linearly if x is out of the range.

This topic is closed to new replies.

Advertisement