Semilog plot that includes curve fit

37 visualizzazioni (ultimi 30 giorni)
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?

Risposta accettata

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

Accedi per commentare.

Più risposte (0)

Community Treasure Hunt

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

Start Hunting!

Translated by