Author |
Topic |
|
aryoo
7 Posts |
Posted - 07/05/2011 : 10:31:34 PM
|
Hi,
I succeeded to fit curve with user defined function having several parameters (up to 8 parameters). However, some data set have many local chi-square minima and each minima close to each other.
Does anyone know how to make a Chi-square landscape or another word the contour plot of Chi-square?
My thought is to fix other parameters and allow only two parameter to fitting and using it as XY axis for contour plot and Chi-square as Z value. However, I don't know how to tell Origin to stop fitting and return corresponding parameter value at particular Chi-square.
Another way is fitting an array of that two parameters shifting by small constant interval. Then allow origin fitting algorithm to perform and ask origin to store a fitting result in each interval.
Are there any command to tell origin to store the fitting result while run looping? Any tutorial or actual command example will be great.
Thank you,
|
|
Penn
China
644 Posts |
Posted - 07/06/2011 : 06:40:52 AM
|
Hi,
There is an Origin C example in this page. You can refer to it.
Penn |
|
|
aryoo
7 Posts |
Posted - 07/06/2011 : 10:58:00 PM
|
Penn, thank for your reply. I don't know what wrong when I tried to follow that thread. I copied the code in to Code Builder, the compiled. After compiled, it showed
compiling... ScriptWindow.OP Linking... Done!
I assume that everything is ok up until this. Then in Command Window, I put
>>run.LoadOC(Originlab\nlsf_utils.c); >>CreateChisqrMatrix 200 300 -3200 -2600 20 80;
I had an error said "Command Error!". Then I tried again with
>>run.CreateChisqrMatrix 200 300 -3200 -2600 20 80; Then "Command Error!" again.
Could anyone enlight me how to run this command?
Thanks
|
|
|
Penn
China
644 Posts |
Posted - 07/06/2011 : 11:37:45 PM
|
Hi,
Maybe the c file you created is not in the currently Origin C workspace in Code Builder. You can try to following the steps below.
1. In Code Builder, create a new c file, named ChiSquare_ex.c for example.
2. Copy the Origin C code in this page, and append to the newly created c file (ChiSquare_ex.c here).
3. In Command Window, run the followign command.
run.LoadOC(Originlab\nlsf_utils.c); 4. Go back to Code Builder, compile the created c file. After that, you can see the file in the workspace like the image below.
5. Import the data "<Origin Installation Directory>\Samples\Curve Fitting\Exponential Decay.dat".
6. Highlight column B and select menu "Plot: Line: Line" to make a line plot.
7. In Command Window, run the Origin C function like:
CreateChisqrMatrix 200 300 -3200 -2600 20 80; Hope that you can succeed this time.
Penn |
|
|
aryoo
7 Posts |
Posted - 07/07/2011 : 12:11:31 AM
|
Hi, Thank you for your detail again. As you said I didn't have the file in the workspace like your image. However, after follow you suggestion. I still have error as follow C:\Users\ATOM\Documents\Origin User Files\ChiSquare_test.c(169) :Error, Member function NLFitSession::SetFunction not defined or does not have matching prototype. C:\Users\ATOM\Documents\Origin User Files\ChiSquare_test.c(137) :Error, error(s) found in compiling method C:\Users\ATOM\Documents\Origin User Files\ChiSquare_test.c(169) :Error, general compile error C:\Users\ATOM\Documents\Origin User Files\ChiSquare_test.c(137) :Error, error(s) found in compiling function CreateChisqrMatrix
I think the problem is "NLFitSession::SetFunction not defined or does not have matching prototype" How could I fix there error. Thanks, |
|
|
Penn
China
644 Posts |
Posted - 07/07/2011 : 01:42:22 AM
|
Hi,
I am not sure which version of Origin you are using, you can check by menu Help: About Origin. I have tried this in Origin 8 SR0, it came up the same error messages. However, in Origin 8 SR6, it works. So, I guess you are using Origin 8, but not SR6. If so, please update to Origin 8 SR6 from this page and then try again.
Penn |
|
|
aryoo
7 Posts |
Posted - 07/10/2011 : 9:20:33 PM
|
Penn, You right. I have SR0. So I did update to Origin 8 SR6. I think there is also an update in the example. Good news, I can do as the example said.
Now, I want to use my custom function that I create in user defined in "Fitting function organizer".
I tried to change from "Poly" function in example to my function in two point of the example code.
BOOL chiSqrSurf(const vector& vX, const vector& vY, const vector& vParams, double dXFrom, double dXTo, int nXStep, double dYFrom, double dYTo, int nYStep, string strFunc = "Function_X2_landscape", int nVaryParamX = 0, int nVaryParamY = 1) {
// The following 3 steps to access NLFitSession to get parameter init values // 1. Set fucntion string strFunc = "Function_X2_landscape"; NLFitSession FitSession; if(!FitSession.SetFunction(strFunc, NULL)) // set function, category name can be ignore { MessageBox(NULL, "Fail to set fitting function"); return; }
Then, I changed in the range of data in here chiSqrSurf(vX, vY, vParams, 200, 300, 6, -3200, -2600, 6, strFunc);
to>>> chiSqrSurf(vX, vY, vParams, -1, 1, 0.1, -1, 1, 0.1, strFunc);
I use this range because my fitting result show that my parameters; a1 andd a2 in this range.
Model Function_1 (User) Equation y=sqrt((1+a1*x)^2 + (a2*x)^2); Reduced Chi-Sqr 2.867E-4 Adj. R-Square 0.87735 Value Standard Error a1 -0.45556 0.01981 a2 0.55863 0.0182
However, it show that the command error when I do so.
Invalid object error #Command Error!
Do you have any idea how to fix this?
Thank for your help, Aryoo |
Edited by - aryoo on 07/10/2011 9:24:55 PM |
|
|
aryoo
7 Posts |
Posted - 07/11/2011 : 12:59:18 AM
|
More information about error
D:\Origin8_User Files\OriginC\CUSTOMFUN.C(57) :Origin C Function Runtime Error, Invalid object error
// prepare matrix window MatrixPage matPage; matPage.Create("origin"); MatrixLayer ml = matPage.Layers(0); MatrixObject mo = ml.MatrixObjects(0); Matrix& mat = mo.GetDataObject(); mo.SetXY(dXFrom, dYFrom, dXTo, dYTo); mat = mm; <<<<< This is line#57
My data is
X Y 1.2474 0.82146 1.2474 0.81281 0.567 0.8212 0.567 0.82604 0.3335 0.86347 0.3335 0.86439 0.29 0.87502 0.29 0.89357 0.2522 0.88826 0.2522 0.89738 0.194 0.8739 0.194 0.89789 0.097 0.96767 0.097 0.96447
Custom Equation>> y=sqrt((1+a1*x)^2 + (a2*x)^2) a1 and a2 are parameter which I would like to plot Chi-square landscape.
Thank you, |
|
|
Penn
China
644 Posts |
Posted - 07/11/2011 : 06:19:02 AM
|
Hi,
In the example, the number of steps is needed, but not the increment step. That is to say, please change the line you mentioned:
chiSqrSurf(vX, vY, vParams, -1, 1, 0.1, -1, 1, 0.1, strFunc);
to
chiSqrSurf(vX, vY, vParams, -1, 1, 10, -1, 1, 10, strFunc);
Sorry for the confusion.
Penn |
|
|
aryoo
7 Posts |
Posted - 07/11/2011 : 07:36:19 AM
|
Thank Peen for response,
However, it still have the same error. I forgot that it did generate 31 by 31matrix but no value inside before and change the number of steps.
Thank you, |
|
|
cpyang
USA
1406 Posts |
Posted - 07/11/2011 : 09:50:27 AM
|
Maybe originlab should put this together as a tool to put on file exchange.
|
|
|
Penn
China
644 Posts |
Posted - 07/12/2011 : 04:11:46 AM
|
Hi,
It is very strange that you cannot get the code work. Here I will provide the whole procedure on how I run the example code, with your fitting function and data, together with the screenshots.
1. Start Origin 8 SR6.
2. Open Code Builder, and then select menu File: New Workspace to create a new workspace, so to make the procedure clean.
3. Select menu File: New to create a new c file, here named chisqare_space.c. Please make sure that the name you used does not exist yet.
4. Copy the example code from this page and paste to the newly created c file.
5. In the chiSqrSurf_ex function, change the line:
string strFunc = "Poly"; to
string strFunc = "Function_X2_landscape"; And the line:
chiSqrSurf(vX, vY, vParams, 200, 300, 6, -3200, -2600, 6, strFunc); to
chiSqrSurf(vX, vY, vParams, -1, 1, 31, -1, 1, 31, strFunc);
6. Go back to Origin main window. Open the Command Window by menu Window: Command Window, if it is not opened yet. Then run the following script.
run.LoadOC(Originlab\nlsf_utils.c);
7. Switch to Code Builder again, and compile the c file.
8. Go back to Origin main window, and create a fitting function with the name of "Function_X2_landscape" for your equation.
9. Create a line plot from your data, like below.
10. Activate this graph and run the following line in Command Window.
chiSqrSurf_ex1
11. Finally get the result graph like:
I hope that you can get the code work now. If still not, please provide your details steps. Thanks.
Penn |
|
|
aryoo
7 Posts |
Posted - 07/12/2011 : 09:34:58 AM
|
Penn,
Thank you so much. Now I can run what I want.
I found that I did add the initial value in my function with a1=-0.4 and a2=0.5 that why I couldn't run it. When I change the initial value to a1=0 and a2=0, then Bang! I can run it now.
One more question, how could I export the result into new workbook with the data in column like this?
a1 a2 chi_square -1 -1 xxxx . . xxxx . . xxxx 1 1 xxxx
Thank you for your help and support, I very much appreciate your help. Aryoo |
|
|
Penn
China
644 Posts |
Posted - 07/12/2011 : 9:25:11 PM
|
Hi Aryoo,
You can use the m2w X-Function to convert the matrix data to XYZ worksheet data.
Penn |
|
|
|
Topic |
|
|
|