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

1 次查看(过去 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 个)

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