Fit curves to data
3 views (last 30 days)
Show older comments
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.
0 Comments
Accepted Answer
Image Analyst
on 27 Nov 2011
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 Comments
More Answers (0)
See Also
Categories
Find more on Linear and Nonlinear Regression in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!