Generating Non Linear Equation

3 views (last 30 days)
ElevenFourth
ElevenFourth on 4 Jun 2013
Hi all !
I am going to determine the value of a and b of the equation as follows :
y = 1 - ax - bx^2
where, a + b = 1
I also have a set of data to be fitted by the above equation. How can I do this with matlab?
Your guidance and help would be highly appreciated.

Answers (2)

the cyclist
the cyclist on 4 Jun 2013
If you have the Statistics Toolbox, you can do this with nlinfit().
% Generate some pretend data to be fit
x=(0:1:10)'; % Explanatory variable
y = 1 - 0.3*x - 0.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) 1 - F.*x - (1-F).*x.^2;
F_fitted = nlinfit(x,y,f,[1]);
% Display fitted coefficients
disp(['F = ',num2str(F_fitted)])
% Plot the data and fit
figure
plot(x,y,'*',x,f(F_fitted,x),'g');
legend('data','fit')

Roger Stafford
Roger Stafford on 4 Jun 2013
Edited: Roger Stafford on 4 Jun 2013
It depends on what you want to minimize in your "fitting". To get the least squares difference between y and the above expression, do this:
a = sum((1-y-x.^2).*(x-x.^2))/sum((x-x.*2).^2);
where x and y are vectors of the given data.
You can derive this by setting the partial derivative with respect to 'a' equal to zero and solving for 'a' for the expression
sum((y-1+a*x+b*x^2)^2)
where b is set to 1-a.

Community Treasure Hunt

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

Start Hunting!