Hey guys! I'm some given some huge set of data. I am trying to fit a set of data into a model of functional form as described below:
z(x, y) = c0. * x^0 * y^2 + c1. * x^1 * y^1 + c2. * x^2 *y^1
where c0, c1, c2 are the coefficients to be found.
My attempt is to use the nlinfit function to solve it.
So far I have tried:
% i have just added a small portion of my data
a= [ 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001, 0.001,0.011, 0.011, 0.011, 0.011, 0.011, 0.011, 0.011, 0.011, 0.011, 0.011];
x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
y = x.* a;
z = [ -.304860225, .170315374, .343019354, .370114906, .373180536, .36719579, .363397853, .363417755, .366962504, .379710865, -.304860225, .170315374, .343019354, .370114906, .373180536, .36719579, .363397853, .363417755, .366962504, .379710865];
model= c0.* (x(:).^0).* (y(:).^2) + c1.* (x(:).^1).* (y(:).^1) + c2.* (x(:).^2).* (y(:).^0)
[c0 c1 c2] = [0.001 0.007 0.788]
C= nlinfit( [x,y], z, 'model', [0.001 0.007 0.788])
% Here x,y are independent variables and z is dependent variable.
How can one set these initial values for the coefficients? I'm not getting how to pass the arguments. I'm getting this error "??? Undefined function or variable 'c0' ". Please help!!!
Thanks in advance, Syeda

 채택된 답변

Greg Heath
Greg Heath 2013년 10월 9일

0 개 추천

The solution is trivial because you have a linear system of equations for the 3 coefficients
A*c = b;
c = A\b
Hope this helps
Thank you for formally accepting my answer
Greg

댓글 수: 3

Thankyou Greg! Now, if I put these three co-efficients back into my proposed model. and re-check for random x,y values again. It gives error:
x=1;
y=0.001;
z = 0 * x^0 * y^2 + 0.0000 * x^1 * y^1 + 0.0056 * x^0 * y^2
Any advice?
Matt J
Matt J 2013년 10월 9일
편집: Matt J 2013년 10월 9일
It's really not ideal to fit polynomials bluntly using backslash. That's why MATLAB offers the more robust POLYFIT and why the File Exchange offers a variety of polynomial fitters.
Syeda
Syeda 2013년 10월 9일
yes!! My results are not accurate by using this method.

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Matt J
Matt J 2013년 10월 8일

1 개 추천

Using a nonlinear solver for a linear fitting problem seems like the wrong way to go. A better option might be

댓글 수: 10

Syeda
Syeda 2013년 10월 8일
Thankyou for replying Matt.
I tried John D'Errico's polyfitn()as well. But, how can I fit my proposed model in this function?
model = [ ( x(:).^0 ).*( y(:).^2 ), ( x(:).^1 ).*( y(:).^1 ), ( x(:).^2 ).*( y(:).^0 ) ]
p = polyfitn([x,y], z, 'model');
It gives an error:
??? Error using ==> polyfitn at 137 indepvar and depvar are of inconsistent sizes.
I've never used the tool myself. Perhaps you need to columnize z,
p = polyfitn([x,y], z(:), 'model');
Syeda
Syeda 2013년 10월 8일
It still gives the same error.
Anyways, thankyou for replying!!
Maybe this
p = polyfitn([x(:),y(:)], z(:), {'y', 'x*y', 'x^2*y'});
Syeda
Syeda 2013년 10월 8일
편집: Syeda 2013년 10월 8일
This works for me
p = polyfitn([x(:),y(:)], z(:), 'model')
But, it is giving only one coefficient. I want 3 coefficients..
Syeda
Syeda 2013년 10월 8일
편집: Syeda 2013년 10월 8일
I tried both equations, but it gives error!
p = polyfitn([x(:),y(:)], z(:), {'y', 'x*y', 'x^2*y'});
p = polyfitn([x(:),y(:)], z(:), {'x*y^2', 'x*y', 'x^2*y'});
??? Error using ==> polyfitn>parsemodel at 319 Variable is not a valid name: '2'
Error in ==> polyfitn at 151 [modelterms,varlist] = parsemodel(modelterms,p);
I can't see why the syntax
p = polyfitn([x(:),y(:)], z(:), 'model')
would work. And it is obviously not working, since it gives you the wrong number of coefficients.
Syeda
Syeda 2013년 10월 8일
yes! it is not working as well. I guess there is some syntax problem in my code.
Matt J
Matt J 2013년 10월 8일
Dunno. Instead of 'x^2*y' maybe you should try either 'x*x*y' or 'y*x^2'.
Syeda
Syeda 2013년 10월 9일
No, it still gives error.
??? Undefined function or method 'polyfitn' for input arguments of type 'cell'

댓글을 달려면 로그인하십시오.

카테고리

도움말 센터File Exchange에서 Least Squares에 대해 자세히 알아보기

질문:

2013년 10월 8일

댓글:

2013년 10월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by