Author |
Topic |
|
AddExperience
Germany
3 Posts |
Posted - 06/19/2017 : 04:23:18 AM
|
Origin Ver. and Service Release (Select Help-->About Origin): OriginPro 9.1.0G (64-bit) Operating System: Windows 10 64-bit
Good morning,
I am trying to build a fitting function with a summation. My function is:
y = a * sum[1/(2n+1) * {exp(-(2n+1)^2 * b * x) - 1}] with n from 0 to 20
with a = constant, and I also know the value of a.
Is there a way to fit this function with origin? I've already tried to reproduce the fitting procedure of this thread (without sucess): http://www.originlab.com/forum/topic.asp?TOPIC_ID=28114 I also already read the tutorial about fitting with summation, but it deals with an integral. http://wiki.originlab.com/~originla/howto/index.php?title=Tutorial:Fitting_with_Summation
Thank you very much for your help in advance!
Regards AddExperience
|
|
arstern
USA
237 Posts |
Posted - 06/19/2017 : 10:35:03 AM
|
Hi,
How did you setup your fit function from using the forum thread? Also what type of error appeared when using this method (or how was it not successful?)
Thanks, Aviel OriginLab |
|
|
AddExperience
Germany
3 Posts |
Posted - 06/22/2017 : 04:24:20 AM
|
Hey,
I used the method described by yuki wu in the mentioned forum thread:
quote: Hi,
In my previous post, you should set these items as follow:
Independent Variables: x Dependent Variables: y Parameter Names: n Function Form: Equations Function: y = sum(x, n) LabTalk Functions Definition and Initializations:
function double sum(double x, double n) { double bb = 0; for(ii =1; ii<=n; ii++) { bb = ii * x * x + bb; } return bb; }
As data I used:
x y 0 0 1 55 2 220 3 495 4 880 5 1375
Which would led to n = 10.
The error mesage is: Fit konvergiert nicht - Grund unbekannt. I think this translates to fit does not converge - unknown reason.
I also expanded the fit function to:
y = a * sum(x, n)
and set n as a constant and n = 10. With the same data it should led to a = 1. But the error message is again "fit does not converge - unknown reason".
I also get sometimes, not always, the error message: column name not definded: sum(x, n) (Spaltenname nicht definiert: sum(x, n)).
Thanks, AddExperience |
|
|
yuki_wu
896 Posts |
Posted - 06/23/2017 : 05:04:13 AM
|
Hi,
I camp up this very simple example only for showing how to define a fitting function with a summation. I am sorry that I only checked its simulation at that time but did not take into account the fitting part.
The reason why fit did not coverage is that the parameter n is an integer but is treated as a double during the fitting. Thus, there are many solutions of this fitting. Just imagine, when Origin is finding the nearest value via iterations, 10.1 is okay while 10.2 is also okay based on the function “for(int ii =1; ii<=n; ii++)”.
I have a workaround to fix this issue. You should define the function in Origin C type:
Independent Variables: x Dependent Variables: y Parameter Names: n Function Form: Origin C Derivatives: check
Function: 1) Click the button next to Function box to open Code Builder 2) Edit the function:
void _nlsfsummation(
// Fit Parameter(s):
double n,
// Independent Variable(s):
double x,
// Dependent Variable(s):
double& y,
// Partial Derivative(s):
double& dy_n)
{
// Beginning of editable part
double bb = 0;
for(int ii =1; ii<=n; ii++)
{
if(floor(n)==ii)
bb = n * x * x + bb;
else
bb = ii * x * x + bb;
}
y = bb;
dy_n= x^2;
// End of editable part
}
Hope it helps.
Regards, Yuki OriginLab
|
|
|
AddExperience
Germany
3 Posts |
Posted - 06/28/2017 : 06:04:00 AM
|
Hey,
The above mentioned procedure works pretty fine. Thank you. I adapted the example to my problem:
y = a * sum[1/(2n+1) * {exp(-(2n+1)^2 * b * x) - 1}] with n from 0 to 20
with a = constant, and I also know the value of a.
Independent Variables: x Dependent Variables: y Parameter Names: b Constant Names: a,n Function Form: Origin C Derivatives: check
Function: 1) Click the button next to Function box to open Code Builder 2) Edit the function:
void _nlsfsummation( // Fit Parameter(s): double b, // Independent Variable(s): double x, // Dependent Variable(s): double& y, // Partial Derivative(s): double& dy_b) { const double n=20; const double a=0.02021; // Beginning of editable part double bb = 0; for(int ii =0; ii<=n; ii++) { if(floor(n)==ii) bb = a / ((2 * n + 1)^2) * (exp( - ((2 * n + 1)^2) * b * x) - 1) + bb; else bb = a / ((2 * ii + 1)^2) * (exp( - ((2 * ii + 1)^2) * b * x) - 1) + bb; } y = bb; dy_b= - a * x * exp( - ((2 * ii + 1)^2) * b * x); // End of editable part }
n and a is then set as needed.
Thank you for your kind help. I hope my answer helps if someone struggles with a similar problem.
|
|
|
|
Topic |
|
|
|