Curve fitting with multiple variables?

15 ビュー (過去 30 日間)
Liril Silvi
Liril Silvi 2024 年 4 月 22 日
コメント済み: Sam Chak 2024 年 4 月 23 日
I have three different curves for S=2, S=1 and S=0.5 as shown in Figure below. The Book1.xlsx is attached.
I want to use curve fitting to find out one common equation such as, HF = f(S,T).
I used the following comman line to get the equation which I got from previous post (link). But I am getting following error:
Error using nlinfit (line 213)
Error evaluating model function '@(b,x)exp(b(1)).*x(:,1).^b(2)+b(3).*x(:,2)
What should I do? Is there any other better method as well?
T1 = readtable('Book1.xlsx', 'VariableNamingRule','preserve')
T1 = 10x4 table
T S2 S1 S0.5 __ __________ __________ __________ 10 2.2639e+05 1.63e+05 44925 12 3.2139e+05 2.3122e+05 1.3253e+05 14 4.0539e+05 2.9211e+05 2.0969e+05 16 4.7838e+05 3.4568e+05 2.764e+05 18 5.4038e+05 3.9192e+05 3.3265e+05 20 5.9137e+05 4.3084e+05 3.7846e+05 22 6.3136e+05 4.6243e+05 4.1383e+05 24 6.6034e+05 4.867e+05 4.3874e+05 26 6.7833e+05 5.0363e+05 4.532e+05 28 6.8531e+05 5.1325e+05 4.5722e+05
VN = T1.Properties.VariableNames
VN = 1x4 cell array
{'T'} {'S2'} {'S1'} {'S0.5'}
Viv = regexp([VN{:}], '(\d*\.\d*)|\d*', 'match')
Viv = 1x3 cell array
{'2'} {'1'} {'0.5'}
Viv = str2double(Viv)
Viv = 1x3
2.0000 1.0000 0.5000
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
Viym = T1{:,2:4};
Viyv = Viym(:); % 'Vi' Vector
Vivm = repmat(Viv, size(T1,1), 1);
Vivv = Vivm(:); % 'Viv' Vector
y = Viyv;
SoCv = repmat(T1.T, size(Viym,2), 1) % 'SoC' Vector
Error using . (line 229)
Unrecognized table variable name 'deltaT'.
x = [SoCv Vivv]; % Concatenate Vectors
objfcn = @(b,x) exp(b(1)).*x(:,1).^b(2) + b(3).*x(:,2);
mdl = fitnlm(x, y, objfcn, rand(3,1))
  3 件のコメント
Torsten
Torsten 2024 年 4 月 22 日
編集済み: Torsten 2024 年 4 月 22 日
x must be the matrix
x = [10 0.5;12 0.5;...;28 0.5;10 1;12 1;...;28 1;10 2;12 2;...;28 2]
and y must be the matrix
y = [S0.5;S1;S2]
in your code.
And why do you use exp(b(1)) in your model function ? Why not simply b(1) ?
Liril Silvi
Liril Silvi 2024 年 4 月 23 日
編集済み: Torsten 2024 年 4 月 23 日
deltaT should be T (I edited this one in the original question). Now, you will get the error as I said.

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

採用された回答

Sam Chak
Sam Chak 2024 年 4 月 23 日
See if the polynomial surface function is acceptable
tb = readtable('Book2.xlsx', 'VariableNamingRule','preserve');
S = tb{1:30,2};
T = tb{1:30,1};
HF = tb{1:30,3};
[sf, gof] = fit([S, T], HF, 'poly24')
sf =
Linear model Poly24: sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 + p21*x^2*y + p12*x*y^2 + p03*y^3 + p22*x^2*y^2 + p13*x*y^3 + p04*y^4 Coefficients (with 95% confidence bounds): p00 = -1.028e+06 (-1.028e+06, -1.028e+06) p10 = 1.173e+06 (1.173e+06, 1.173e+06) p01 = 1.108e+05 (1.108e+05, 1.108e+05) p20 = -4.329e+05 (-4.329e+05, -4.329e+05) p11 = -9.663e+04 (-9.663e+04, -9.662e+04) p02 = -2110 (-2110, -2110) p21 = 4.004e+04 (4.004e+04, 4.004e+04) p12 = 2021 (2021, 2021) p03 = -5.616e-12 (-5.367e-11, 4.243e-11) p22 = -826.9 (-826.9, -826.9) p13 = -1.061e-12 (-5.705e-12, 3.584e-12) p04 = 8.182e-14 (-5.452e-13, 7.089e-13)
gof = struct with fields:
sse: 2.0286e-17 rsquare: 1 dfe: 18 adjrsquare: 1 rmse: 1.0616e-09
plot(sf, [S, T], HF), xlabel('S'), ylabel('T'), zlabel('HF')
  2 件のコメント
Liril Silvi
Liril Silvi 2024 年 4 月 23 日
This polynomial equation has excellent fit. But, I was more into a simple and concise equation rather than a long one with lots of coefficients. Something like as you said about paraboloid function.
Sam Chak
Sam Chak 2024 年 4 月 23 日
The Hyperbolic Paraboloid has this polynomial form when expanded. Thus, you can select 'poly22'.
syms a [1 4]
syms x y
%% Hyperbolic Paraboloid
hp = a1*(x - a3)^2 + a2*(y - a4)^2;
expand(p)
ans = 
tb = readtable('Book2.xlsx', 'VariableNamingRule','preserve');
S = tb{1:30,2};
T = tb{1:30,1};
HF = tb{1:30,3};
[sf, gof] = fit([S, T], HF, 'poly22')
sf =
Linear model Poly22: sf(x,y) = p00 + p10*x + p01*y + p20*x^2 + p11*x*y + p02*y^2 Coefficients (with 95% confidence bounds): p00 = -5.246e+05 (-6.033e+05, -4.459e+05) p10 = 8.896e+04 (2.158e+04, 1.563e+05) p01 = 6.545e+04 (5.808e+04, 7.282e+04) p20 = 2105 (-2.134e+04, 2.555e+04) p11 = 2343 (827.7, 3858) p02 = -1199 (-1386, -1012)
gof = struct with fields:
sse: 4.9789e+09 rsquare: 0.9936 dfe: 24 adjrsquare: 0.9922 rmse: 1.4403e+04
plot(sf, [S, T], HF), xlabel('S'), ylabel('T'), zlabel('HF')

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

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeLinear and Nonlinear Regression についてさらに検索

製品


リリース

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by