Kilian Weber
Kilian Weber

Reputation: 160

MATLAB fitting matrices

I got the case P(t) = a * A(t) - b* B(t), each 17281x1 doubles.

Now I want to use curve fitting to get the variables a and b.

Fitting 1

Fitting case, here a and b as 1 and 1

I know fit and fittype, but they seem not to work in this case.

Any ideas, how to get this solved?

Upvotes: 1

Views: 122

Answers (1)

David Wright
David Wright

Reputation: 494

How about using the least squares method? If I understand correctly, your problem could be expressed as P(t) = [A(t), B(t)] * [a; -b].

Let [a; -b] = x, [A(t), B(t)] = Y and P(t) = P

Now the least squares solution would be:

x = ((Y'*Y)^-1)*Y'*P;

In Matlab you could also use the 'backslash operator' for this case:

x = Y\P;

for this, you'll find the documentation here: mldivide

As a reference:

Wikipedia

Mathworks

I hope this helps.

EDIT:

Here's my test code:

A = [1;2;3]
B = [4;5;6]
P = [7;8;9]

Y = [A, -B]

disp('------- regular least squares formula -------')
x = ((Y'*Y)^-1)*Y'*P

a = x(1)
b = x(2)

disp('------- mldivide -------')
x = Y\P

a = x(1)
b = x(2)

Upvotes: 1

Related Questions