curve-fitting

Dimension issue with scipy's curve_fit function

一世执手 提交于 2019-12-23 17:09:32
问题 I'm new to curve fitting in python, as well as python in general. Currently, I'm trying to use the curve_fit module from scipy to fit 4 spectroscopic peaks. In a few words, I have data in a text file that has two columns. So my first step was to import the data into two arrays, one containing the xdata, the other the y data. Then I tried to define the function that I was going to fit (four voigt peaks). Finally, when I tried to run the whole thing, I get the following error: raise TypeError(

How to fit a curve to a histogram

痴心易碎 提交于 2019-12-23 12:46:48
问题 I've explored similar questions asked about this topic but I am having some trouble producing a nice curve on my histogram. I understand that some people may see this as a duplicate but I haven't found anything currently to help solve my problem. Although the data isn't visible here, here is some variables I am using just so you can see what they represent in the code below. Differences <- subset(Score_Differences, select = Difference, drop = T) m = mean(Differences) std = sqrt(var

How do I constrain a fitted curve through specific points like the origin in MATLAB, also implementing gradient

百般思念 提交于 2019-12-23 04:45:49
问题 I know, I know, that there are several similar Questions already on this and other forums. I read and tried them all...Didn't work for me though. I followed this MATLAB post to solve my problems, here the code x0 = Xh(end,1); %end point of previous curve to add on y0 = fh(end,1); %end point of previous curve to add on x = A.data(co2:end,1); %a 17280 x 1 double of real data (shaky) y = A.data(co2:end,31); %a 17280 x 1 double of real data (shaky) % 'C' is the Vandermonde matrix for 'x' n = 25;

How to fit and plot exponential decay function using ggplot2 and linear approximation

别说谁变了你拦得住时间么 提交于 2019-12-22 10:34:10
问题 I am trying to fit exponential decay functions on data which has only few time points. I would like to use the exponential decay equation y = y0*e^(-r*time) in order to compare r (or eventually half-life) between datasets and factors. I have understood that using a linear fit instead of nls is a better alternative for this particular function [1,2], if I want to estimate the confidence intervals (which I do). Copy this to get some example data: x <- structure(list(Factor = structure(c(3L, 3L,

Tricks for fitting data in nlme?

随声附和 提交于 2019-12-22 10:02:48
问题 When I fit data in nlme, I never succeed on the first try, and after nlme(fit.model) I am accustomed to seeing things such as: Error in nlme.formula(model = mass ~ SSbgf(day, w.max, t.e, t.m), random = list( : step halving factor reduced below minimum in PNLS step Error in MEestimate(nlmeSt, grpShrunk) : Singularity in backsolve at level 0, block 1 So I go back and 1)Change the units of the x-axis (e.g. from years to days, or days to growing degree days). 2)Make a x=0, y=0 measurement in my

Scatter plot kernel smoothing: ksmooth() does not smooth my data at all

僤鯓⒐⒋嵵緔 提交于 2019-12-22 05:30:51
问题 Original question I want to smooth my explanatory variable, something like Speed data of a vehicle, and then use this smoothed values. I searched a lot, and find nothing that directly is my answer. I know how to calculate the kernel density estimation ( density() or KernSmooth::bkde() ) but I don't know then how to calculate the smoothed values of speed. Re-edited question Thanks to @ZheyuanLi, I am able to better explain what I have and what I want to do. So I have re-edited my question as

Scatter plot kernel smoothing: ksmooth() does not smooth my data at all

廉价感情. 提交于 2019-12-22 05:30:42
问题 Original question I want to smooth my explanatory variable, something like Speed data of a vehicle, and then use this smoothed values. I searched a lot, and find nothing that directly is my answer. I know how to calculate the kernel density estimation ( density() or KernSmooth::bkde() ) but I don't know then how to calculate the smoothed values of speed. Re-edited question Thanks to @ZheyuanLi, I am able to better explain what I have and what I want to do. So I have re-edited my question as

Add constraints to scipy.optimize.curve_fit?

微笑、不失礼 提交于 2019-12-22 05:24:42
问题 I have the option to add bounds to sio.curve_fit. Is there a way to expand upon this bounds feature that involves a function of the parameters? In other words, say I have an arbitrary function with two or more unknown constants. And then let's also say that I know the sum of all of these constants is less than 10. Is there a way I can implement this last constraint? import numpy as np import scipy.optimize as sio def f(x, a, b, c): return a*x**2 + b*x + c x = np.linspace(0, 100, 101) y = 2*x*

How to extract equation from a polynomial fit?

心已入冬 提交于 2019-12-22 04:39:22
问题 My goal is to fit some data to a polynomial function and obtain the actual equation including the fitted parameter values. I adapted this example to my data and the outcome is as expected. Here is my code: import numpy as np import matplotlib.pyplot as plt from sklearn.linear_model import Ridge from sklearn.preprocessing import PolynomialFeatures from sklearn.pipeline import make_pipeline x = np.array([0., 4., 9., 12., 16., 20., 24., 27.]) y = np.array([2.9,4.3,66.7,91.4,109.2,114.8,135.5,134

fit using lsqcurvefit

孤者浪人 提交于 2019-12-21 20:45:17
问题 I want to fit some data to a lorentz function but I figure problems with fitting when I use parameters which are of different orders of magnitude. This my lorentz function: function [ value ] = lorentz( x,x0,gamma,amp ) value = amp * gamma^2 ./ ((x-x0).^2 + gamma^2); end Now the script to generate sample data: x = linspace(2e14,6e14,200); x0 = 4.525e14; gamma = 0.5e14; amp = 2e-14; y = lorentz(x,x0,gamma,amp); And the script for fitting lorentz to the sample data: params = [4.475e14;0.4e14;1