1

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:

enter image description here

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
Osiris
  • 2,783
  • 9
  • 17

1 Answers1

2

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).

max
  • 3,915
  • 2
  • 9
  • 25
  • Thank you very much. However, could I somehow integrate this to the code to do it automatically?? To my understanding, this plot shows the value of Y Axis when you have a peak right? Could I somehow from this find the index of W_esc_half nearest to the peak? –  Mar 13 '20 at 14:22
  • Ok found it! yval_peak=findpeaks(diff(relative)); [~,itM] = min(abs(yval_peak-relative)); start=itM; –  Mar 13 '20 at 14:33
  • rigth, the function only plots if you don't assign an output value to it =) (nice debugging feature) – max Mar 13 '20 at 14:43