I want to fit two functions at the same time with python.

Asked 1 years ago, Updated 1 years ago, 60 views

I am using python 3.6.The following code is a two-dimensional graph of data_x and data1 and a two-dimensional graph of data_x and data2 are fitted with different functions.

import matplotlib.pyplot as plt
import scipy as sp
from scope.optimize import curve_fit

data_x = [3.15571008, 3.1066576, 3.05910671, 3.01298953, 2.96824216, 2.92480447, 2.88261979, 2.84163467, 2.80179867, 2.76306413, 2.72538598, 2.6887216, 2.66530306, 2.61827474, 2.584417, 2.5525]
data1 = [3.50054094, 4.16089732, 4.98809876, 6.04311513, 7.33692531, 8.81495875, 10.2995666, 11.43818035, 11.81403109, 11.24183674, 9.90566277, 8.29082274, 6.75265875, 5.44632354, 4.3975, 3.572575, 3.571648]
data2 = [1.27224069, 1.56148443, 1.93069611, 2.40000859, 2.98302372, 3.66684273, 4.38099848, 4.97231414, 5.24594599, 5.09652223, 4.61044296, 3.96359503, 3.3131098, 2.74026653, 2.267126, 1.88411]

plt.plot(data_x,data1,"o")
plt.figure()
plt.plot(data_x,data2,"o")

A_initial=1
f_initial=1
m_initial = 1
r_initial = 0.5
w0_initial=2.7
B_initial=1

parameter_initial1 = np.array([A_initial, f_initial, m_initial, r_initial, w0_initial])
parameter_initial2 = np.array([B_initial, f_initial, m_initial, r_initial, w0_initial])

def sc_fit(w, A, F, m, r, w0):
    return A*F/(m*(w0**2-w**2)**2+4*r**2*w**2)**(1/2))
param_sc, cov=sp.optimize.curve_fit(sc_fit, data_x, data1, p0=parameter_initial1)

defab_fit(w,B,F,m,r,w0):
    return B*F**2/(4*m*r)*1/(1+(w0**2-w**2)/(2*r*w))**2)
param_ab, cov=sp.optimize.curve_fit(ab_fit, data_x, data2, p0=parameter_initial2)

freq = np.range (2.5, 3.2, 0.01)
y1 = sc_fit(freq, param_sc[0], param_sc[1], param_sc[2], param_sc[3], param_sc[4])
y2 = ab_fit(freq, param_ab[0], param_ab[1], param_ab[2], param_ab[3], param_ab[4])

plt.figure()
plt.plot(data_x,data1,"o")
plt.plot(freq,y1)

plt.figure()
plt.plot(data_x,data2,"o")
plt.plot(freq,y2)

This allows the two functions to be fitted.However, the fitting parameters F, m, r, and w0 are naturally different because the two fitting parameters are done separately.A and B can be different values, but I would like to fit the other 4 (F, m, r, w0) with the same values.If you fix the fitting parameters you found in the first fitting and make the second fitting, it won't fit at all, so you have to adjust the two fittings, but I don't know what to do at all.

python scipy

2022-09-30 21:32

1 Answers

Since they are two independent and different graphs, it is natural that the functions fitted to them are different.

It is possible to fit both graphs with one function, but it does not match the "best fit" of each of the two graphs.

In other words, if the function "not the best" (it doesn't matter if it's off, wrong, or irresponsible), you can "fit both graphs with one function."
 However, I wonder if it's meaningful to ask for something like that.

Deepening Toma's problem raising may be significant in the mathematical society and may be awarded a Fields Award by recalling and solving various lemma problems from the original problem, such as "defining how to measure the deviation from the best and fitting two graphs with one function."
 I pray that a unique solution will be created.


2022-09-30 21:32

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.