Fitting ad hoc function to data using MatLab

Dear all,
I have the following data:
x=[0 0 0 11 14 14 19 19 26 37 47 47 53 53 63 74 81 81 86 86 89 100 100 100];
y=[33 33 27 32 32 26 43 38 42 42 45 40 51 43 73 61 52 67 69 79 89 60 86 75];
I have plotted these data using
plot (x, y,'o')
Now I would like to fit the function of the form:
f=(p^(gamma))/(p^gamma+(1-p))^(1/gamma)
to the data. p(i) is equal to x(i). gamma is a variable.
Basically,
(1) I need to find gamma which produces the best fit of the function f (specified above) to the data;
(2) I need to plot f against the data (on the same graph).
I am sure this can be done but for some reason cannot come up with a good algorithm. Could you please suggest some solutions?
Thank you,
Ganna

 Akzeptierte Antwort

the cyclist
the cyclist am 15 Jul. 2012
Bearbeitet: the cyclist am 15 Jul. 2012

0 Stimmen

If you have the Statistics Toolbox, you can use the function nlinfit() to do this.
Here is a simple example of its use:
function [] = nlinfitExample()
% Here is an example of using nlinfit(). For simplicity, none of
% of the fitted parameters are actually nonlinear!
% Define the data to be fit
x=(0:1:10)'; % Explanatory variable
y = 5 + 3*x + 7*x.^2; % Response variable (if response were perfect)
y = y + 2*randn((size(x)));% Add some noise to response variable
% Define function that will be used to fit data
% (F is a vector of fitting parameters)
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
F_fitted = nlinfit(x,y,f,[1 1 1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure(1)
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')
end

2 Kommentare

Ganna
Ganna am 15 Jul. 2012
Thank you so much for the quick reply. The problem is that my fitting parameter (gamma) is a scalar and what i need to find out is the y=f(x gamma). In your example, it is y = 5 + 3*x + 7*x.^2. My problem is that I know the function I am fitting, but need to estimate gamma first. I have tried to use a vector of values
y=[33 33 27 32 32 26 43 38 42 42 45 40 51 43 73 61 52 67 69 79 89 60 86 75];
instead of y=f(x) as in your example
but I can't make it work.
Basically, I have two vectors: one is x another is y. My y=(x^(gamma))/(x^gamma+(1-x))^(1/gamma) where gamma is a scalar.
I need to find gamma.
Sorry, this is probably completely stupid but for some reason I can't make it work.
Thank again,
Ganna
the cyclist
the cyclist am 16 Jul. 2012
Bearbeitet: the cyclist am 16 Jul. 2012
You have not fully understood my example. Specifically, you have confused the few lines of code that I used to generate some random data (e.g. y = 5 + ...), with the part of the code that actually does the fitting.
So, in the section where I comment "% Define the data to be fit", you already have that. Those are the (x,y) data you already have.
Where I have written
f = @(F,x) F(1) + F(2).*x + F(3).*x.^2;
that is where you will define your own functional form. Your gamma will take the place of my F (and you will not need three parameters like I have in my example). So, I think it will be
f = @(Gamma,x) x.^Gamma % etc ...
Then where I have
F_fitted = nlinfit(x,y,f,[1 1 1]);
you will have the same thing, except you will substitute just one starting guess for gamma (instead of three starting guesses for my three-element vector F.)
I hope that helps.

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Community Treasure Hunt

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

Start Hunting!

Translated by