1

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?

Kilian Weber
  • 160
  • 1
  • 8

1 Answers1

1

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)
David Wright
  • 464
  • 6
  • 18
  • Sorry, I adjusted my answer and tried a little example on my computer, with this approach I think you should get your solution – David Wright Jul 11 '17 at 08:08
  • Thx, but how to do `[a; -b] = x` as there are Multiple left-hand sides can you just copy yur code please – Kilian Weber Jul 11 '17 at 08:17
  • 1
    I just used `[a; -b] = x` so that I could replace `[a; -b]` with `x` in the formula, as a simplification, and because the solution of the least squares method would be a column vector, that I just named `x`. I added my test code to my answer. – David Wright Jul 11 '17 at 08:30
  • That's great, glad i could help! – David Wright Jul 11 '17 at 08:48