MATLAB Answers

How to sum two data sets to get a best match of another data set?

1 view (last 30 days)
I have three spectrum, which essentially shares the same wavelength (x-axis). I would like to add the first two spectrum (i.e. column y1 & y2) is such a way as describes below to match the 3rd spectrum:
Ideally: y3 = a*y1 + b*y2
But in reality, I would like to find coefficients a & b with minimised y3 - (a*y1 + b*y2)
All x-axis, y1, y2, and y3 are columns of 608X1
The problem I am facing is that I don't know how to optimise the coefficients a & b to achive what I want.
Thank you !

Accepted Answer

Mathieu NOE
Mathieu NOE on 3 May 2021
hello
see demo below :
I added some noise on y3 to see the robustness of fminsearch
got the results :
a = 3.0379
b = 5.0149
which is pretty close to the input equation coefficients
y1 = rand(1,20);
y2 = 3*rand(1,20);
noise = 0.1*rand(1,20);
y3 = 3*y1+5*y2+noise;
% equation to minimize : y3-(a*y1+b*y2) %a = x(1), b=x(2)
% fminsearch optimization loop
fun = @(x)norm(y3-(x(1)*y1+x(2)*y2)); %a = x(1), b=x(2)
x0 = [0, 0];
X = fminsearch(fun,x0);
a = X(1)
b = X(2)
  3 Comments

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!