Convert from linear to log scale and back again

조회 수: 33 (최근 30일)
Louise Wilson
Louise Wilson 2021년 11월 23일
댓글: Louise Wilson 2021년 11월 25일
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.

답변 (1개)

Jon
Jon 2021년 11월 23일
I think you can get what you want using MATLAB's semilogx function
  댓글 수: 21
Jon
Jon 2021년 11월 25일
편집: Jon 2021년 11월 25일
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.
Louise Wilson
Louise Wilson 2021년 11월 25일
Yes that is definitely the output. That is why I included a few comments back, the screenshot of running the code and the resultant figure, so you could believe it. I will start a new question.

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Linear and Nonlinear Regression에 대해 자세히 알아보기

태그

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by