curve-fitting

SciPy Curve Fit Fails Power Law

前提是你 提交于 2019-12-18 03:39:13
问题 So, I'm trying to fit a set of data with a power law of the following kind: def f(x,N,a): # Power law fit if a >0: return N*x**(-a) else: return 10.**300 par,cov = scipy.optimize.curve_fit(f,data,time,array([10**(-7),1.2])) where the else condition is just to force a to be positive. Using scipy.optimize.curve_fit yields an awful fit (green line), returning values of 1.2e+04 and 1.9e0-7 for N and a, respectively, with absolutely no intersection with the data. From fits I've put in manually,

Optimize constants in differential equations in Python

假装没事ソ 提交于 2019-12-18 03:01:07
问题 Okay so how would i approach to writing a code to optimize the constants a and b in a differential equation, like dy/dt = a*y^2 + b, using curve_fit? I would be using odeint to solve the ODE and then curve_fit to optimize a and b. If you could please provide input on this situation i would greatly appreciate it! 回答1: You might be better served by looking at ODEs with Sympy. Scipy/Numpy are fundamentally numerical packages and aren't really set up to do algebraic/symbolic operations. 回答2: You

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

半腔热情 提交于 2019-12-17 22:18:04
问题 I've been trying to fit an exponential to some data for a while using scipy.optimize.curve_fit but i'm having real difficulty. I really can't see any reason why this wouldn't work but it just produces a strait line, no idea why! Any help would be much appreciated from __future__ import division import numpy from scipy.optimize import curve_fit import matplotlib.pyplot as pyplot def func(x,a,b,c): return a*numpy.exp(-b*x)-c yData = numpy.load('yData.npy') xData = numpy.load('xData.npy') trialX

Fitting piecewise function in Python

霸气de小男生 提交于 2019-12-17 20:23:43
问题 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)

Fitting a curve to specific data

纵饮孤独 提交于 2019-12-17 17:54:08
问题 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? 回答1: 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

Curve fitting with y points on repeated x positions (Galaxy Spiral arms)

匆匆过客 提交于 2019-12-17 16:53:10
问题 I currently have a MATLAB program which takes RGB images of traced spiral arms from galaxies and selects the biggest arm component and plots only that. I have tried using matlab's built in curve fitting tool with smoothing spline to fit it and I get the following result: I have tried using interp1 with parametric fitting to only get bad results. Is there a way to fit this type of curve at all? 回答1: Your fail is due to that you handle your 2D curve as function which is not the case (you got

Fitting data using UnivariateSpline in scipy python

半腔热情 提交于 2019-12-17 16:07:03
问题 I have a experimental data to which I am trying to fit a curve using UnivariateSpline function in scipy. The data looks like: x y 13 2.404070 12 1.588134 11 1.760112 10 1.771360 09 1.860087 08 1.955789 07 1.910408 06 1.655911 05 1.778952 04 2.624719 03 1.698099 02 3.022607 01 3.303135 Here is what I am doing: import matplotlib.pyplot as plt from scipy import interpolate yinterp = interpolate.UnivariateSpline(x, y, s = 5e8)(x) plt.plot(x, y, 'bo', label = 'Original') plt.plot(x, yinterp, 'r',

Smooth spline representation of an arbitrary contour, f(length) --> x,y

纵然是瞬间 提交于 2019-12-17 15:35:46
问题 Suppose I have a set of x,y coordinates that mark points along contour. Is there a way that I can build a spline representation of the contour that I can evaluate at a particular position along its length and recover interpolated x,y coordinates? It is often not the case that there is a 1:1 correspondence between X and Y values, so univariate splines are no good to me. Bivariate splines would be fine, but as far as I can tell all of the functions for evaluating bivariate splines in scipy

Curve Fitting to a time series in the format 'datetime'?

柔情痞子 提交于 2019-12-17 12:25:55
问题 Here is my problem: polyfit does not take datetime values, so that I converted datetime with mktime producing the polynomial fit works z4 = polyfit(d, y, 3) p4 = poly1d(z4) For the plot however, I would like the datetime description on the axis and didn't # figure out how to do that. Can you help me? fig = plt.figure(1) cx= fig.add_subplot(111) xx = linspace(0, d[3], 100) pylab.plot(d, y, '+', xx, p4(xx),'-g') cx.plot(d, y,'+', color= 'b', label='blub') plt.errorbar(d, y, yerr, marker='.',

How to calculate the vertex of a parabola given three points

故事扮演 提交于 2019-12-17 09:30:43
问题 I have three X/Y points that form a parabola. I simply need to calculate what the vertex of the parabola is that goes through these three points. Preferably a quick way as I have to do a LOT of these calculations! The "Ask A Scientist" website provides this answer: The general form of a parabola is given by the equation: A * x^2 + B * x + C = y where A, B, and C are arbitrary Real constants. You have three pairs of points that are (x,y) ordered pairs. Substitute the x and y values of each