curve-fitting

Using scipy curve_fit for a variable number of parameters

大憨熊 提交于 2019-11-28 13:59:46
I have a fitting function which has the form: def fit_func(x_data, a, b, c, N) where a, b, c are lists of lenth N, every entry of which is a variable parameter to be optimized in scipy.optimize.curve_fit(), and N is a fixed number used for loop index control. Following this question I think I am able to fix N, but I currently am calling curve_fit as follows: params_0 = [a_init, b_init, c_init] popt, pcov = curve_fit(lambda x, a, b, c: fit_func(x, a, b, c, N), x_data, y_data, p0=params_0) I get an error: lambda() takes exactly Q arguments (P given) where Q and P vary depending on how I am

Fitting piecewise function in Python

只愿长相守 提交于 2019-11-28 12:32:56
I'm trying to fit a piecewise defined function to a data set in Python. I've searched for quite a while now, but I haven't found an answer whether it is possible or not. To get an impression of what I am trying to do, look at the following example (which is not working for me). Here I'm trying to fit a shifted absolute value function (f(x) = |x-p|) to a dataset with p as the fit parameter. import scipy.optimize as so import numpy as np def fitfunc(x,p): if x>p: return x-p else: return -(x-p) fitfunc = np.vectorize(fitfunc) #vectorize so you can use func with array x=np.arange(1,10) y=fitfunc(x

Approximation of n points to the curve with the best fit

我与影子孤独终老i 提交于 2019-11-28 11:34:45
问题 I have a list of n points(2D): P1(x0,y0), P2(x1,y1), P3(x2,y2) … Points satisfy the condition that each point has unique coordinates and also the coordinates of each point xi, yi> 0 and xi,yi are integers. The task is to write an algorithm which make approximation of these points to the curve y = | Acos (Bx) | with the best fit (close or equal to 100%) and so that the coefficients A and B were as simple as possible. I would like to write a program in C # but the biggest problem for me is to

Interpolating a closed curve using scipy

会有一股神秘感。 提交于 2019-11-28 11:15:59
I'm writing a python script to interpolate a given set of points with splines. The points are defined by their [x, y] coordinates. I tried to use this code: x = np.array([23, 24, 24, 25, 25]) y = np.array([13, 12, 13, 12, 13]) tck, u = scipy.interpolate.splprep([x,y], s=0) unew = np.arange(0, 1.00, 0.005) out = scipy.interpolate.splev(unew, tck) which gives me a curve like this: However, I need to have a smooth closed curve - on the picture above the derivatives at one of the points are obviously not the same. How can I achieve this? ali_m Your closed path can be considered as a parametric

Use of curve_fit to fit data

自古美人都是妖i 提交于 2019-11-28 11:10:54
I'm new to scipy and matplotlib, and I've been trying to fit functions to data. The first example in the Scipy Cookbook works fantastically, but when I am trying it with points read from a file, the initial coefficients I give (p0 below) never seem to actually change, and the covariance matrix is always INF. I've tried to fit even data following a line, to no avail. Is it a problem with the way I am importing the data? If so, is there a better way to do it? import matplotlib.pyplot as plt from scipy.optimize import curve_fit import scipy as sy with open('data.dat') as f: noms = f.readline()

How to fit a gaussian to a histogram in R? [duplicate]

家住魔仙堡 提交于 2019-11-28 11:01:32
问题 This question already has answers here : Closed 6 years ago . Possible Duplicate: Fitting a density curve to a histogram in R best fitting curve from plot in R I have a set of data that when plotted in a histogram is a Gaussian distribution. I have read the data in from a .csv file that looks like this: "values" 1.29989 1.15652 1.27818 1.19699 1.28243 1.19433 1.10991 ... using data<-read.csv("~/peak.csv") ... so I create a histogram using hist(data$values) . I want to be able to fit a

Forcing nls to fit a curve passing through a specified point

橙三吉。 提交于 2019-11-28 10:41:48
问题 I'm trying to fit a Boltzmann sigmoid 1/(1+exp((x-p1)/p2)) to this small experimental dataset: xdata <- c(-60,-50,-40,-30,-20,-10,-0,10) ydata <- c(0.04, 0.09, 0.38, 0.63, 0.79, 1, 0.83, 0.56) I know that it is pretty simple to do it. For example, using nls : fit <-nls(ydata ~ 1/(1+exp((xdata-p1)/p2)),start=list(p1=mean(xdata),p2=-5)) I get the following results: Formula: ydata ~ 1/(1 + exp((xdata - p1)/p2)) Parameters: Estimate Std. Error t value Pr(>|t|) p1 -33.671 4.755 -7.081 0.000398 ***

How to fit a function with multiple files in gnuplot

时光怂恿深爱的人放手 提交于 2019-11-28 09:53:18
问题 I would like to use the fit command for multiple files in gnuplot. I know that for one file the command is for example: f(x)=a*x+b fit f(x) 'file1' u ($18/-200):($4/200) via a, b Now instead of a single file I would like to have multiple ones ('file1','file2','file3'etc.) and find the best a, b, parameters that fit all data sets. This question is similar to this one but with different files. Thanks! 回答1: Like in the question you linked, also here you must combine all files to a single one.

How to use least squares method in Matlab?

大兔子大兔子 提交于 2019-11-28 09:10:11
问题 I have 37 linear equations and 36 variables in the form of a matrix equation; A*X=B . The equations don't have an exact answer. I want to use Matlab least square method to find the answers with the least error. I am new to Matlab so any comments will help. Thank you 回答1: If A is of full rank, i.e. the columns of A are linearly independent, the least-squares solution of an overdetermined system of linear equations A * x = b can be found by inverting the normal equations (see Linear Least

Fitting a curve to specific data

ぐ巨炮叔叔 提交于 2019-11-28 06:07:17
I have the following data in my thesis: 28 45 91 14 102 11 393 5 4492 1.77 I need to fit a curve into this. If I plot it, then this is what I get. I think some kind of exponential curve should fit this data. I am using GNUplot. Can someone tell me what kind of curve will fit this and what initial parameters I can use? Ben Just in case R is an option, here's a sketch of two methods you might use. First method: evaluate the goodness of fit of a set of candidate models This is probably the best way as it takes advantage of what you might already know or expect about the relationship between the