Fit curves to data

2 次查看(过去 30 天)
Abdullah
Abdullah 2011-11-27
Hello!
Assume that I have this code:
u0 = pi*4E-7;
I = 5;
N = 1;
X = [ 12.0 ; 8.5 ; 6.0 ; 4.1 ; 3.5 ];
B1 = [ 0.03 ; 0.05 ; 0.1 ; 0.2 ; 0.35 ];
B2= [ 0.07 ; 0.07 ; 0.15 ; 0.24 ; 0.32];
Bex= (B1 + B2)/2;
Bth= 1E5*u0*I*N./(X);
plot(X,Bex,'bo',X,Bth,'r^');
Now, I want to curves to fit data, and their equations not necessery for me now, but it is easy to find them I want that two.
Of course, I do not want to plot like:
plot(X,Bex,'bo-',X,Bth,'r^-');
because the two produced lines are not smooth.
THank you in advance.

采纳的回答

Image Analyst
Image Analyst 2011-11-27
Add this code to the end of your script:
% Fit the Bex points
coeffs1 = polyfit(X, Bex, 2);
newX = linspace(X(1), X(end), 100);
fittedValues = polyval(coeffs1, newX);
hold on;
plot(newX, fittedValues, 'b-');
% Fit the Bth points
coeffs2 = polyfit(X, Bth, 2);
newX2 = linspace(X(1), X(end), 100);
fittedValues2 = polyval(coeffs2, newX2);
hold on;
plot(newX2, fittedValues2, 'r-');
This will fit a quadratic to your data points. You can use another order if you want. Do you know what model you want? If you don't want a polynomial, you'll have to use a different function. Look up fitting in the help. There is also a Curve Fitting Toolbox that you may have that could help.

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Linear and Nonlinear Regression 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by