how to fit a curve in matlab?

9 views (last 30 days)
heba raouf
heba raouf on 17 Jun 2023
Edited: John D'Errico on 17 Jun 2023
i have a curve like shown and i need to make more smmoth -- can you tell the way for doing that if it is possible?

Answers (2)

Cris LaPierre
Cris LaPierre on 17 Jun 2023
Smoothing and curve fitting are different things.
For smoothing, consider using the Smooth Data live task in a live script to interactively adjust the settings.
For curve fitting, consider the options presented here: https://www.mathworks.com/help/matlab/data_analysis/interactive-fitting.html

John D'Errico
John D'Errico on 17 Jun 2023
Edited: John D'Errico on 17 Jun 2023
You really have very little signal, compared to what is apprently noise in those "curves". As such, you would be ill-advised to even try to perform any sophisticated smoothing OR curve fitting. At most, you might decide to model/fit them using a linear or at most a quadratic polynomial. Given as few data points as you have, smoothing will also be difficult to do at all well.
And, since you don't actually provide any data, it is even more difficult to help you. But, for example, suppose you had this data.
x = linspace(0,1,8)';
y = cos(x - 2) + randn(size(x))/3;
plot(x,y,'o')
While there may be some information content in that data, I doubt it. At most, you might dcide a straight line is all it is worth. The problem is, too often we end up overfitting our data.
Polyfit can find the coefficients of a straight line fit. Or, we could use fit. Fit is nice, if you have the curve fitting toolbox.
mdl = fit(x,y,'poly1')
mdl =
Linear model Poly1: mdl(x) = p1*x + p2 Coefficients (with 95% confidence bounds): p1 = 0.8332 (0.1347, 1.532) p2 = -0.4197 (-0.8371, -0.002332)
plot(mdl,x,y,'o')
mdl2 = fit(x,y,'smoothingspline')
mdl2 =
Smoothing spline: mdl2(x) = piecewise polynomial computed from p Coefficients: p = coefficient structure
hold on
plot(mdl2,'g')
And you should see that the fit from a smoothing spline is meaningless garbage, the result of throwing a sophisticated tool at data that lacks any redemptive value. With some understanding of a smoothing spline, we COULD arguably make it do better, but the result here would not be much more than just using a simple low order polynomial model.
Simple things are often the best choices.

Tags

Community Treasure Hunt

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

Start Hunting!