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