![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/989015/image.png)
Creating equation gives error using fit function.
3 次查看(过去 30 天)
显示 更早的评论
Hello,
I'm trying to make an equation from data with fit function. In a data it seems like a straight line and what I'm expecting in my equation.but not sure why it give me larger error. It gives me a coefficient of the equation which is not fitting to my data sets.
Is there any another function that can help me to set a data sets and getting coefficient of the equation?
I'm trying wih the available data and I divide column as per requirement and I'm ploting column 13 Vs. 22. I expecting same results as in the column 14. But it's deviating.
load trial_hand_fixing.txt
h_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,4);
h_delta_DC_SPHERE_250_G1_R8= trial_hand_fixing(:,22);
k_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,13);
g_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,15);
w_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,16);
m_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,17);
k_w_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,19);
u50_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,5);
u50_corrected_DC_SPHERE_250_G1_R8 = trial_hand_fixing(:,14);
temp_DC_SPHERE_250_G1_R8= trial_hand_fixing(:,2);
x=h_delta_DC_SPHERE_250_G1_R8;
x1=h_delta_DC_SPHERE_250_G1_R8-11;
y=k_DC_SPHERE_250_G1_R8;
f=fit(x1,y,'poly3') %using poly3 fir function for the calculating coefficient
y_est = f.p1*x1.^3 + f.p2*x1.^2 + f.p3*x1 + f.p4;
plot(x1, y , 'b-', x1, y_est, 'r-', 'LineWidth', 1)
xlabel('h/delta')
ylabel('k')
legend('real','polynominal3')
axis square
title('DC SPHERE 500 G04 2 POLY3')
error= y_est-y;
rmse= sqrt(mean(error.^2))
2 个评论
Monica Roberts
2022-5-6
It looks like fit is doing an okay job here. It's a bit more clear if you plot with markers instead of lines:
plot(x1, y , 'bx', x1, y_est, 'ro', 'LineWidth', 1)
There is a larger deviation in the data on the left side so the majority of the error is coming from there.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/989015/image.png)
回答(1 个)
Nipun
2023-12-29
Hi Nilanshu,
I understand that you are trying to retrieve the polynomial equation from the "fit" function in MATLAB.
On analyzing your code, I notice that you are fitting the data using a third degree polynomial regression
f=fit(x1,y,'poly3') %using poly3 fir function for the calculating coefficient
The coefficients can be accessed using "coeffvalues" function on the "cfit" object as under:
coeff_array = coeffvalues(f);
Here, "coeff_array" represents the array of coefficients. For instance to get the second coefficient "p2", do the following
p2 = coeff_array(2);
I am attaching the link to a similar question for more information: How can I obtain the coefficient values, confidence intervals, and other data generated by the CFTOOL GUI within the Curve Fi... - MATLAB Answers - MATLAB Central (mathworks.com)
Hope this helps.
Regards,
Nipun
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!