The following code computes and plots the fit for a graph. The problem is that each time I have to find where the slope changes on my own. See the following graph:
Is there a way to find it automatically?
start=50; rd1start=1;
ending=100; rd1end=100;
relative=zeros(1,100);
W_esc_half=linspace(1,100,100); % x axis values
relative(1:50)=W_esc_half(1:50).^(1.2); % y axis values fof smaller times
relative(50:100)=W_esc_half(50:100).^(1.5); % y axis values for greater times
figure(1)
fitResults1 = polyfit(log10(W_esc_half(start:ending)),log10(relative(start:ending)),1);
pol=polyval(fitResults1,log10(W_esc_half(start:ending))); % Compute the fit coefficients
a=fitResults1(1);
b=fitResults1(2);
polyfit_str = ['<(?r)^2> ~ ? ^{' (num2str(a)) ' } '] ;
fit1=W_esc_half(start:ending).^a*10^(b);
hold on
loglog((W_esc_half(rd1start:rd1end)),(relative(rd1start:rd1end)),'blue-','LineWidth',2) % draw the original function
loglog((W_esc_half(start:(length(pol)+start-1))),fit1,'cyan--','LineWidth',2); % draw the fit
I usually like doing it quick by searching in the diff
off the signal
subplot(2,1,1)
plot(diff(relative),'.')
subplot(2,1,2)
findpeaks(diff(relative))
But you have a slowly increasing slope in addition to an aburbt change. So if your function is not that nice, you may need to tune the findpeaks
function a bit. On the other hand, if you have constant slopes and abrubt changes, you can also go with find(abs(diff(relative)) > 1)
.