Semilog plot that includes curve fit

39 次查看(过去 30 天)
I have used the curve fitting app to generate code to plot the data and the curve fit. I am new to MatLab but it looks like the relevant line of code is:
h = plot( fitresult, xData, yData );
This generates a plot with the data and curve.
However, I want a semilog plot. So I tried;
h = semilogx(xData, yData,'o');
which generates a semilog plot of the data points WITHOUT the fitted curve. When I try the following I get an error:
h = semilogx(fitresult, xData, yData,'o');
It looks like the plot function allows the "fitresult" to be added in a single line, but semilogx does not. Is that correct?
How do I add the fitted curve to the semilog plot?

采纳的回答

Dave B
Dave B 2021-11-13
编辑:Dave B 2021-11-13
It turns out that semilogx is a shortcut for plot and setting the XScale property on the axes. So you can just do:
plot(fitresult, xData, yData, 'o')
set(gca, 'XScale', 'log')
(Maybe this is obvious, but just in case, this just affects how the data are plotted not how the fit is applied...if you want to fit some data logarithmically you might consider transforming xData before fitting, plotting linearly, and modifying the ticks/ticklabels.)
  3 个评论
Dave B
Dave B 2021-11-13
编辑:Dave B 2021-11-13
If you use the fit() function (which what the tool generates when you generate code?) then it's in the second output, documented as gof here: https://www.mathworks.com/help/curvefit/fit.html
load census
f=fit(cdate,pop,'poly2');
[f,gof]=fit(cdate,pop,'poly2');
gof.rsquare
ans = 0.9987
Robert Demyanovich
Robert Demyanovich 2021-11-13
I thought I tried that before I asked the question. Anyway, it does work. Thanks.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Curve Fitting Toolbox 的更多信息

标签

Community Treasure Hunt

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

Start Hunting!

Translated by