Curve fitting for multiple variable
9 views (last 30 days)
Show older comments
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 .
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1201068/image.png)
2 Comments
Matt J
on 21 Nov 2022
It would be easiest if you showed us the model function you want applied to the different curves.
Accepted Answer
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 = readtable('https://www.mathworks.com/matlabcentral/answers/uploaded_files/1201063/ReqFittingData.xlsx', 'VariableNamingRule','preserve')
VN = T1.Properties.VariableNames
Viv = regexp([VN{:}], '(\d*\.\d*)|\d*', 'match')
Viv = str2double(Viv)
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
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))
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
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.
.
4 Comments
Alex Sha
on 22 Nov 2022
try the model function below, where, x1=SoC, x2=Vi, y=Req
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1202533/image.jpeg)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1202538/image.jpeg)
if want simple model function:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1202543/image.jpeg)
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
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1202548/image.jpeg)
More Answers (0)
See Also
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!