The Origin Forum
File Exchange
Try Origin for Free
The Origin Forum
Home | Profile | Register | Active Topics | Members | Search | FAQ | Send File to Tech support
Username:
Password:
Save Password
Forgot your Password? | Admin Options

 All Forums
 Origin Forum for Programming
 Forum for Origin C
 Import NAG function
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

mwort

6 Posts

Posted - 08/18/2020 :  4:49:21 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
Hi,
I am completely new to C. To fit a model I need the hypergeometric Kummer function from the NAG library and I have already read a lot about it in the manual (https://www.originlab.com/pdfs/nagcl25/manual/pdf/s/s22bbc.pdf#lnk_s22bbc). If I want to use the function, do I simply have to copy the program text into the Code Builder?
When I do that, I get the error message: "...\OriginC\NLSF\_nlfnag_specfun_1f1_real_scaled.fit(11) :Error, include file not found"
Does anyone perhaps have a practical hint/instruction that is understandable for a complete beginner?

kind regards,
Martin

YimingChen

1621 Posts

Posted - 08/18/2020 :  5:10:40 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi,

Can you please check this tutorial page to fit NAG function:
https://www.originlab.com/doc/Tutorials/Fitting-Special-NAG

James
Go to Top of Page

mwort

6 Posts

Posted - 08/18/2020 :  5:22:38 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hey James,

thank you for your response. I have done exactly what is suggested in the tutorial you posted - I just exchanged the program text with the one from this link I stated in my original post.
I still get the same error message...
Go to Top of Page

Castiel

343 Posts

Posted - 08/18/2020 :  5:37:59 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by mwort

Hi,
I am completely new to C. To fit a model I need the hypergeometric Kummer function from the NAG library and I have already read a lot about it in the manual (https://www.originlab.com/pdfs/nagcl25/manual/pdf/s/s22bbc.pdf#lnk_s22bbc). If I want to use the function, do I simply have to copy the program text into the Code Builder?
When I do that, I get the error message: "...\OriginC\NLSF\_nlfnag_specfun_1f1_real_scaled.fit(11) :Error, include file not found"
Does anyone perhaps have a practical hint/instruction that is understandable for a complete beginner?

kind regards,
Martin





Show your code.


------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------
Go to Top of Page

mwort

6 Posts

Posted - 08/18/2020 :  6:04:53 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
#pragma numlittype(push, TRUE)
#pragma warning(error : 15618)
#include <origin.h>

// Add your special include files here.
// For example, if you want to fit with functions from the NAG library,
// add the header file for the NAG functions here.

// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.

// You can access C functions defined in other files, if those files are loaded and compiled
// in your workspace, and the functions have been prototyped in a header file that you have
// included above.

// You can access NLSF object methods and properties directly in your function code.

// You should follow C-language syntax in defining your function.
// For instance, if your parameter name is P1, you cannot use p1 in your function code.
// When using fractions, remember that integer division such as 1/2 is equal to 0, and not 0.5
// Use 0.5 or 1/2.0 to get the correct value.

// For more information and examples, please refer to the "User-Defined Fitting Function"
// section of the Origin Help file.

//----------------------------------------------------------
//
void _nlsfnag_specfun_1f1_real_scaled(
// Fit Parameter(s):
double a, double b,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nags.h>
#include <nagx02.h>
int main(void)
{
/* Scalars */
Integer exit_status = 0;
Integer k, maxexponent, scm;
double ani, adr, bni, bdr, delta, frm, x;
/* Arrays */
double frmv[2];
Integer scmv[2];
/* Nag Types */
NagError fail;
maxexponent = X02BLC;
printf("nag_specfun_1f1_real_scaled (s22bbc) Example Program Results\n\n");
ani = -10.0;
bni = 30.0;
delta = 1.0E-4;
adr = delta;
bdr = -delta;
x = 25.0;
printf("%9s%10s%10s%12s%9s%12s\n", "a", "b", "x", "frm", "scm", "M(a,b,x)");
for (k = 0; k < 2; k++)
{
INIT_FAIL(fail);
/* Compute the real confluent hypergeometric function M(a,b,x) in scaled
* form using nag_specfun_1f1_real_scaled (s22bbc).
*/
nag_specfun_1f1_real_scaled(ani, adr, bni, bdr, x, &frm, &scm, &fail);
switch (fail.code) {
case NE_NOERROR:
case NW_UNDERFLOW_WARN:
case NW_SOME_PRECISION_LOSS:
{
if (scm < maxexponent)
printf(" %9.4f %9.4f %9.4f %13.4e %5"NAG_IFMT" %13.4e\n",
ani+adr, bni+bdr, x, frm, scm, frm*pow(2.0, scm));
else
printf(" %9.4f %9.4f %9.4f %13.4e %5"NAG_IFMT" %17s\n",
ani+adr, bni+bdr, x, frm, scm, "Not Representable");
frmv[k] = frm;
scmv[k] = scm;
break;
}
default:
{
/* Either the result has overflowed, no accuracy may be assumed,
* or an input error has been detected.
*/
printf(" %9.4f %9.4f %9.4f %17s\n", ani+adr, bni+bdr, x, "FAILED");
exit_status = 1;
goto END;
}
}
adr = -adr;
bdr = -bdr;
}
/* Calculate the product M1*M2*/
frm = frmv[0] * frmv[1];
scm = scmv[0] + scmv[1];
printf("\n");
if (scm < maxexponent)
printf("%-30s%12.4e%6"NAG_IFMT"%12.4e\n",
"Solution product", frm, scm, frm*pow(2.0, scm));
else
printf("%-30s%12.4e%6"NAG_IFMT"%17s\n",
"Solution product", frm, scm, "Not Representable");
/* Calculate the ratio M1/M2*/
if (frmv[1] != 0.0)
{
frm = frmv[0]/frmv[1];
scm = scmv[0] - scmv[1];
printf("\n");
if (scm < maxexponent)
printf("%-30s%12.4e%6"NAG_IFMT"%12.4e\n",
"Solution ratio", frm, scm, frm*pow(2.0, scm));
else
printf("%-30s%12.4e%6"NAG_IFMT"%17s\n",
"Solution ratio", frm, scm, "Not Representable");
}
END:
return exit_status;
}
// End of editable part
}
Go to Top of Page

Castiel

343 Posts

Posted - 08/18/2020 :  8:32:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by mwort

#pragma numlittype(push, TRUE)
#pragma warning(error : 15618)
#include <origin.h>

// Add your special include files here.
// For example, if you want to fit with functions from the NAG library,
// add the header file for the NAG functions here.

// Add code here for other Origin C functions that you want to define in this file,
// and access in your fitting function.

// You can access C functions defined in other files, if those files are loaded and compiled
// in your workspace, and the functions have been prototyped in a header file that you have
// included above.

// You can access NLSF object methods and properties directly in your function code.

// You should follow C-language syntax in defining your function.
// For instance, if your parameter name is P1, you cannot use p1 in your function code.
// When using fractions, remember that integer division such as 1/2 is equal to 0, and not 0.5
// Use 0.5 or 1/2.0 to get the correct value.

// For more information and examples, please refer to the "User-Defined Fitting Function"
// section of the Origin Help file.

//----------------------------------------------------------
//
void _nlsfnag_specfun_1f1_real_scaled(
// Fit Parameter(s):
double a, double b,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y)
{
// Beginning of editable part
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <nag.h>
#include <nag_stdlib.h>
#include <nags.h>
#include <nagx02.h>
int main(void)
{
/* Scalars */
Integer exit_status = 0;
Integer k, maxexponent, scm;
double ani, adr, bni, bdr, delta, frm, x;
/* Arrays */
double frmv[2];
Integer scmv[2];
/* Nag Types */
NagError fail;
maxexponent = X02BLC;
printf("nag_specfun_1f1_real_scaled (s22bbc) Example Program Results\n\n");
ani = -10.0;
bni = 30.0;
delta = 1.0E-4;
adr = delta;
bdr = -delta;
x = 25.0;
printf("%9s%10s%10s%12s%9s%12s\n", "a", "b", "x", "frm", "scm", "M(a,b,x)");
for (k = 0; k < 2; k++)
{
INIT_FAIL(fail);
/* Compute the real confluent hypergeometric function M(a,b,x) in scaled
* form using nag_specfun_1f1_real_scaled (s22bbc).
*/
nag_specfun_1f1_real_scaled(ani, adr, bni, bdr, x, &frm, &scm, &fail);
switch (fail.code) {
case NE_NOERROR:
case NW_UNDERFLOW_WARN:
case NW_SOME_PRECISION_LOSS:
{
if (scm < maxexponent)
printf(" %9.4f %9.4f %9.4f %13.4e %5"NAG_IFMT" %13.4e\n",
ani+adr, bni+bdr, x, frm, scm, frm*pow(2.0, scm));
else
printf(" %9.4f %9.4f %9.4f %13.4e %5"NAG_IFMT" %17s\n",
ani+adr, bni+bdr, x, frm, scm, "Not Representable");
frmv[k] = frm;
scmv[k] = scm;
break;
}
default:
{
/* Either the result has overflowed, no accuracy may be assumed,
* or an input error has been detected.
*/
printf(" %9.4f %9.4f %9.4f %17s\n", ani+adr, bni+bdr, x, "FAILED");
exit_status = 1;
goto END;
}
}
adr = -adr;
bdr = -bdr;
}
/* Calculate the product M1*M2*/
frm = frmv[0] * frmv[1];
scm = scmv[0] + scmv[1];
printf("\n");
if (scm < maxexponent)
printf("%-30s%12.4e%6"NAG_IFMT"%12.4e\n",
"Solution product", frm, scm, frm*pow(2.0, scm));
else
printf("%-30s%12.4e%6"NAG_IFMT"%17s\n",
"Solution product", frm, scm, "Not Representable");
/* Calculate the ratio M1/M2*/
if (frmv[1] != 0.0)
{
frm = frmv[0]/frmv[1];
scm = scmv[0] - scmv[1];
printf("\n");
if (scm < maxexponent)
printf("%-30s%12.4e%6"NAG_IFMT"%12.4e\n",
"Solution ratio", frm, scm, frm*pow(2.0, scm));
else
printf("%-30s%12.4e%6"NAG_IFMT"%17s\n",
"Solution ratio", frm, scm, "Not Representable");
}
END:
return exit_status;
}
// End of editable part
}



C does not work like that. You may want some C tutorial first.


------------------------------------------
       Be The Change
             You Want To See
                   In The World
------------------------------------------
Go to Top of Page

YimingChen

1621 Posts

Posted - 08/19/2020 :  2:56:17 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Hi Martin,

Here is a much easier way to fit Hypergeometric function (defined in Python) in Origin 2021. Here as an example I used hyp0f1() function. Please send email to <tech@originlab.com> and we can send you a beta version for testing.
http://mpmath.org/doc/current/functions/hypergeometric.html

Here are the steps:
1. In Origin main workspace, select from menu Connectivity:Open LabTalk Default... and paste in the code below. The Python function then can be recognized by Origin LabTalk script.
Note: In Code Builder Window, you can select menu Tools:Python Packages.. to install mpmath module.

from mpmath import *
    
def hyp3(a,x):
    return float(hyp0f1(a,x))


2. Close Code Builder, Hit F8 to open Fitting Function Builder.. dialog. Create a new function and make sure Function Type is set to LabTalk Script. And define the function as shown below:


3. Then you should be able to fit your data with the fitting function. See result below:


James

Edited by - YimingChen on 08/19/2020 3:50:58 PM
Go to Top of Page

Peter9312

12 Posts

Posted - 09/15/2021 :  03:30:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I'm using 2021b (no beta) and would like to use this tutorial to implement another function. When I open the "Connectivity" menu I cannot find "Open LabTalk Default". Is it different to the beta mentioned here?

All I see in this menu is:
- MATLAB console
- R console
- Rserve console
- Mathematica link
- Python console
- new untitled.py ...
- open Python functions ...
- open examples.py ...
- open ...
- Python packages
Go to Top of Page

YimingChen

1621 Posts

Posted - 09/15/2021 :  10:45:53 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Please choose Open Default Python Functions...

But we have improved this area to fit with Python function, please use the method in the page below:
https://www.originlab.com/doc/python/tools/NLFit-and-Peak-Analyzer

James

Edited by - YimingChen on 09/15/2021 10:48:09 AM
Go to Top of Page

Peter9312

12 Posts

Posted - 09/16/2021 :  03:19:33 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Thanks, James. I will try the way explained on the side you mentioned.

BTW, what is the exact difference between "Python Function(Vector)" and "Python Function(scalar)"? I mean, the example formula on the side is scalar as far as I see it and that's why I do not understand why the vector version is used in the example?

Go to Top of Page

YimingChen

1621 Posts

Posted - 09/16/2021 :  1:50:21 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If you check the Python function definition in the page, the input x and return y are actually Python lists. If you define Python function this way, you need to choose Python Function(Vector) in the first page. On the other hand, if the input and return value is just a scalar, you need to choose Python Function(scalar).

def MassDiffuse(x, y0, D, L):
	sm = [float((nsum(lambda ii: 1/(2*ii+1)**2*exp(-D*(2*ii+1)**2*np.pi**2*t/(4*L**2)),[0, inf]))) for t in x]
	return [y0*(1-8/np.pi**2*t) for t in sm]


James

quote:
Originally posted by Peter9312

Thanks, James. I will try the way explained on the side you mentioned.

BTW, what is the exact difference between "Python Function(Vector)" and "Python Function(scalar)"? I mean, the example formula on the side is scalar as far as I see it and that's why I do not understand why the vector version is used in the example?




Edited by - YimingChen on 09/16/2021 1:51:55 PM
Go to Top of Page
  Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
The Origin Forum © 2020 Originlab Corporation Go To Top Of Page
Snitz Forums 2000