curve-fitting

MATLAB curve fitting - least squares method - wrong “fit” using high degrees

喜你入骨 提交于 2019-12-01 10:27:38
Anyone here that could help me with the following problem? The following code calculates the best polynomial fit to a given data-set, that is; a polynomial of a specified degree. Unfortunately, whatever the data-set may be, usually at degree 6 or higher, MATLAB gets a totally wrong fit. Usually the fit curves totally away from the data in a sort of exponantial-looking-manner downwards. (see the example: degree = 8). x=[1.0 1.5 2.0 2.5 3.0 3.5 4.0 4.5] % experimental x-values y=[4.3 6.2 10.1 13.5 19.8 22.6 24.7 29.2] % experimental y-values degree=8; % specify the degree A = zeros(length(x)

Fitting with constraints on derivative Python

蓝咒 提交于 2019-12-01 09:57:18
问题 While trying to create an optimization algorithm, I had to put constraints on the curve fitting of my set. Here is my problem, I have an array : Z = [10.3, 10, 10.2, ...] L = [0, 20, 40, ...] I need to find a function that fits Z with condition on slope which is the derivative of the function I'm looking for. Suppose f is my function, f should fit Z and have a condition on f its derivative, it shouldnt exceed a special value. Are there any libraries in python that can help me achieve this

Problems fitting to boxcar function using scipy's curvefit in python

荒凉一梦 提交于 2019-12-01 08:02:20
问题 I can't get this boxcar fit working...I get " OptimizeWarning: Covariance of the parameters could not be estimated category=OptimizeWarning)" , and the output coefficients are not improved beyond the starting guess. import numpy as np from scipy.optimize import curve_fit def box(x, *p): height, center, width = p return height*(center-width/2 < x)*(x < center+width/2) x = np.linspace(-5,5) y = (-2.5<x)*(x<2.5) + np.random.random(len(x))*.1 coeff, var_matrix = curve_fit(box, x, y, p0=[1,0,2])

MATLAB Curve Fitting (Ellipse-like)

旧城冷巷雨未停 提交于 2019-12-01 06:39:52
I need to fit 10 data points ( x,y ) into this equation: ay² + bxy + cx + dy + e = x² It's told that this is a ellipse-like equation. I can't do it with usual curve fitting tools because it is not really a function (one x corresponds to 2 y s). I can't either use ellipse curve fitting because there is no c*x and d*y in an ellipse equation. Any ideas? Thanks in advance. EDIT: Both Oil and AK4749 gave right answer! Thank you guys! It is a linear system with variables [a b c d e]. You can use \ to solve it: x=rand(10,1); y=rand(10,1); [y.^2,x.*y,x,y,ones(numel(x),1)]\x.^2 ans = -0.4437 %% a 1

Why does scipy.optimize.curve_fit not fit correctly to the data?

倖福魔咒の 提交于 2019-12-01 05:32:35
问题 I've been trying to fit a function to some data for a while using scipy.optimize.curve_fit but I have real difficulty. I really can't see any reason why this wouldn't work. # encoding: utf-8 from __future__ import (print_function, division, unicode_literals, absolute_import, with_statement) import numpy as np from scipy.optimize import curve_fit import matplotlib.pyplot as mpl x, y, e_y = np.loadtxt('data.txt', unpack=True) def f(x, a, k): return (1/(np.sqrt(1 + a*((k-x)**2)))) popt, pcov =

How to compute standard error from ODR results?

元气小坏坏 提交于 2019-12-01 04:22:13
I use scipy.odr in order to make a fit with uncertainties on both x and y following this question Correct fitting with scipy curve_fit including errors in x? After the fit I would like to compute the uncertainties on the parameters. Thus I look at the square root of the diagonal elements of the covariance matrix. I get : >>> print(np.sqrt(np.diag(output.cov_beta))) [ 0.17516591 0.33020487 0.27856021] But in the Output there is also output.sd_beta which is, according to the doc on odr Standard errors of the estimated parameters, of shape (p,). But, it does not give me the same results : >>>

curve fitting with python

一个人想着一个人 提交于 2019-12-01 02:40:59
I'm trying to fit some data and stuff, I know there is a simple command to do this with python/numpy/matplotlib, but I can't find it. I think it is something like popt,popc = numpy.curvefit(f,x) where popt is the paramters of f , popc is the fit quality and f is a predefined function of f. Does any of you know it? Take a look at scipy.optimize.curve_fit : scipy.optimize.curve_fit(f, xdata, ydata, p0=None, sigma=None, **kw) Use non-linear least squares to fit a function, f, to data. Found it . Curve_fit from optimize in scipy 来源: https://stackoverflow.com/questions/8280871/curve-fitting-with

Curve fit fails with exponential but zunzun gets it right

China☆狼群 提交于 2019-12-01 01:39:57
I'm trying to compute the best fit of two forms of an exponential to some x, y data (the data file can be downloaded from here ) Here's the code: from scipy.optimize import curve_fit import numpy as np # Get x,y data data = np.loadtxt('data.txt', unpack=True) xdata, ydata = data[0], data[1] # Define first exponential function def func(x, a, b, c): return a * np.exp(b * x) + c # Get parameters estimate popt, pcov = curve_fit(func, xdata, ydata) print popt # Define second exponential function (one more parameter) def func2(x, a, b, c, d): return a * np.exp(b * x + c) + d # Get parameters

Fitting binned lognormal data in Python

≡放荡痞女 提交于 2019-12-01 01:17:05
I have a range of particle size distribution data arranged by percentage volume fraction, like so:; size % 6.68 0.05 9.92 1.15 etc. I need to fit this data to a lognormal distribution, which I planned to do using python's stats.lognorm.fit function, but this seems to expect the input as an array of variates rather than binned data, judging by what I've read . I was planning to use a for loop to iterate through the data and .extend each size entry to a placeholder array the required number of times to create an array with a list of variates that corresponds to the binned data. This seems really

Dose Response - Global curve fitting using R

半城伤御伤魂 提交于 2019-12-01 00:24:35
I have the following dose response data and wish to plot dose response model and global fit curve. [xdata = drug concentration; ydata(0-5) = response values at different concentrations of the drug]. I plotted the Std Curve without an issue. Std Curve Data fit: df <- data.frame(xdata = c(1000.00,300.00,100.00,30.00,10.00,3.00,1.00,0.30, 0.10,0.03,0.01,0.00), ydata = c(91.8,95.3,100,123,203,620,1210,1520,1510,1520,1590, 1620)) nls.fit <- nls(ydata ~ (ymax*xdata / (ec50 + xdata)) + Ns*xdata + ymin, data=df, start=list(ymax=1624.75, ymin = 91.85, ec50 = 3, Ns = 0.2045514)) Dose Response Curve Data