Matlab sine curve fitting with linear increase - python

I have multiple sets of data, each of which have a roughly sinusoidal shape. I have attached a photo of one such data Roughly sinusoidal data
I want to fit an equation of the form y = Asin(kx+b)+mx+c. Basically the data is a sine wave with a linear increase. Is there any simple way of doing this, preferably without toolboxes as I have about 20 of these data to sort through?
Thank you.
I am using matlab, however I am open to python too :).

What you want to do is to use regression in your function to fit the data.
One way to do this is to apply Non-linear least squares to find the parameters that minimize the square error for your function.
Take a look at the matlab function lsqcurvefit (http://es.mathworks.com/help/optim/ug/lsqcurvefit.html)

Related

use FFT spectrum to define a lambda function

my problem is the following: I have N different measurements of a quantity which depends on two other quantities that I also know. I would like to use find a function two variable function that approximates the data, and I thought that using Fourier transforms was a nice idea.
Does anybody has a suggestion on how should I proceed? I think as a first step I want to do a FFT of my data, but then how can I implement the inverse FT not only for the points where I measured but for any pair (x,y) as input?
Thanks a lot.
(I am using python).

How would I calculate a continuous PDF from discrete data using Python?

I have some discrete data of the beta spectrum of Bismuth-210, which I would like to try to fit a continuous PDF to so I can improve the accuracy of a simulation I'm building. Is there anything in Python that allows me to input discrete PDF data and output a function for a continuous distribution? I'm looking to be able to derive a mathematical function, i.e. f(x)= ax+b (obviously not actually of this form, but you get the point). I haven't been able to try too much because I'm so lost as to where to start, but I tried creating a piecewise function of the data, which is not sufficiently smooth (nor succinct) for my purposes.

Curve Fitting in Python for extrapolation, Regression analysis

This question is regarding curve fitting in python.
First, I would say that I do not know the curve fit function to insert into "curve_fit" function in the scipy library; therefore, I am trying to use a polyfit which is OK if I am interested in interpolation but my goal is to predict values at future points, in other words extrapolation.
I have attached a screenshot of a raw signal, smoothed and its polyfit result. It has the correct poly order but still fails at extrapolation. My conclusion is that poly fit is not the right approach here, but I can not estimate the curve function. What are you thoughts?
Please note that this is not a distribution since the y values may keep slowly decreasing infinitely, even below 0.
I'd say the function looks like an exponential Gaussian but again it's not a distribution so dont want to do that.
My last thought was to split the plot into two, the first model can certainly be modeled as a polynomial and the second as an exponential. (values are different than first png cuz it's of a different signal).
Then, maybe combine the two. What do you think about this?
Attached is a screenshot of this too.
Since many curves can fit the data and extrapolate differently, you need to choose the right basis functions to get the behaviour you want.
So far you have tried polynomials for instance, these however tend to +- infinite, which is perhaps not what you want.
I would try and use curve_fit on a sum of Hermite polynomials or Laguerre polynomials. For instance, for Laguerre polynomials, you could try
a + b*exp(-k x) + c*(1-x)*exp(-k x) + d*(x^2 - 4*x + 2)*exp(-k x) + ...
Python has a lot of convenience functions built in for this, see e.g. https://docs.scipy.org/doc/numpy-1.13.0/reference/routines.polynomials.laguerre.html
Note however that you should also fit k to your data, which you could use curve_fit for.

piecewise linear regression python: arbitrary amount of knots

I have an experimental data, which is piecewise continuous, and each part should fit linearly. However, I would like to fit it without knowing where exactly are the knots (so the points where the slope is changing), since its not easy to determine them manually.
So far I found the advice of using py-earth for this task, but couldn't understand how to implement it; that is, having just a list of variables for X and Y, how to be able to do such piecewise linear regression.
Could anybody give me advice on how to do that?
UPD: Turned out, the problem was because of the different format of array. "X=numpy.array([X]).T" for my arrays solved it, and now py_earth is working. However, it's to "rough", showing one single line for several knots. Can anybody suggest some other solution for the piecewise linear regression?

How calculate the Error for trapezoidal rule if I only have data? (Python)

I got this array of data and I need to calculate the area under the curve, so I use the Numpy library and the Scipy library which contain the functions trapz in Numpy and integrate.simps in Scipy for a Numerical Integration which gave me a really nice result in both cases.
The problem now is, that I need the error for each one or at least the error for the Trapezoidal Rule. The thing is, that the formula for that ask me a function, which obviously I don't have. I have been researching for a way to obtain the error but always return to the same point...
Here are the pages of scipy.integrate http://docs.scipy.org/doc/scipy/reference/integrate.html and trapz in Numpy http://docs.scipy.org/doc/numpy/reference/generated/numpy.trapz.html I try and see a lot of code about the Numerical Integration and prefer to use the existing ones...
Any ideas please?
While cel is right that you cannot determine an integration error if you don't know the function, there is something you can do.
You can use curve fitting to fit a function through the available data points. You can then use that function for error estimation.
If you expect the data to fit a certain kind of function like a sine, log or exponential it is good to use that as a basis for curve fitting.
For instance, if you are measuring the drag on a moving car, it is known that this mostly proportional to the velocity squared because of air resistance.
However, if you do not have any knowledge about the applicable function then assuming you have N data points, there is a polynomial of the N-1 degree that fits exactly though all those data points. Determining such a polynomial from the data is solving a system of lineair equations. See e.g. polynomial interpolation. You could use this polynomial as an estimate for the unknown real function. Note however that outside the range of data points this polynomial might be wildly inaccurate.

Categories

Resources