Convert from linear to log scale and back again
33 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
I have include some example data.
%fit curve
[xData, yData] = prepareCurveData(log10(plot_data.xdata), plot_data.ydata);
ft = fittype( 'poly1' );
[fitresult, gof] = fit( xData, yData, ft ); %fit model to data
ci=confint(fitresult,0.95);
sp1=plot(fitresult,xData,yData,'ob'); %plot
I have distance on the x-axis, and sound pressure level on the y-axis. The distances are originally in metres, but when I fit a best fit line, the equation I use needs them to be logged. My issue is that I want to convert the x-axis back to metres so it's in a readable format. In addition, I'd like this to be on a logarithmic scale.
I tried the following:
xticklabels({'','','100','','','','','1000'}) %re-label x ticks in metres
set(gca,'XScale','log')
but the x-axis doesn't appear to be logged at all when I do this and the 1000m value disappears when I set the scale to log.
0 Commenti
Risposte (1)
Jon
il 23 Nov 2021
I think you can get what you want using MATLAB's semilogx function
21 Commenti
Jon
il 25 Nov 2021
Modificato: Jon
il 25 Nov 2021
Sorry, I don't have the curve fitting toolbox, so I can't try running your exact code to see if I can replicate the problem. I'm pretty sure the plot3.png that you attached is not the output from your code above, maybe some confustion in the files or something like that. For me using semilogx always produces a log spaced output and your plot3.png has a linear one. As I showed you a few post ago, when I make a ordinary numerical vectors xData, and yfit, and and run the last three lines of code above I get a log spaced x axis with labels 10,100,...
Just to be clear I will post the example again here.
x = 1:1000
yfit = x.^2
semilogx(x,yfit),
get(gca,'xtick')
set(gca,'XTickLabel',New_XTickLabel)
Which produces
Is this the kind of plot you are hoping to get? When you run the above code do you get the same plot I show above
If so, I can't really explain why you don't get the same result with your code and I guess I can't offer you any more help. If the example above, with made up data gives the plot you want, and when you assign the output using fitresult(xData) it doesn't work, then either there is some slight but important difference between our two codes, or maybe there is some aspect of using the curve fit toolbox that I am not aware of, but I'm mystified and frustrated I couldn't get this working for you. Sorry, I hope you can figure it out soon.
Vedere anche
Categorie
Scopri di più su Linear and Nonlinear Regression in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!