Thread Subject: polyfit with two input instead of one

Subject: polyfit with two input instead of one

From: Charbel

Date: 25 Nov, 2009 18:56:19

Message: 1 of 10

Hi all,
i'm trying to obtain correlations for certain physic parameters .
my question is
Can i use polyfit to obtain y=a1 + a2.x + a3.x^2 + a4.z +a5.z^2+a6.z^3
with 'ai' are the coefficients to be found (i=1 to 6)
and x is certain parameter ( like pressure)
and z is another parameter ( like temperature)
( I already know y experimental corresponding to a certain x and certain z)

Maybe there another function with Matlab that can do this ( other than polyfit)

Any suggestions?

Subject: polyfit with two input instead of one

From: Matt

Date: 25 Nov, 2009 19:14:19

Message: 2 of 10

"Charbel " <charboul23@hotmail.com> wrote in message <hejukj$qti$1@fred.mathworks.com>...
> Hi all,
> i'm trying to obtain correlations for certain physic parameters .
> my question is
> Can i use polyfit to obtain y=a1 + a2.x + a3.x^2 + a4.z +a5.z^2+a6.z^3
> with 'ai' are the coefficients to be found (i=1 to 6)
> and x is certain parameter ( like pressure)
> and z is another parameter ( like temperature)
> ( I already know y experimental corresponding to a certain x and certain z)
>
> Maybe there another function with Matlab that can do this ( other than polyfit)
>
> Any suggestions?

X=bsxfun(@power,x(:), [0 1 2]);
Z=bsxfun(@power,z(:), [1 2 3]);

a=[X,Z]\y; %a=[a1,a2,a3,a4,a5,a6].'

Subject: polyfit with two input instead of one

From: Charbel

Date: 25 Nov, 2009 19:31:36

Message: 3 of 10

"Matt " <xys@whatever.com> wrote in message <hejvmb$28e$1@fred.mathworks.com>...
> "Charbel " <charboul23@hotmail.com> wrote in message <hejukj$qti$1@fred.mathworks.com>...
> > Hi all,
> > i'm trying to obtain correlations for certain physic parameters .
> > my question is
> > Can i use polyfit to obtain y=a1 + a2.x + a3.x^2 + a4.z +a5.z^2+a6.z^3
> > with 'ai' are the coefficients to be found (i=1 to 6)
> > and x is certain parameter ( like pressure)
> > and z is another parameter ( like temperature)
> > ( I already know y experimental corresponding to a certain x and certain z)
> >
> > Maybe there another function with Matlab that can do this ( other than polyfit)
> >
> > Any suggestions?
>
> X=bsxfun(@power,x(:), [0 1 2]);
> Z=bsxfun(@power,z(:), [1 2 3]);
>
> a=[X,Z]\y; %a=[a1,a2,a3,a4,a5,a6].'




bsxfun?
it is a certain function for a certain toolbox?
because i've tried it and Matlab gave me this error:
??? Undefined function or method 'bsxfun' for input arguments of type 'function_handle'.

Subject: polyfit with two input instead of one

From: Charbel

Date: 25 Nov, 2009 19:37:18

Message: 4 of 10

I realised it's a multiple regression but may you please re explain the bsxfun thing.?

Subject: polyfit with two input instead of one

From: Matt

Date: 25 Nov, 2009 19:44:04

Message: 5 of 10

"Charbel " <charboul23@hotmail.com> wrote in message <hek0mo$4t1$1@fred.mathworks.com>...

> >
> > X=bsxfun(@power,x(:), [0 1 2]);
> > Z=bsxfun(@power,z(:), [1 2 3]);
> >
> > a=[X,Z]\y; %a=[a1,a2,a3,a4,a5,a6].'
>
>
>
>
> bsxfun?
> it is a certain function for a certain toolbox?
> because i've tried it and Matlab gave me this error:
> ??? Undefined function or method 'bsxfun' for input arguments of type 'function_handle'.

I can only assume you have an older version of MATLAB. bsxfun is a standard function as of R2008.

In any case, you can get the same effect by doing

x=x(:); z=z(:);
X=[x.^0 x.^1 x.^2];
Z=[z.^1 z.^2 z.^3];

Subject: polyfit with two input instead of one

From: Matt

Date: 25 Nov, 2009 19:55:23

Message: 6 of 10

And here's the description of bsxfun, in case you were at all interested...

>> help bsxfun

 BSXFUN Binary Singleton Expansion Function
    C = BSXFUN(FUNC,A,B) Apply an element-by-element binary operation to
    arrays A and B, with singleton expansion enabled. FUNC is a
    function handle. FUNC can either be a function handle for an
    M-function, or one of the following built-in functions:
 
                @plus Plus
                @minus Minus
                @times Array multiply
                @rdivide Right array divide
                @ldivide Left array divide
                @power Array power
                @max Binary maximum
                @min Binary minimum
                @rem Remainder after division
                @mod Modulus after division
                @atan2 Four-quadrant inverse tangent
                @hypot Square root of sum of squares
                @eq Equal
                @ne Not equal
                @lt Less than
                @le Less than or equal
                @gt Greater than
                @ge Greater than or equal
                @and Element-wise logical AND
                @or Element-wise logical OR
                @xor Logical EXCLUSIVE OR
 
    If an M-function is specified, the M-function must be able to
    accept as input either two column vectors of the same size, or
    one column vector and one scalar, and return as output a column vector
    of the same size as the input(s).
    Each dimension of A and B must be equal to each other, or equal to one.
    Whenever a dimension of one of A or B is singleton (equal to 1), the array
    is virtually replicated along that dimension to match the other array
    (or diminished if the corresponding dimension of the other array is 0).
    The size of the output array C is equal to
    max(size(A),size(B)).*(size(A)>0 & size(B)>0). For
    example, if size(A)==[2 5 4] and size(B)==[2 1 4 3], then
    size(C)==[2 5 4 3].
 
    Examples - Subtract the column means from the matrix A
     
      A = magic(5);
      A = bsxfun(@minus, A, mean(A));
 
    See also repmat, arrayfun

    Reference page in Help browser

Subject: polyfit with two input instead of one

From: Charbel

Date: 25 Nov, 2009 20:03:22

Message: 7 of 10

you're right
2006
and i've tried, it worked
although the results are not good
but it gives an answer anyway
thank you very much
Best Regards

Subject: polyfit with two input instead of one

From: Matt

Date: 25 Nov, 2009 20:17:20

Message: 8 of 10

"Charbel " <charboul23@hotmail.com> wrote in message <hek2ia$s3j$1@fred.mathworks.com>...
> you're right
> 2006
> and i've tried, it worked
> although the results are not good
> but it gives an answer anyway
> thank you very much
> Best Regards

It may also help to try this

[Q,R]=qr([X,Z],0);
a=R\(Q'*y);

It's done in a variety of regression routines and seems to stabilize things numerically. You might also look at POLYFITN on the File Exchange.

Failing all that, it looks like it might just be a case of bad/noisy data...

Subject: polyfit with two input instead of one

From: Charbel

Date: 26 Nov, 2009 12:35:08

Message: 9 of 10

Actually i retried your equations, and actually it's good
Thank you very much for your help
and thanks really for the help of the bsxfun.
I'll try to check it out later.
Best Regards
Charbel

Subject: polyfit with two input instead of one

From: Charbel

Date: 26 Nov, 2009 12:49:04

Message: 10 of 10

Actually i retried your equations, and actually it's good
Thank you very much for your help
and thanks really for the help of the bsxfun.
I'll try to check it out later.
Best Regards
Charbel

Tags for this Thread

Everyone's Tags:

Add a New Tag:

Separated by commas
Ex.: root locus, bode

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Tag Activity for This Thread
Tag Applied By Date/Time
matt Charbel 25 Nov, 2009 14:39:23
linear square Charbel 25 Nov, 2009 13:59:07
regressions Charbel 25 Nov, 2009 13:59:07
polyval Charbel 25 Nov, 2009 13:59:07
nonlin Charbel 25 Nov, 2009 13:59:07
lsqcurvefit Charbel 25 Nov, 2009 13:59:07
polyfit Charbel 25 Nov, 2009 13:59:07
rssFeed for this Thread

Contact us at files@mathworks.com