Semilog plot that includes curve fit

43 visualizaciones (últimos 30 días)
Robert Demyanovich
Robert Demyanovich el 13 de Nov. de 2021
Comentada: Robert Demyanovich el 13 de Nov. de 2021
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?

Respuesta aceptada

Dave B
Dave B el 13 de Nov. de 2021
Editada: Dave B el 13 de Nov. de 2021
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 comentarios
Dave B
Dave B el 13 de Nov. de 2021
Editada: Dave B el 13 de Nov. de 2021
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 el 13 de Nov. de 2021
I thought I tried that before I asked the question. Anyway, it does work. Thanks.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Curve Fitting Toolbox en Help Center y File Exchange.

Etiquetas

Community Treasure Hunt

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

Start Hunting!

Translated by