how to find curve fiitng equation through custom equation

16 ビュー (過去 30 日間)
parshwa
parshwa 2025 年 1 月 22 日
コメント済み: Image Analyst 2025 年 1 月 25 日 15:15
Question:
I have measured data for the discharging state of charge (SOC) and inverter power of a Redox Flow Battery (RFB). From this data, I need to determine the auxiliary power loss of the battery. I’m trying to find an equation for this non-linear relationship using curve fitting or other methods.
I want to create a model where the equation is in the form of f(X)+f(Y) where X represents SOC and Y represents inverter power. The key requirement is that X and Y should be treated as independent variables, meaning they should not be combined into one term in the equation.
I have tried some equations where X and Y are separate in the equation, but I am getting a low R-squared value (around 0.6). However, when I combine Xand Y together in one equation, the R-squared value improves to 0.9, which is better.
Now, I need to apply piecewise linearization to the model with X and Ytogether. How can I proceed with this approach and still maintain the independent nature of X and Y in the equation for the fitting process? Any advice or guidance would be much appreciated!
  1 件のコメント
Sam Chak
Sam Chak 2025 年 1 月 23 日

Even if you provide the data, there are relatively few data points (black dots) available as seen in the figure, making it challenging to fit a reliable surface function.

I'm not sure, but you can probably decipher the equation if you can model the rate of change of the power loss in some form of differential equations.

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

採用された回答

Image Analyst
Image Analyst 2025 年 1 月 23 日
I understand that you've measured x and y points and you want to fit these to an equation for the discharging state of charge (SOC) and inverter power of a Redox Flow Battery (RFB). You can use fitnlm but you need to know the form of the equation and then it can determine the parameters. Like if the theoretical formula is ZMeasured = a*x + b*y + c then it can determine what a, b, and c are, if you supply ZMeasured, x and y. Do you have the theoretical form of the equations for the discharging state of charge (SOC) and inverter power of a Redox Flow Battery (RFB)?
I'm attaching some demos of fitnlm.
  3 件のコメント
parshwa
parshwa 2025 年 1 月 24 日 10:10
編集済み: parshwa 2025 年 1 月 24 日 10:10
  1. Combined x and y equation:p1 + p2 * exp(-p3 * x) + p4 * x.^2 + p5 * y.^2 + p6 * (x .* y), where x and y are interdependent, I achieved an R2R^2R2 value of 0.8, which suggests the model has a good fit.
  2. Separate independent x and y equation:p1.*exp(-p2.*x) + p3.*x.^2 + p4.*sin(p5.*x) + p6.*x.^3 + p7.*exp(-p8.*y.^2) + p9.*y.^3 + p10.*cos(p11.*y) + p12.*y.^4 equation, I obtained a root mean square error (RMSE) of 0.6.
My Questions:
  1. Is it possible to apply a piecewise fitting approach with the combined x and y equation, given that x and yare dependent on each other? If so, how would I do this?
  2. Is there any other method or approach (apart from the curve fitting tool) that I can use to derive the equation with better accuracy or a better fit? I am not very familiar with advanced techniques in the curve fitting tool, so any suggestions would be helpful.
Image Analyst
Image Analyst 2025 年 1 月 25 日 15:15
  1. If you want to just fit z as a function of x, and then z as a function of only y, of course you can do that. But you have to understand how to interpret it and what you're modeling. Of course the function for x and the function for y will have different parameters, and of course they will also be different parameters than the function fitting both x and y. To do it, just make your model equation and send in just x and z info fitnlm, and then do it again where you send in only z and y to fitnlm.
  2. If you want a better fit to your training data, you can of course just do an interpolation rather than a regression, which is what fitnlm does. The "fit" will be perfect for the training data. However the z values you get for validation (non-training data for which you know the "correct" z values) will be different for the interpolation than they'll be for the regression. Which one is more accurate would have to be determined for multiple sets of validation data, meaning that just because regression is more accurate for one particular set of validation data, doesn't mean it will be more accurate for a different set of validation data.
Have you tried the Regression Learner app on the Apps tab of the tool ribbon? It will let you try out many different modeling techniques all in one run, and return the accuracy of each one. Then you can pick the best, and export the model as a .mat file, which you can then read in to your program and pass your test data to it.
Definitions:
training data = data for which you know the z values for, and the x and y values, and for which you will train your fit/model.
validation data = data for which you know the z values for, and the x and y values, and for which you will assess how well your fit/model worked. Validation data should not contain the same points as your training data.
test data = data for which you do NOT know the z values for, and the x and y values which you assume are correct, and for which you will run your fit/model on. You don't know the true z values for these x and y points but if you trust the model you created, you just trust that the z values it produced for your input x and y values are correct/reasonable and you use them.

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

その他の回答 (2 件)

John D'Errico
John D'Errico 2025 年 1 月 22 日
編集済み: John D'Errico 2025 年 1 月 22 日
You can't. Yes, I want world peace. But do you think that just wanting it is sufficient? Nothing I do will change it either. The same applies to you in this regard. You want a model in a specific form. But your data is not consistent with what you want. (We don't have your data, unfortunately, so this is not something I can prove to you.) But just looking at the picture, and reading what you have said tells me that it is very likely true.
Effectively, the model is nonlinear in a way where the variables X and Y interact with each other. Change X, and the shape changes for Y. It might be rather simple in how the variables interact, but it is very likely true. Yes, it could also be that you have just not chosen good forms for F(X) and G(Y). It might be that a simple transformation of the data, a rotation, perhaps, might untangle the interaction. Again, we cannot know, despite my having tried the crystal ball toolbox. It did say you might be coming into money soon though. Or maybe your heirs will be coming into money. Very unclear.
If you want better help, then you need to post your data. A picture is not sufficient, even if it might be worth a thousand words. We need 1001, at least. (No, two pictures will not help either. Things are very nonlinear in this respect.)

William Rose
William Rose 2025 年 1 月 23 日
編集済み: William Rose 2025 年 1 月 23 日
[Edit: fix spelling.]
I agree with @John D'Errico that providing your data is an excellent idea.
You say "I need to apply piecewise linearization to the model with X and Y together". You want the model to be additively separable: h(x,y)=f(x)+g(y), and you are OK with nonlinear f(x) and nonlinear g(y).
You can do it piecewise by defining ranges for one variable of the other, or both.
For example, define region boundaries for x: xb=[0,33,67,100]. Fit the data in each region separately:
H1=f1(x)+g1(y) fit it for the data (xb(1)<=x<xb(2), all y)
H2=f2(x)+g2(y) fit it for the data (xb(2)<=x<xb(3), all y)
H3=f3(x)+g3(y) fit it for the data (xb(3)<=x<xb(4), all y)
Or you could do it with bounds for y, instead of x. Or you could have ranges for both x and y. But that seems too obvious, so I doubt you want that. One drawback with the approach above is that the combined model will probably be discontinuous at the region edges.
If you want C0 continuity, you could map x and/or y in a piecewise continuous way to an intermediate variable, then fit the intermediate variable.
Example, using x only. x is mapped to xint, which varies in a continuous peicewise linear way from 0 to 1.The The mapping from x to xint is fully determined by 2 parameters: the horizontal and vertical coordinates (x1,xint1) of the breakpoint.
x=10:2:40;
x1=35; % x coordinate of breakpoint, shoud be min(x)<=x1<=max(x)
xint1=0.3; % xint when x=x1
% 0<xint1<1 for monotonic xint(x)
xint=xmapped(x,x1,xint1);
function xint=xmapped(x,x1,xint1)
xint=zeros(size(x));
for i=1:length(x)
if x(i)<=x1
xint(i)=xint1*(x(i)-min(x))./(x1-min(x));
else
xint(i)=xint1+(x(i)-x1).*(1-xint1)./(max(x)-x1);
end
end
end
plot(x,xint,'-bx'); grid on; xlabel('x'); ylabel('xint')
If you do not require that 0<xint1<1, then xint(x) could be a non-monotonic peicewise linear function of x, without adding any more adjustable parameters.
Use this approach to fit some nonlinear data.
% Make some data to fit
xdata=[2:2:98]'; ydata=zeros(size(xdata)); N=length(xdata);
for i=1:N,
if xdata(i)<80,
ydata(i)=100+10*sin(xdata(i)*pi/80);
else
ydata(i)=100-70*(exp((xdata(i)-80)/20)-1);
end
end
figure
plot(xdata,ydata,'ok',MarkerSize=4,MarkerFaceColor='k');
grid on; xlabel('x'); ylabel('y')
Define the model. Note that the model is a quadratic equation, while the data is a half-cycle of a sinusoid connected to an exponential. That is a good reflection of real data, which doesn't come from our model equation.
% myMod: y=a0+a1*xint+a2*xint^2
myMod=fittype(@(a0,a1,a2,x1,xint1,x) a0+a1*xmapped(x,x1,xint1)+a2*xmapped(x,x1,xint1).^2);
Fit the model
f1 = fit( xdata, ydata, myMod,StartPoint=[100,40,-100,80,.5],Upper=[120,100,0,max(xdata),1],Lower=[80,10,-300,min(xdata),0]);
disp(f1)
General model: f1(x) = a0+a1*xmapped(x,x1,xint1)+a2*xmapped(x,x1,xint1).^2 Coefficients (with 95% confidence bounds): a0 = 100.4 (100.1, 100.7) a1 = 84.02 (81.7, 86.35) a2 = -186.8 (-189, -184.7) x1 = 79.46 (79.23, 79.69) xint1 = 0.4583 (0.4504, 0.4663)
plot(f1,xdata,ydata); grid on
That is better than I expected. You could extend this idea to a 2D data set:
z=h(x,y)=f(xint(x))+g(yint(y))
which uses a mapping from x to xint and a mapping from y to yint. Each of those mappings introduces two adjustable parameters, if you have one breakpoint in each. Or you could remap just one of the independent variables.
  1 件のコメント
parshwa
parshwa 2025 年 1 月 23 日
thanks,for your help I have derived equations using the curve fitting tool, and here’s what I have so far:
  1. Combined x and y equation:p1 + p2 * exp(-p3 * x) + p4 * x.^2 + p5 * y.^2 + p6 * (x .* y), where x and y are interdependent, I achieved an R2R^2R2 value of 0.8, which suggests the model has a good fit.
  2. Separate independent xxx and yyy equation:p1.*exp(-p2.*x) + p3.*x.^2 + p4.*sin(p5.*x) + p6.*x.^3 + p7.*exp(-p8.*y.^2) + p9.*y.^3 + p10.*cos(p11.*y) + p12.*y.^4 equation, I obtained a root mean square error (RMSE) of 0.6.
My Questions:
  1. Is it possible to apply a piecewise fitting approach with the combined x and y equation, given that x and yare dependent on each other? If so, how would I do this?
  2. Is there any other method or approach (apart from the curve fitting tool) that I can use to derive the equation with better accuracy or a better fit? I am not very familiar with advanced techniques in the curve fitting tool, so any suggestions would be helpful.

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

カテゴリ

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

Community Treasure Hunt

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

Start Hunting!

Translated by