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
 Origin Forum
 Non-linear fitting of data as column formula
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic Lock Topic Edit Topic Delete Topic New Topic Reply to Topic

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  07:53:00 AM  Show Profile  Edit Topic  Reply with Quote  View user's IP address  Delete Topic
OriginPro 2022b (64-bit) SR1 9.9.5.167
Windows 7 Pro SP1 x64

Greetings!

I have the following data structure:


What is the easiest way to fill columns L, M and Y with non-linear fit parameters for the following relationship?


Can I accomplish this using a formula?

Thank you.

---
Andrey

YimingChen

1664 Posts

Posted - 07/01/2022 :  10:15:09 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
You can set column values with Python function. See the example below:




import originpro as op
import numpy as np
from scipy.optimize import curve_fit

wks = op.find_sheet()

def func(x, a, b, c):
    return a + (1 - a) * (b/x)**(1/c)
    
def fit(i, help):
    '''f:i'''
    x = np.linspace(1, 5, 6)
    y = wks.to_list2(i,i,0,5)
    popt, pcov = curve_fit(func, x, np.transpose(y)[0])
    return popt[0]


Edited by - YimingChen on 07/01/2022 10:16:16 AM
Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  11:40:14 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

You can set column values with Python function. See the example below:



Haven't got the method.
Can you share a sample project, please?

---
Andrey
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/01/2022 :  11:47:30 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise.opju

James
Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  11:48:40 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

import originpro as op
import numpy as np
from scipy.optimize import curve_fit

wks = op.find_sheet()

def func(x, a, b, c):
    return a + (1 - a) * (b/x)**(1/c)
    
def fit(i, help):
    '''f:i'''
    x = np.linspace(1, 5, 6)
    y = wks.to_list2(i,i,0,5)
    popt, pcov = curve_fit(func, x, np.transpose(y)[0])
    return popt[0]




Few questions on the code.
1) Is help parameter of fit function redundant and can be removed?

---
Andrey
Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  11:51:19 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise.opju
James



Dear James,

OK, now I see how it works.

Can I do the same using the built-in Origin's NLF engine via LT and column formula?

---
Andrey
Go to Top of Page

aplotnikov

Germany
169 Posts

Posted - 07/01/2022 :  12:14:28 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

You can set column values with Python function
from scipy.optimize import curve_fit


It seems to be a very promising approach: to use Python libraries instead of Origin "native" features. It gives some users (like me) something to think about...
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/01/2022 :  12:30:22 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
I don't find a way to do so with Labtalk. Looks to me the input of nlbegin has to be columns.

James

quote:
Originally posted by AKazak

quote:
Originally posted by YimingChen

https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise.opju
James



Dear James,

OK, now I see how it works.

Can I do the same using the built-in Origin's NLF engine via LT and column formula?

---
Andrey

Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  12:34:49 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

import originpro as op
import numpy as np
from scipy.optimize import curve_fit

wks = op.find_sheet()

def func(x, a, b, c):
    return a + (1 - a) * (b/x)**(1/c)
    
def fit(i, help):
    '''f:i'''
    x = np.linspace(1, 5, 6)
    y = wks.to_list2(i,i,0,5)
    popt, pcov = curve_fit(func, x, np.transpose(y)[0])
    return popt[0]




Few questions on the code.
1) Is help parameter of fit function redundant and can be removed?

---
Andrey

Edited by - AKazak on 07/01/2022 12:42:06 PM
Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  12:43:35 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

I don't find a way to do so with Labtalk. Looks to me the input of nlbegin has to be columns.

James



nlbegin is a good option, but it does not output the fitting parameters to a worksheet, but to report sheet only.
So I will continue using Python approach.

---
Andrey
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/01/2022 :  1:43:07 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Yes, you can remove help parameter (the input column). It is of not much use in this case as the input columns are not passed by the function arguments.

James

quote:
Originally posted by AKazak

quote:
Originally posted by YimingChen

import originpro as op
import numpy as np
from scipy.optimize import curve_fit

wks = op.find_sheet()

def func(x, a, b, c):
    return a + (1 - a) * (b/x)**(1/c)
    
def fit(i, help):
    '''f:i'''
    x = np.linspace(1, 5, 6)
    y = wks.to_list2(i,i,0,5)
    popt, pcov = curve_fit(func, x, np.transpose(y)[0])
    return popt[0]




Few questions on the code.
1) Is help parameter of fit function redundant and can be removed?

---
Andrey

---
Andrey

Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/01/2022 :  2:43:14 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

Yes, you can remove help parameter (the input column). It is of not much use in this case as the input columns are not passed by the function arguments.

James



How do I pass the input columns by the function arguments to make the function much more universal?

---
Andrey
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/01/2022 :  3:29:53 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
It is not possible in this case as the input columns can vary. So I have to use wks.to_list2() Python function to extract data from each worksheet row.

James

Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/02/2022 :  01:21:14 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

It is not possible in this case as the input columns can vary. So I have to use wks.to_list2() Python function to extract data from each worksheet row.

James



Dear James,

OK, got it.

Another question on Python.
I noticed that column formulas for columns L, M and Y are different in parts of Python scripts since each columns return its own component of popt vector. Does this mean that for each row fit(i, help) function is being called three times?
If this is a case, then it seems like wasting the resources.
Is this possible to fill columns L, M and Y with a single run of fit(i, help) function per each row?

---
Andrey
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/05/2022 :  09:07:24 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
Yes, you are right. the fitting function is called 3 times. Please see the updated project file. The fitted values are output in one run.

https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise1.opju

James
Go to Top of Page

AKazak

Russia
1205 Posts

Posted - 07/05/2022 :  10:23:51 AM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
quote:
Originally posted by YimingChen

Yes, you are right. the fitting function is called 3 times. Please see the updated project file. The fitted values are output in one run.

https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise1.opju

James



Dear James,

What is the purpose of passing the col(a) to the function:
py.fit(col(A))


For me it seems that py.fit was designed in such a way that it does not require any inputs, doesn't it?

How do I modify py.fit to depend on the source data columns A:F?

---
Andrey

Edited by - AKazak on 07/05/2022 10:24:16 AM
Go to Top of Page

YimingChen

1664 Posts

Posted - 07/05/2022 :  3:08:13 PM  Show Profile  Edit Reply  Reply with Quote  View user's IP address  Delete Reply
If you want the recalculation to be triggered by all the input columns, you need to set all those columns as the arguments of the Python function. Check the updated project file.
https://my.originlab.com/ftp/forum_and_kbase/Images/fitRowWise2.opju

James
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