Polyfitting in for loop

2 vues (au cours des 30 derniers jours)
Dan
Dan le 11 Juin 2012
Hello,
I am working on fitting a straightline through a section of data. I have about fifty of these all on the same excel sheet. I am trying to get matlab to automatically run through all of the different data sets and output the slope and index number of each one (and possibly some other data from the excel sheet) in an array. However it is not quite working... the x vector is constant through all of the curve fitting, and only the y value changes by shifting over one column at a time, this term is then divided by a constant (vector) and this is why there is a ratio. I have pasted my code below. Any ideas?
PS. I have not worked out the display command part yet either, i am aware its not really doing anything, just trying to get the polyfit to fit all the datasets first. Thanks.
for n = 1 : 1 : 48
AmplP=data (9:10008,n); % Amplitude Array P wave
NFFT=2^nextpow2(L); % Nyquist Frequecy maybe, length of Fourier window fP=Fs/2*linspace (0,1,NFFT/2+1); AmplP_2=AmplP (1:L); % Amplitude through all steps YP=fft (AmplP_2,NFFT)/L;
RatioP1=log(absalumYP./absYP);
clippedfP=fP(21:91); clippedRatioP1=RatioP1(21:91); clippedfP2=clippedfP(1,70); clippedRatioP12=clippedRatioP1(70,1); plot (clippedfP,clippedRatioP1) hold on % Fit a line thru the data and plot the result over the data plot temp = polyfit(clippedfP2 (n,:) ,clippedRatioP12 (n,:) ,1); % least squares fitting to a line a1 = temp(2) % y-intercept of the fitted line a2 = temp(1) % slope of fitted lines Pratiofit = a1+a2*clippedfP; plot(clippedfP,Pratiofit) axis([1*10^5 5.5*10^5 -2 5]) hold on
disp('Experiment Event Pressure SlopeP SlopeS1 SlopeS2')
end

Réponse acceptée

Tom Lane
Tom Lane le 12 Juin 2012
Without having your data and just looking at your code, I observe that you have
for n = 1 : 1 : 48
...
clippedfP2=clippedfP(1,70);
...
temp = polyfit(clippedfP2 (n,:) ,clippedRatioP12 (n,:) ,1); % least squares fitting to a line
so it appears you are getting a variable clippedfP2 with just one row and then you are indexing into its nth row.
Also, in order to get more and better answers, consider editing your submission and putting all the code into "code" format.

Plus de réponses (0)

Community Treasure Hunt

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

Start Hunting!

Translated by