Apologies if my use of terminology is wrong.
I essentially have four equal sized data sets (eg. x y z w) and I want find an equation that describes one of those data sets as a funtion of the other three. To begin with I'm assuming this is a linear relationship i.e. x = a*y + b*z + c*w.
Is there a way to fit these data sets to find the values of the coefficients a b c?
Thanks for your help!

 採用された回答

Star Strider
Star Strider 2022 年 1 月 19 日

1 投票

Yes! The mldivide,\ funciton will do this.
x = randn(10,1);
y = randn(10,1);
z = randn(10,1);
w = randn(10,1);
DM = [y(:) z(:) w(:)]; % Design Matrix
abc = DM \ x(:)
abc = 3×1
-0.0250 -0.2847 -0.4305
Results = table(x(:),DM*abc,x(:)-DM*abc, 'VariableNames',{'Original x','Regressed x','Difference'})
Results = 10×3 table
Original x Regressed x Difference __________ ___________ __________ 0.13267 0.10844 0.024228 -2.5397 -1.2413 -1.2984 0.05946 0.71663 -0.65717 0.82979 1.1043 -0.2745 -0.11678 -0.37399 0.25721 0.42652 -0.55752 0.98404 0.26181 0.62586 -0.36404 1.0023 -0.039926 1.0422 0.57563 -0.16844 0.74407 -0.92174 -0.34643 -0.57531
meanDifference = mean(Results.Difference)
meanDifference = -0.0118
To get statistics on the fit, use regress, fitlm, or similar functions.
.

2 件のコメント

Fynn Oppermann
Fynn Oppermann 2022 年 1 月 19 日
Thank you!
Would this method still work if my function changes to be nonlinear?
Star Strider
Star Strider 2022 年 1 月 19 日
My pleasure!
It will only work for linear relations, however coding it for nonlilnear relations would be straightforward and may not involve anythng other than core MATLAB. For a nonlinear regression, one approach would be to use fminsearch if other Toolboxes are not available.
Example —
x = randn(10,1);
y = randn(10,1);
z = randn(10,1);
w = randn(10,1);
yzw = [y(:) z(:) w(:)]; % Contatenate Column Vectors
% % % FUNCTION: exp(a*y) * sin(2*pi*b*z) + c*w
objfcn = @(b,iv) exp(b(1)*iv(:,1)) .* sin(2*pi*b(2).*iv(:,2)) + b(3)*iv(:,3)
objfcn = function_handle with value:
@(b,iv)exp(b(1)*iv(:,1)).*sin(2*pi*b(2).*iv(:,2))+b(3)*iv(:,3)
B = fminsearch(@(b) norm(x(:) - objfcn(b,yzw)), rand(3,1))
B = 3×1
0.3240 1.2573 0.0702
fprintf(1, '\n\ta = %9.4f\n\tb = %9.4f\n\tc = %9.4f\n',B)
a = 0.3240 b = 1.2573 c = 0.0702
Results = table(x(:), objfcn(B,yzw), x(:)-objfcn(B,yzw), 'VariableNames',{'Original x','Regressed x','Difference'})
Results = 10×3 table
Original x Regressed x Difference __________ ___________ __________ 1.1085 0.83031 0.27821 0.90088 0.47772 0.42316 -0.67653 0.63336 -1.3099 0.0039623 -0.88416 0.88813 1.1665 0.61915 0.54735 0.80068 0.90119 -0.10051 0.12086 -0.39058 0.51144 -0.59784 0.17719 -0.77503 0.88238 0.58316 0.29923 0.033881 -0.74814 0.78202
Here, ‘iv’ is the independent variable matrix, composed of the independent variable vectors. This allows a single variable to be passed to any of the curve-fitting (parameter estimation) functions, as their syntax requires, while fitting every independent variable.
Since there are only three parameters, the fminsearch function can likely do a decent approximaton to the desired parameter estimates.
.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

ヘルプ センター および File Exchange で Curve Fitting Toolbox についてさらに検索

製品

リリース

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by