0

I want to fit a curve to my experimental dataset, and I do not really know how to do it. I have been looking for possibilities, and I came across curve_fit (and also least_suqares), which seems to be up for the task, but I am still very much unfamiliar with how it works, as I struggle to get it into my thick head. I started my attempt by defining the initial values:

import numpy as np import math from scipy.optimize import curve_fit, least_squares f_exp = np.array([1, 1.6, 2.7, 4.4, 7.3, 12, 20, 32, 56, 88, 144, 250000]) e_exp = np.array([7.15, 7.30, 7.20, 7.25, 7.26, 7.28, 7.32, 7.25, 7.35, 7.34, 7.37, 13.55]) n_e_exp = len(e_exp) ezero = 7.15 einf = 13.55 fc = np.arange(1,11000,1000) alpha = np.arange(0,1.1,0.1) log_f_mod = np.arange(-3, 6.5, 0.5) f_mod = 10 ** log_f_mod n_f_mod = len(f_mod) n_fc = len(fc) n_alpha = len(alpha) x = np.zeros((n_f_mod, n_fc)) for j in range(n_f_mod): for k in range(n_fc): x[j,k] = np.log(f_mod[j] / fc[k]) 

Notice that x is function of fc. Now, I define the function I want to run using either curve_fit, least_squares, or some other function that is more suitable:

def c_c_eRI(einf, ezero, alpha, x): eR = einf + 1/2 * (ezero - einf) * (1 - np.sinh((1 - alpha) * x) / (np.cosh((1 - alpha) * x) + np.cos(alpha * math.pi / 2))) eI = np.abs(1/2 * (ezero - einf) * np.cos(alpha * math.pi / 2) / (np.cosh((1 - alpha) * x) + np.sin(alpha * math.pi / 2))) eRI = np.sqrt(eR ** 2 + eI ** 2) return eRI 

At this point, I tried to make it work without any luck by:

fit = curve_fit(c_c_eRI, f_exp, e_exp) 
  • Is there a way to use a function (e.g. curve_fit, least_squares, or some other) to fit the curve to the experimental data and simultaneously provide the value of the independent variables that are alpha and fc (which x is a function of) that are used to achieve the fit itself?

In other words, the aim is to find the values of alpha and fc (which x is a function of) that provides the best possible fit to f_exp versus e_exp in a similar manner as the EXCEL solver finds the minimum squared residuals by varying alpha and fc.

The end goal is to plot f_exp vs. e_exp as well as the fitted curve using matplotlib -- I am also a bit lost on how to do this.

I do apologize for the lack of a more generalized example.

1 Answer 1

1

If I understood your example correctly, I think you just have to change your function definition to

def c_c_eRI(x, einf, ezero, alpha): ... 

From the curve_fit docs: The model function, f(x, …). It must take the independent variable as the first argument and the parameters to fit as separate remaining arguments.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you, @Richard Fitzhugh. I had failed to mention that both alpha and fc (which x is a function of) are independent variables -- I rectified it by editing it as soon as I realized, sorry for that and thank you for pointing it out. Do you by any chance know how I can obtain the values for alpha and fc that are used for the best fit?
After looking at your code, I'm not sure what you're trying to accomplish. It seems to me that you have experimental data for a 1D function (f_exp vs e_exp) and you're trying to fit this 1D experimental data to an equation with 2 independent variables? So like a function plotted over the Cartesian plane? It's not possible to fit 1D data to a 2D function. So I think either you have only 1 independent variable, or you're trying to do something that's not really possible.
If you really want to fit a function with multiple independent variables, here's how to do it, but you need data to match what you're trying to fit.
Thank you, @Richard Fitzhugh. I may very well be trying to do something impossible, and I am also terrible at explaining what I want to do. I thought it would be simple to find the values of fc and alpha that provides the best fit but I guess I am out of my depth.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.