Finding the best fitting function

5 visualizaciones (últimos 30 días)
Tien Tran
Tien Tran el 23 de Abr. de 2016
Comentada: Tien Tran el 23 de Abr. de 2016
I see a question on Mathwork: " I have 4 data points that I have plotted and are supposed to yield a cosine wave. How do I go about finding the best fit for this cosine wave? data = [1 4.2101; 2 -33.0595; 3 -5.6488; 4 76.2462]"
Star Strider have answered with a good solution that:
data = [1 4.2101; 2 -33.0595; 3 -5.6488; 4 76.2462];
x = data(:,1);
y = data(:,2);
yu = max(y);
yl = min(y);
yr = (yu-yl); % Range of ‘y’
yz = y-yu+(yr/2);
zx = x(yz(:) .* circshift(yz(:),[1 0]) <= 0); % Find zero-crossings
per = 2*mean(diff(zx)); % Estimate period
ym = mean(y); % Estimate offset
fit = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4); % Function to fit
fcn = @(b) sum((fit(b,x) - y).^2); % Least-Squares cost function
s = fminsearch(fcn, [yr; per; -1; ym]) % Minimise Least-Squares
xp = linspace(min(x),max(x));
figure(1)
plot(x,y,'b', xp,fit(s,xp), 'r')
grid
=> s = [174.092; 10.138; -0.6873; 140.8256];
my question is how to find the fitting function? because I try to replace 's' constant and x into 'fit' function, it not correct with y

Respuesta aceptada

Star Strider
Star Strider el 23 de Abr. de 2016
I am lost. What data are you trying to fit with my sinusoidal function?
In my code, there are some parts that may seem irrelevant but are nevertheless important.
The model or ‘objective’ function (that describes the process you want to model) is:
fit = @(b,x) b(1).*(sin(2*pi*x./b(2) + 2*pi/b(3))) + b(4); % Function to fit
the sum-of-squared-error cost function is:
fcn = @(b) sum((fit(b,x) - y).^2); % Sum-Squared-Error Cost Function
and the error minimsation function is:
[s, SSE] = fminsearch(fcn, [yr; per; -1; ym]) % Minimise Sum-of-Squares for a 4-parameter system
A 4-parameter system requires at least 4 data points to accurately model it.
If you have a different model in in mind, please describe your data, the process that produced your data, and the model you would like to apply to it. Not all modeling efforts are successful for a variety of reasons, so I will need to know how you acquired your data, and what process created it. The purpose of modeling is to identify particular parameters that, in the context of the model, describe the system dynamics.
i will be glad to help you as I can, but I need to know what you want to do.
Having a sample of your data would definitely help. This code may be appropriate for my present model or may not. We won’t know until we see it. I will need to know the process that created your data in order to develop a function that describes it, if at all possible. I will need at least as many data points as there are parameters you would like fitted to the function.
Nota Bene: I cannot guarantee that the fit will be successful. The best we can do is to give it our best effort.
  3 comentarios
Star Strider
Star Strider el 23 de Abr. de 2016
Editada: Star Strider el 23 de Abr. de 2016
My pleasure.
You have to decide on the function yourself. It should be obvious from the process that created your data. (This sounds like the ideal gas law or something similar, so it should be relatively easy.)
EDIT If you have an equation you want to fit to your data, I will do my best to help you code it. I have no idea what you are doing. I cannot help you write it until I do. (15.44 GMT 2016-04-23)
Tien Tran
Tien Tran el 23 de Abr. de 2016
Thank Star
I will send it as a new question

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Get Started with Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by