1.12.3.5 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.

Linear

The formula of getting Y from X:

y = a + x * b;

The formula of getting X from Y:

x = (y - a) / b;

Non-linear

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");
		return;
	}
	
	NumericFunction	func;
	if (!func.SetTree(trFF))
	{
		out_str("NumericFunction object init failed");
		return;
	}
	
	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)
	{
		return;
	}

	//get data to fitting
	DataRange dr;
	dr.Add(wks, 0, "X");
	dr.Add(wks, 1, "Y");
	DWORD dwPlotID;
	vector vDataX, vDataY;
	if(dr.GetData(DRR_GET_DEPENDENT | DRR_NO_FACTORS, 0, &dwPlotID, NULL, 
	&vDataY, &vDataX) < 0)
	{
		printf("failed to get data");
		return;
	}	
	
	uint nFindXNum = 2; //set how many x should be found
	vector vFindX;
	vFindX.SetSize(nFindXNum);
	
	string strFile = GetOriginPath() + "OriginC\\OriginLab\\nlsf_utils.c";
	PFN_STR_INT_DOUBLE_DOUBLE_DOUBLEP pFunc = 
		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]);
}