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
 How to generate a built-in 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

irek

Japan
14 Posts

Posted - 05/28/2003 :  5:03:37 PM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
I have two questions to ask.

2. Is there any sum function, say "sum(f(x),0,5,1)" where f(x) is a function of x and 0,5,1 means for a range of x from 0 to 5 with a step of 1?

Thank you


Edited by - irek on 05/28/2003 5:11:59 PM

Edited by - irek on 05/28/2003 6:11:35 PM

eparent

118 Posts

Posted - 05/29/2003 :  10:37:13 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If I understand you correctly then following code should help you.

 
// Define your functions here
// Your first function
double func1(double x)
{
double y = sqrt(x);
return y;
}

// Your second function
double func2(double x)
{
double y = x^2;
return y;
}

// Define pointer for function
typedef double (*PFNSUM)(double);

// This is the Sum function - uses function pointer to call appropriate function
// The Sum computed is a simple sum of y values at each x - you can replace with your algorithm as needed
double Sum(PFNSUM pfn, double xmin, double xmax, double xstep)
{
double sum = 0;
for(double x = xmin; x <= xmax; x += xstep)
sum += pfn(x);
return sum;
}

// This function makes it accessible from LabTalk
// You can go to script window and type:
// mysum = LTSum(func1, 0, 5, 1)
// and the LT variable mysum will have your sum value
double LTSum(string strFunc, double xmin, double xmax, double xstep)
{
PFNSUM pfn;
// Check name passed from LabTalk and assign pointer accordingly
if( 0 == strFunc.CompareNoCase("func1") )
pfn = func1;
else if( 0 == strFunc.CompareNoCase("func2") )
pfn = func2;
else
return NANUM; // if no match on function name, return missing value
return Sum(pfn, xmin, xmax, xstep); // else call Sum function to compute
}

// This shows how you will call the Sum function from Origin C
void dd()
{
// Call Sum by directly specifying function name as argument
double y = Sum(func1, 1, 2, 1);
}


Edited by - eparent on 05/29/2003 10:47:48 AM
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