How to make a linear regression from an excel file

조회 수: 8 (최근 30일)
Nick Elias
Nick Elias 2021년 3월 15일
편집: Rik 2021년 3월 23일
Hi all,
This question is related to mechanics of materials if anyone can help. I have an excel file (a large one, 102k rows) with three columns: cycle count, force and strain.
Data was taken at a frequency of 8Hz (8 datapoints/sec, each cycle count lasts for about 40 rows) and I need to write a code to find the two points in each cycle (one in the increasing portion of the cycle, the other in the decreasing portion) where the correlation between strain and force becomes no longer linear (i.e. R^2 less than 95%), that is the elastic limit of the material. Basically the output would be a matrix with a number of rows equal to the number of cycles and three columns: cycle count, max elastic limit, min elastic limit.
Thank you for anything!
  댓글 수: 4
Rik
Rik 2021년 3월 15일
OK, so which part of the coding is causing you trouble? From what I understand you want to do an analysis on the parts of your data separatly for every value of cycle. How did you try to separate the several chunks?
Nick Elias
Nick Elias 2021년 3월 15일
I have no clue about how to go about that in Matlab, basically the array size will not be the same for every cycle as I have to keep track of the absolute value of strain and calculate the correlation for a set of datapoints until the strain trend changes direction.

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

답변 (1개)

Rik
Rik 2021년 3월 15일
편집: Rik 2021년 3월 18일
You can use the unique function and a loop.
(edited to include a custom function to extract a specific strain value)
data=xlsread('Sample Data.xlsx');
count=data(:,1);
strain=data(:,2);
force=data(:,3);
unique_counts=unique(count);
elasticlimit=zeros(size(unique_counts));
meanstrain=zeros(size(unique_counts));
for n=1:numel(unique_counts)
c=unique_counts(n);
rows=ismember(count,c);
strain_=strain(rows);
force_=force(rows);
%now you have the strain and the force for a specific cycle
%here you can do the processing you need
elasticlimit(n)=find_elasticlimit(force_,strain_);
%you can also use simple functions here, like mean():
meanstrain(n)=mean(strain_);
end
disp(elasticlimit.')
0.0109 -0.0084 0.0060 -0.0093 0.0070 -0.0098 NaN
function elasticlimit=find_elasticlimit(force,strain,thres)
%Write an explanation for what this function does here.
if nargin<3,thres=0.99;end%set default
found=false;
for N=2:numel(strain)
R=corrcoef(strain(1:N),force(1:N));
R=R(2);
if R<thres,found=true;break,end
end
if found
elasticlimit=strain(N-1);
else
%return an error condition or error
%you should decide what is appropriate
elasticlimit=NaN;
end
end
  댓글 수: 27
Nick Elias
Nick Elias 2021년 3월 23일
I tried going over Onramp but there wasn't something covering the second output of max/min. No Matlab users around me either so I'm stuck. I used the search function but couldn't come across a similar situation.
Rik
Rik 2021년 3월 23일
편집: Rik 2021년 3월 23일
Unaccepting my answer comes across as a bit childish.
You need to use the second output of min. That second output contains indices. You can use those indices on the original vector you use in min, or for another vector.
v1=[9 0 6];
v2=[2 4 6];
[val,ind]=min(v1)
val = 0
ind = 2
v2(ind)
ans = 4

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

카테고리

Help CenterFile Exchange에서 Stress and Strain에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by