curve-fitting

R : catching errors in `nls`

主宰稳场 提交于 2019-11-30 09:59:12
I'm fitting some exponential data using nls . The code I'm using is: fit <- nls(y ~ expFit(times, A, tau, C), start = c(A=100, tau=-3, C=0)) expFit is defined as expFit <- function(t, A, tau, C) { expFit <- A*(exp(-t/tau))+C } This works well for most of my data, for which the starting parameters provided (100, -3 and 0) work well. Sometimes, though, I have data that doesn't go well with those parameters and I get errors from nls (e.g. "singular gradient" or things like that). How do I "catch" these errors? I tried to do something like fit <- NULL fit <- nls(...) if (is.null(fit)) { // Try nls

How to fit the 2D scatter data with a line with C++

可紊 提交于 2019-11-30 08:37:09
问题 I used to work with MATLAB, and for the question I raised I can use p = polyfit(x,y,1) to estimate the best fit line for the scatter data in a plate. I was wondering which resources I can rely on to implement the line fitting algorithm with C++. I understand there are a lot of algorithms for this subject, and for me I expect the algorithm should be fast and meantime it can obtain the comparable accuracy of polyfit function in MATLAB. 回答1: I would suggest coding it from scratch. It is a very

When to choose nls() over loess()?

若如初见. 提交于 2019-11-30 06:59:03
If I have some (x,y) data, I can easily draw straight-line through it, e.g. f=glm(y~x) plot(x,y) lines(x,f$fitted.values) But for curvy data I want a curvy line. It seems loess() can be used: f=loess(y~x) plot(x,y) lines(x,f$fitted) This question has evolved as I've typed and researched it. I started off with wanting to a simple function to fit curvy data (where I know nothing about the data), and wanting to understand how to use nls() or optim() to do that. That was what everyone seemed to be suggesting in similar questions I found. But now I stumbled upon loess() I'm happy. So, now my

How can I get a cubic bezier curve closest to given points?

℡╲_俬逩灬. 提交于 2019-11-30 06:54:30
Given n points: p0, p1, p2, ..., pn; How can I get the point c1, c2 so that the cubic bezier curve defined by p0, c1, c2, pn closest to the given points? I tried least square method. I wrote this after I read the pdf document in http://www.mathworks.com/matlabcentral/fileexchange/15542-cubic-bezier-least-square-fitting . But I can't find a good t(i) function. using System; using System.Collections.Generic; using System.Linq; using System.Windows; namespace BezierFitting { class CubicBezierFittingCalculator { private List<Point> data; public CubicBezierFittingCalculator(List<Point> data) { this

Create scipy curve fitting definitions for fourier series dynamically

喜你入骨 提交于 2019-11-30 05:44:48
问题 I'd like to achieve a fourier series development for a x-y-dataset using numpy and scipy . At first I want to fit my data with the first 8 cosines and plot additionally only the first harmonic . So I wrote the following two function defintions: # fourier series defintions tau = 0.045 def fourier8(x, a1, a2, a3, a4, a5, a6, a7, a8): return a1 * np.cos(1 * np.pi / tau * x) + \ a2 * np.cos(2 * np.pi / tau * x) + \ a3 * np.cos(3 * np.pi / tau * x) + \ a4 * np.cos(4 * np.pi / tau * x) + \ a5 * np

Python / Scipy - implementing optimize.curve_fit 's sigma into optimize.leastsq

北城以北 提交于 2019-11-30 05:20:03
问题 I am fitting data points using a logistic model. As I sometimes have data with a ydata error, I first used curve_fit and its sigma argument to include my individual standard deviations in the fit. Now I switched to leastsq, because I needed also some Goodness of Fit estimation that curve_fit could not provide. Everything works well, but now I miss the possibility to weigh the least sqares as "sigma" does with curve_fit. Has someone some code example as to how I could weight the least squares

6th degree curve fitting with numpy/scipy

∥☆過路亽.° 提交于 2019-11-30 04:00:40
I have a very specific requirement for interpolating nonlinear data using a 6th degree polynomial. I've seen numpy/scipy routines (scipy.interpolate.InterpolatedUnivariateSpline) that allow interpolation only up to degree 5. Even if there's no direct function to do this, is there a way to replicate Excel's LINEST linear regression algorithm in Python? LINEST allows 6th degree curve-fitting but I do NOT want to use Excel for anything as this calculation is part of a much larger Python script. Any help would be appreciated! Use numpys polyfit routine. http://docs.scipy.org/doc/numpy-1.3.x

Playing perfect Tetris: how to align and scale two curves using scaling and translation?

允我心安 提交于 2019-11-30 02:41:44
问题 Given parameters of scaling on the y axis (s) and translation on the x axis (t), how to scale and align two curves that do not coincide when the purpose is to maximize curve superposition (not minimize distance)? As indicated by @DWin, this could be rebranded "How to play Tetris perfectly with R", though it does have applications far beyond winning a Tetris game. A variation of this question could involve any number of rigid body transformations (rotation, translation and scaling). Given

Correct fitting with scipy curve_fit including errors in x?

佐手、 提交于 2019-11-30 00:23:39
I'm trying to fit a histogram with some data in it using scipy.optimize.curve_fit . If I want to add an error in y , I can simply do so by applying a weight to the fit. But how to apply the error in x (i. e. the error due to binning in case of histograms)? My question also applies to errors in x when making a linear regression with curve_fit or polyfit ; I know how to add errors in y , but not in x . Here an example (partly from the matplotlib documentation ): import numpy as np import pylab as P from scipy.optimize import curve_fit # create the data histogram mu, sigma = 200, 25 x = mu +

Using OpenCV fitEllipse() for circle fitting

醉酒当歌 提交于 2019-11-29 19:43:20
问题 Is it valid to use OpenCV fitEllipse for circle fitting. fitEllipse() returns cv::RotatedRect how about averaging width and height to get fitted circle radius? 回答1: I think that the "validity" of using cv::fitEllipse for fitting circles depends on the precision you require for the fitting. For example you can run your algorithm on a test set, fitting points with cv::fitEllipse and logging the length of the two axes of the ellipse, then have a look at the distributions of the ratio of two axes