Find XY

The following procedure will show how to use the specified parameter values to get the value of the dependent variable from the independent variable or get the value of the independent variable from the dependent variable.


The formula of getting Y from X:

y = a + x * b;

The formula of getting X from Y:

x = (y - a) / b;


For non-linear function, we use NumericFunction class to get y from x and use ocmath_find_xs function to get x from y.

Prior to running the following example, the nlsf_utils.c file need to be loaded and compiled. This can be done from script with the command run.LoadOC(Originlab\nlsf_utils.c) or just add this file to your workspace.

Get Y from X

#include <ONLSF.h>
#include <..\Originlab\nlsf_utils.h>
void _findy_from_x()
	// Please use the proper function from the related category in 
	// Fitting Function Organizer dialog. Press F9 to open this dialog. 
	// The Poly function below under Polynomial category.
	string 	strFuncFileName = "Poly";
	Tree	trFF;
	if( !nlsf_load_fdf_tree(trFF, strFuncFileName) )
		out_str("Fail to load function file to tree");
	NumericFunction	func;
	if (!func.SetTree(trFF))
		out_str("NumericFunction object init failed");
	int nNumParamsInFunc = trFF.GeneralInformation.NumberOfParameters.nVal;
	vector vParams(nNumParamsInFunc);
	vParams = NANUM;
	vParams[0] = 1;
	vParams[1] = 2;
	vParams[2] = 3;		

	vector vX = {1, 1.5, 2};
	vector vY;
	vY = func.Evaluate(vX, vParams);	

Get X from Y

The following function shows how to get two X values from the specified Y value. Before running, please import Samples\Curve Fitting\Gaussian.dat to Worksheet and keep the Worksheet active.

#include <...\originlab\nlsf_utils.h>
#include <FDFTree.h> 
void _findx_from_y()
	double y = 20; // assign 20 as Y value to find X value
	Worksheet wks = Project.ActiveLayer();
	if (!wks)

	//get data to fitting
	DataRange dr;
	dr.Add(wks, 0, "X");
	dr.Add(wks, 1, "Y");
	DWORD dwPlotID;
	vector vDataX, vDataY;
	&vDataY, &vDataX) < 0)
		printf("failed to get data");
	uint nFindXNum = 2; //set how many x should be found
	vector vFindX;
	string strFile = GetOriginPath() + "OriginC\\OriginLab\\nlsf_utils.c";
		Project.FindFunction("compute_y_by_x", strFile, true);
	string strFuncFileName = "Gauss";
	vector vParams(4);
	vParams[0] = 5.58333; // y0
	vParams[1] = 26; // xc
	vParams[2] = 8.66585; // w
	vParams[3] = 976.41667; // A
	int nRet = ocmath_find_xs(y, (uint)(vDataY.GetSize()), vDataX, 
		vDataY, nFindXNum, vFindX, strFuncFileName, vParams.GetSize(), 
		vParams, pFunc);	
	if( OE_NOERROR == nRet )
		printf("Y = %g\tX1 = %g\tX2 = %g\n", y, vFindX[0], vFindX[1]);