MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

# Thread Subject: Fit two matrices to one equation - To find coefficients of that equation

Subject: Fit two matrices to one equation - To find coefficients of that equation

From: Oweme Koul

### Aumi

(Oweme Koul)

Date: 23 Dec, 2008 23:51:04

Message: 1 of 4

Hi all,

I have 2 matrices "alpha" && "f" both 6401*1 -
I can plot alpha with f and i know from Maple :
alpha=A*sqrt(f)+B*(f)+C*(f.^2)

I want to find out the coefficients A,B,C ?
Can i perform this using matlab? Would be of great help.

Thanks

Subject: Fit two matrices to one equation - To find coefficients of that equation

From: Roger Stafford

### Roger Stafford

Date: 24 Dec, 2008 00:14:04

Message: 2 of 4

"Oweme Koul" <ak987@mst.edu> wrote in message <girth8$9l9$1@fred.mathworks.com>...
> Hi all,
>
> I have 2 matrices "alpha" && "f" both 6401*1 -
> I can plot alpha with f and i know from Maple :
> alpha=A*sqrt(f)+B*(f)+C*(f.^2)
>
> I want to find out the coefficients A,B,C ?
> Can i perform this using matlab? Would be of great help.
>
> Thanks

[sqrt(f),f,f.^2]\alpha

gives [A;B;C].

Roger Stafford

Subject: Fit two matrices to one equation - To find coefficients of that equation

From: Aumi

### Aumi

Date: 24 Dec, 2008 01:00:05

Message: 3 of 4

Thanks
Aumi

Subject: Fit two matrices to one equation - To find coefficients of that equation

From: Roger Stafford

### Roger Stafford

Date: 24 Dec, 2008 03:53:06

Message: 4 of 4

"Aumi " <ak987@mst.edu> wrote in message <gis1il$d3l$1@fred.mathworks.com>...
> ......

I interpreted your question to mean that you wanted to adjust the three coefficients, A, B, and C, so as to minimize the mean square difference between the elements in your 'alpha' vector and the corresponding elements of the vector A*sqrt(f)+B*(f)+C*(f.^2). According to your statement the arrays 'alpha', 'f', and therefore 'A*sqrt(f)+B*(f)+C*(f.^2)' are each column vectors containing 6401 elements. If you subtract the first of these from the third, square these differences, and then find the mean value of these squares, you would presumably want this mean value to be a minimum. If you could get it down to zero, all the better.

That is exactly what the backslash '\' operator does in matlab. When the number of equalities (6401) exceeds the number of unknowns (3), it adjusts these unknowns so as to minimum the mean square differences. In this case on the left side is a 6401 x 3 matrix of values and on the right side a 6401 by 1 column vector and the 3 x 1 answer of [A;B;C] which it delivers is intended to satisfy the requirement

alpha = [sqrt(f),f,f.^2]*[A;B;C] % <-- which is A*sqrt(f)+B*f+C*f.^2

as closely as it can in a least squares sense.

Roger Stafford