How to fit a log curve to a scatterplot?

Could anyone advise me on how to a fit a curve to this plot?
The y-axis is decibels and so logarithmic.
I have tried the help page of lsqcurvefit but clearly using the wrong inputs as the curve doesn't align with the points.
x=slopeDist; %18 distances in km
y=PSD; %18 dB values from 50-90
scatter(x,y)
p = polyfit(x,y,1);
f = polyval(p,x);
plot(x,y,'o',x,f,'-')
legend('data','linear fit')
%fits a line but we want a curve!
%Log curve? Doesn't work...
fun = @(x,xdata)x(1)*exp(x(2)*xdata);
x0 = [100,-1];
x = lsqcurvefit(fun,x0,xdata,ydata)
times = linspace(xdata(1),xdata(end));
plot(xdata,ydata,'ko',times,fun(x,times),'b-')
legend('Data','Fitted exponential')
title('Data and Fitted Curve')

댓글 수: 1

Walter Roberson
Walter Roberson 2021년 5월 31일
I tried your data using cftool. All of the models produced fairly bad fits, except for the piecewise interpolations (such a cubic spline), unless you go for something like an 8 term fourier (each term has two coefficients, so 2*8 = 16 coefficients, and you only have 17 datapoints, so it is not surprising you can get something pretty close. But it is also useless, taking wild swings.)

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

답변 (1개)

Star Strider
Star Strider 2021년 5월 28일
편집: Star Strider 2021년 5월 28일

0 개 추천

If you have the Statistics and Machine Learning Toolbox, use the lsline funciton.
Otherwise use polyfit and polyval.
x = 1:10;
y = 10.^(-x/20) + randn(size(x))/10;
figure
scatter(x, mag2db(y), 'p')
grid
lsline
EDIT — Added plot.
.

댓글 수: 6

Louise Wilson
Louise Wilson 2021년 5월 28일
Thanks for your help! I am not sure what the purpose of mag2db() is as the values I have are already in dB? This fits a line but I am looking to fit a curve?
The mag2db function transforms its argument from linear to decibels.
I have no idea what you are starting with, so I have no idea what sort of fit you want.
Since your data are already in dB, changing the code slightly:
x = linspace(0,6E5,18); % Create Vector
y = 10.^(-x*5E-6) * 10^3.5 + rand(size(x))*100; % Original Data
dBy = 20*log10(y); % Convert To dB
figure
plot(x, dBy, 'p')
grid
refline
xlabel('Distance')
ylabel('dB')
.
Louise Wilson
Louise Wilson 2021년 5월 30일
Thanks, I will run this code again to produce the data and show you the raw data that I am plotting with. For the plot you show, I am wondering if it is possible to fit a curver rather than a straight line. So, the y intercept would be around 70 rather than ~64.
Star Strider
Star Strider 2021년 5월 30일
The plot image you posted showed a straight line linear regression. The regression fit would have to be changed if the data changed. I cannot anticipate what that would be at this point, however a likely choice would use polyfit and polyval.
I will let you experiment with those.
Thanks. The reason I have a straight line regression is because that's all I know how to do. The data hasn't changed. I have tried with polyfit too but it produces the same output, a straight line.
p = polyfit(xdata,ydata,1);
f = polyval(p,xdata);
plot(xdata,ydata,'o',xdata,f,'-')
legend('data','linear fit')
This is the best I can do —
LD = load('data.mat');
outsort = sortrows(LD.out); % Not Sorting The Rows Creates Problems For The Regression
x = outsort(:,1);
y = outsort(:,2);
fcn = @(b,x) b(1).*exp(b(2).*x) + b(3);
B = fminsearch(@(b)norm(fcn(b,x)-y), [90; 0.001; 60] );
yfit = fcn(B,x);
expstr = @(x) [x(:).*10.^ceil(-log10(abs(x(:)+(x==0)))) floor(log10(abs(x(:)+(x==0))))];
figure
scatter(x, y)
hold on
plot(x, yfit, '-r')
hold off
grid
text(2.5E+5, 77.5, sprintf('$y = %.1f\\ e^{%.1f\\times 10^{%d} \\ x} +%.1f$',B(1),expstr(B(2)),B(3)), 'Interpreter','latex', 'FontSize',15);
I added the equation for the regression. Remove that if it is not necessary. (The ‘expstr’ function generates the matissa and exponent from ‘B(2)’ here. It looks better than displaying all the leading zeros in the regression equation. If you do not want to display the regression equation, the function is not otherwise necessary for the code.)
.

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

카테고리

도움말 센터File Exchange에서 Get Started with Curve Fitting Toolbox에 대해 자세히 알아보기

제품

릴리스

R2020b

질문:

2021년 5월 28일

댓글:

2021년 5월 31일

Community Treasure Hunt

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

Start Hunting!

Translated by