# Curve fitting for multiple variable

13 views (last 30 days)
Haroon Zafar on 21 Nov 2022
Commented: Haroon Zafar on 22 Nov 2022
I am trying to fit the curves as shown below. Equivalent resistance R is varying with SoC. But I have third variable Vi (0.8 -1p.u) which shifts the R downwards. I am trying to find a general fit for these curves such that F ( SoC, Vi).
I have attached data as well if anyone can help me out or any idea to approach this problem. I have tried with Cftool but the data dimensions are not same .
Matt J on 21 Nov 2022
It would be easiest if you showed us the model function you want applied to the different curves.
Haroon Zafar on 21 Nov 2022
I have no specific requirement of the function type. Using Cftool I found first order power function to be satisfactory for individual curves. But how can I include the effect of Vi , which is shifting curves downwards and varies from 0.8-1.0.

Star Strider on 21 Nov 2022
I have no clear idea what you want to do.
This approach fits all the parameters at once —
T1 = 26×6 table
SoC 0.8Vi 0.85Vi 0.9Vi 0.95Vi 1Vi ___ ______ ______ ______ ______ ______ 5 60.989 57.342 54.322 51.526 50.204 10 60.212 56.618 53.619 50.862 49.53 15 59.832 56.268 53.278 50.53 49.205 20 59.334 55.776 52.813 50.061 48.783 25 58.914 55.404 52.422 49.712 48.434 30 58.567 55.057 52.131 49.431 48.144 35 58.187 54.701 51.772 49.087 47.813 40 58.012 54.567 51.637 48.956 47.708 45 57.91 54.439 51.523 48.86 47.59 50 57.79 54.318 51.407 48.758 47.474 55 57.644 54.191 51.278 48.613 47.346 60 57.532 54.066 51.181 48.509 47.242 65 57.384 53.956 51.07 48.427 47.143 70 57.25 53.79 50.946 48.31 47.033 75 57.157 53.707 50.825 48.184 46.918 80 57.075 53.64 50.785 48.141 46.864
VN = T1.Properties.VariableNames
VN = 1×6 cell array
{'SoC'} {'0.8Vi'} {'0.85Vi'} {'0.9Vi'} {'0.95Vi'} {'1Vi'}
Viv = regexp([VN{:}], '(\d*\.\d*)|\d*', 'match')
Viv = 1×5 cell array
{'0.8'} {'0.85'} {'0.9'} {'0.95'} {'1'}
Viv = str2double(Viv)
Viv = 1×5
0.8000 0.8500 0.9000 0.9500 1.0000
Viym = T1{:,2:6};
Viyv = Viym(:); % 'Vi' Vector
Vivm = repmat(Viv, size(T1,1), 1);
Vivv = Vivm(:); % 'Viv' Vector
y = Viyv;
SoCv = repmat(T1.SoC, size(Viym,2), 1) % 'SoC' Vector
SoCv = 130×1
5 10 15 20 25 30 35 40 45 50
x = [SoCv Vivv]; % Concatenate Vectors
objfcn = @(b,x) exp(b(1)).*x(:,1).^b(2) + b(3).*x(:,2);
% [B,resn] = lsqcurvefit(objfcn, rand(3,1), x, y)
mdl = fitnlm(x, y, objfcn, rand(3,1))
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ __________ _______ ___________ b1 4.6505 0.0067979 684.11 2.3549e-228 b2 -0.014195 0.00067264 -21.103 2.4122e-43 b3 -52.331 0.72897 -71.788 1.1374e-104 Number of observations: 130, Error degrees of freedom: 127 Root Mean Squared Error: 0.588 R-Squared: 0.978, Adjusted R-Squared 0.977 F-statistic vs. zero model: 3.4e+05, p-value = 1.03e-247
An alternative approach uses a loop to fit each column individually —
B0 = mdl.Coefficients.Estimate;
for k = 1:size(Viym,2)
x = [T1.SoC Vivm(:,k)]
y = Viym(:,k)
mdl = fitnlm(x, y, objfcn, B0)
end
x = 26×2
5.0000 0.8000 10.0000 0.8000 15.0000 0.8000 20.0000 0.8000 25.0000 0.8000 30.0000 0.8000 35.0000 0.8000 40.0000 0.8000 45.0000 0.8000 50.0000 0.8000
y = 26×1
60.9887 60.2122 59.8316 59.3342 58.9138 58.5672 58.1869 58.0121 57.9105 57.7896
Warning: Iteration limit exceeded. Returning results from final iteration.
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ _______ b1 5.0195 3.948 1.2714 0.21629 b2 -0.010636 0.043594 -0.24397 0.80942 b3 -109.3 746.47 -0.14643 0.88486 Number of observations: 26, Error degrees of freedom: 23 Root Mean Squared Error: 0.117 R-Squared: 0.992, Adjusted R-Squared 0.991 F-statistic vs. constant model: 1.37e+03, p-value = 1.25e-24
x = 26×2
5.0000 0.8500 10.0000 0.8500 15.0000 0.8500 20.0000 0.8500 25.0000 0.8500 30.0000 0.8500 35.0000 0.8500 40.0000 0.8500 45.0000 0.8500 50.0000 0.8500
y = 26×1
57.3419 56.6183 56.2681 55.7763 55.4043 55.0568 54.7007 54.5669 54.4393 54.3177
Warning: Iteration limit exceeded. Returning results from final iteration.
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ _______ b1 4.9841 4.0116 1.2424 0.2266 b2 -0.010412 0.043331 -0.2403 0.81223 b3 -101.15 689.07 -0.14679 0.88457 Number of observations: 26, Error degrees of freedom: 23 Root Mean Squared Error: 0.11 R-Squared: 0.992, Adjusted R-Squared 0.991 F-statistic vs. constant model: 1.38e+03, p-value = 1.08e-24
x = 26×2
5.0000 0.9000 10.0000 0.9000 15.0000 0.9000 20.0000 0.9000 25.0000 0.9000 30.0000 0.9000 35.0000 0.9000 40.0000 0.9000 45.0000 0.9000 50.0000 0.9000
y = 26×1
54.3216 53.6187 53.2782 52.8128 52.4222 52.1308 51.7724 51.6367 51.5228 51.4068
Warning: Iteration limit exceeded. Returning results from final iteration.
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ _______ b1 4.9387 3.9118 1.2625 0.21941 b2 -0.010408 0.042233 -0.24645 0.80752 b3 -91.846 606.51 -0.15143 0.88095 Number of observations: 26, Error degrees of freedom: 23 Root Mean Squared Error: 0.102 R-Squared: 0.992, Adjusted R-Squared 0.991 F-statistic vs. constant model: 1.46e+03, p-value = 6.04e-25
x = 26×2
5.0000 0.9500 10.0000 0.9500 15.0000 0.9500 20.0000 0.9500 25.0000 0.9500 30.0000 0.9500 35.0000 0.9500 40.0000 0.9500 45.0000 0.9500 50.0000 0.9500
y = 26×1
51.5259 50.8622 50.5304 50.0606 49.7115 49.4312 49.0868 48.9559 48.8601 48.7579
Warning: Iteration limit exceeded. Returning results from final iteration.
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ _______ b1 4.9102 3.9509 1.2428 0.22647 b2 -0.010229 0.041892 -0.24418 0.80926 b3 -85.939 564.01 -0.15237 0.88022 Number of observations: 26, Error degrees of freedom: 23 Root Mean Squared Error: 0.0967 R-Squared: 0.992, Adjusted R-Squared 0.992 F-statistic vs. constant model: 1.48e+03, p-value = 5.01e-25
x = 26×2
5 1 10 1 15 1 20 1 25 1 30 1 35 1 40 1 45 1 50 1
y = 26×1
50.2041 49.5297 49.2049 48.7825 48.4339 48.1437 47.8131 47.7076 47.5903 47.4742
Warning: Iteration limit exceeded. Returning results from final iteration.
mdl =
Nonlinear regression model: y ~ exp(b1)*x1^b2 + b3*x2 Estimated Coefficients: Estimate SE tStat pValue _________ ________ ________ _______ b1 4.9107 3.9557 1.2414 0.22696 b2 -0.010039 0.041135 -0.24405 0.80936 b3 -83.087 536.75 -0.1548 0.87833 Number of observations: 26, Error degrees of freedom: 23 Root Mean Squared Error: 0.0933 R-Squared: 0.993, Adjusted R-Squared 0.992 F-statistic vs. constant model: 1.54e+03, p-value = 3.3e-25
The ‘objfun’ function uses a simple power relation and adds a term for the ‘Vi’ value.
I am not certain how the ‘Vi’ values relate the the ‘Eqivalent Resistance’, or for that matter what these data are. So this is essentially a prototype approach to demonstrate how to incorporate all the necessary data, even though I’m not certain how to correctly incorporate it.
I defer to you to determine the desiured result, and how to actually get it.
.
Alex Sha on 22 Nov 2022
try the model function below, where, x1=SoC, x2=Vi, y=Req
Sum Squared Error (SSE): 0.426970556518359
Root of Mean Square Error (RMSE): 0.0573095881706985
Correlation Coef. (R): 0.999892090395877
R-Square: 0.999784192436237
Parameter Best Estimate
--------- -------------
p1 -0.0700360286852488
p2 0.000891267300607825
p3 0.0904543618535223
p4 248.161725009268
p5 2.57600192708312
p6 -0.799835129723361
p7 1.10012417863406
p8 -10036.5979452884
p9 -3.96886303014785
p10 -239.992195659297
p11 3.75637702611614
p12 0.183843198467239
p13 10069.7969832811
if want simple model function:
Sum Squared Error (SSE): 1.61466290688424
Root of Mean Square Error (RMSE): 0.111447224725919
Correlation Coef. (R): 0.999591859922242
R-Square: 0.999183886422807
Parameter Best Estimate
--------- -------------
b0 -190.708200295556
b1 -0.140261199618056
b2 0.00181016348907718
b3 -8.89079975979054E-6
b4 1050.98456432426
b5 -1362.74435026589
b6 553.465126422919
Haroon Zafar on 22 Nov 2022
Thanks a lot Alex. Is there any english version of 1stOpt avaialble? I could only find chinese versions and they just get stuck in between fitting.

### Categories

Find more on Curve Fitting Toolbox in Help Center and File Exchange

R2022a

### Community Treasure Hunt

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

Start Hunting!