How can I use a loop to maximize correlation of multiple graphs?
3 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Mary Juno
am 6 Jan. 2020
Kommentiert: Mary Juno
am 7 Jan. 2020
I have data that is made up of a (mostly) horizontal line, a sloped line, and another horizontal line, as shown below. I am trying to get a best fit for each section and have tried to do this using the correlation coefficient. What I would like to do is create a loop that will determine the best location (j and k variables) to get the length of sections that result in the best fit for the sloped section.

Many of the things I have tried don't work because the correlation is dependent on the values of j and k, making the solution iterative rather than something that could be solved using fminsearch or similar solvers.
Thank you for any help!
function CrackLocationFIT
file = 'FileName.xlsx'
T = readtable(file, 'Sheet', 'Sheet Name'); %import table from excel
[a,~]=size(T); %determine size of imported table
n = a;
increment = 13; %number of lines to skip when reading excel files
xdata = 0:199; %number of data points
for i = 15:increment:n
A = xlsread(file, 'Sheet Name', strcat('C',num2str(i),':GT',num2str(i))); %read y-coordinates from the excel sheet
scatter(xdata,A,'k', '.') %plot scatter for data set
j = 25;
k = 140;
sec1 = 0:(j-1);
sec2 = j:(k-1);
sec3 = k:199;
P1 = mean(A(:,1:j));
P2 = polyfit(sec2,A(:,(j+1):(k)),1);
P3 = mean(A(:,(k+1):200));
yfit1 = repmat(P1(1),1,length(sec1));
yfit2 = P2(1)*sec2+P2(2);
yfit3 = repmat(P3(1),1,length(sec3));
c1 = corrcoef(yfit1,A(:,1:j));
c2 = corrcoef(yfit2,A(:,(j+1):(k)));
c3 = corrcoef(yfit3,A(:,(k+1):200));
plot(sec1,yfit1,'-r') %plot data for data set 1
plot(sec2,yfit2,'-r') %plot data for data set 2
plot(sec3,yfit3,'-r') %plot data for data set 3
xlabel('Location Along Perpendicular Line')
xlim([0 199]) %set x limit for uniform plot
ylabel('Y Displacement [in,]')
title(graphtitle)
hold on
end
hold off
end
0 Kommentare
Akzeptierte Antwort
Stijn Haenen
am 6 Jan. 2020
Maybe something like this can help you:
clear
Y_data=[ones(1,40)*-6,-6:-2/20:-8,ones(1,140)*-8]; %just an example
for x1=1:197
for x2=x1+2:199
hor_section_1=ones(x1,1)*mean(Y_data(1:x1)); %first horizontal section
hor_section_2=ones(200-x2,1)*mean(Y_data(200-x2:200)); %second horizontal section
slope=hor_section_1(1):(hor_section_2(1)-hor_section_1(1))/(x2-x1):hor_section_2(1); %slanting line inbetween
line=[hor_section_1(:)',slope(:)', hor_section_2(:)'];
corr0(x1,x2)=sum(Y_data.*line)/sqrt(sum(Y_data.^2)*sum(line.^2));
end
end
[a,b]=find(corr0==max(corr0(:)));
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!