Hi,
The method should not be sensitive to the actual function? I am not sure why this is not working for you...
I did a quick test with code pasted below and the result looks right..
Easwar
OriginLab
const double h = 0.001;
void finite_derivative()
{
WorksheetPage wpg;
wpg.Create("Origin");
Worksheet wks = wpg.Layers(0);
while( wks.DeleteCol(0) );
wks.AddCol();
wks.Columns(0).SetType(OKDATAOBJ_DESIGNATION_X);
wks.AddCol();
wks.Columns(1).SetLabel("Function");
wks.AddCol();
wks.Columns(2).SetLabel("Derivative");
Dataset dsX(wks, 0);
Dataset dsY(wks, 1);
Dataset dsDY(wks, 2);
for(double x = 0; x < 10; x += 0.1)
{
dsX.Add(x);
dsY.Add(myfunc(x));
dsDY.Add(myderiv(x));
}
GraphPage gpg;
gpg.Create("Origin");
GraphLayer gly = gpg.Layers(0);
gly.AddPlot(wks);
gly.LT_execute("legend;");
gly.Rescale();
}
static double myfunc(double x)
{
return sin(x + 2.5);
}
static double myderiv(double x)
{
return ( (myfunc(x+h) - myfunc(x-h)) / (2 * h) )
}
If you are just interested in computing numerical derivatives, you do not have to write your own routine, but can use the curve_derivative() function listed under Global Functions->Analysis in the Origin C Language Reference help file.
Edited by - easwar on 05/21/2005 07:49:27 AM