How to determine coefficients by fit for multiple series of data?

조회 수: 1 (최근 30일)
Benedikt
Benedikt 2022년 5월 26일
댓글: Benedikt 2022년 5월 30일
Hey there,
I've searched for a while but did not find the right thing for my problem. I have several series of experiments with different amount of data, which might be described by an equation with 2 coefficents. The equation is:
y(x) = a * D * x^2 + b * x
D is a fixed value for each series of experiments, a and b are the coefficients I want to fit.
As an example x, y and D have the following values for three series of experiments (each row is one experiment):
x = [2,4,6,NaN,NaN; 2,4,6,8,10; 2,4,6,8,NaN];
y = [3,5,7,NaN,NaN; 4,8,11,25,32; 7,10,15,17,NaN];
D = [3;5;7];
Now I want to calculate the coefficients a and b by the fitting tool box, but the tool box throws just all values of y in and does not seperate the three cases with different value of D.
Is there a way to tell this the toolbox? or alternatively to calculate the coefficents by code?
Many thanks in advance for any help and best regards,
Benedikt

채택된 답변

Torsten
Torsten 2022년 5월 26일
x = [2,4,6,NaN,NaN; 2,4,6,8,10; 2,4,6,8,NaN];
y = [3,5,7,NaN,NaN; 4,8,11,25,32; 7,10,15,17,NaN];
D = [3;5;7];
N = size(x, 1);
A = [];
b = [];
for K = 1:N
mask = ~(isnan(x(K,:)) | isnan(y(K,:)));
tx = x(K,mask).';
ty = y(K,mask).';
A = [A;D(K)*tx.^2,tx];
b = [b;ty];
end
ab = A\b
a = ab(1)
b = ab(2)
  댓글 수: 1
Benedikt
Benedikt 2022년 5월 30일
Hi Torsten,
thanks for the solution! this works fine for me!
Best regards,
Benedikt

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Walter Roberson
Walter Roberson 2022년 5월 26일
x = [2,4,6,NaN,NaN; 2,4,6,8,10; 2,4,6,8,NaN];
y = [3,5,7,NaN,NaN; 4,8,11,25,32; 7,10,15,17,NaN];
D = [3;5;7];
N = size(x, 1);
ab = zeros(N, 2);
for K = 1 : N
mask = ~(isnan(x(K,:)) | isnan(y(K,:)));
tx = x(K,mask);
ty = y(K,mask);
ty2 = ty ./ tx; %now it is A*D*x + b
p = polyfit(tx(:), ty2(:), 1);
ab(K,1) = p(1)./D(K);
ab(K,2) = p(2);
end
ab
ab = 3×2
-0.0278 1.6389 0.0352 1.3742 -0.0295 3.6875
  댓글 수: 1
Benedikt
Benedikt 2022년 5월 26일
Hi Walter,
thank you for your solution!
But it is not exactly what I search for. I just want one value for each coefficient a and b over all series of experiments because I know that phyiscally all series of experiments are the same but with different input parameters, described by D, and different scope.
So I want to use the data of all the three series to get a more accurate value for a and b, as I calucalte them just by one series of experiments.
Best regards,
Benedikt

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 MATLAB에 대해 자세히 알아보기

제품


릴리스

R2020b

Community Treasure Hunt

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

Start Hunting!

Translated by