How to use polyfit function
조회 수: 2 (최근 30일)
이전 댓글 표시
n=csvread('loadextension.csv',3,0);
l=n(:,1); %this is the extension in mm
force=n(:,2); %this is the force in N
%Part B
area=6.1*50; %in mm
stress=force./area; %in N/mm
strain=l./50;
figure
plot(strain, stress)
xlabel ('Strain')
ylabel ('Stress')
%Part c
%part i
x=strain==linspace(0,.27);
y=stress==linspace(0,.27);
z=polyfit(x,y,4) % code
end
I am trying to estimate the linear portion of a polynomial, and when I am using this polyfit function, I keep getting an error. Can someone help please?
댓글 수: 0
답변 (2개)
madhan ravi
2018년 11월 12일
x=linspace(0,.27);
y=linspace(0,.27);
z=polyfit(x,y,4)
댓글 수: 12
madhan ravi
2018년 11월 12일
perhaps:
n=csvread('LoadExtension.csv',3,0);
l=n(:,1); %this is the extension in mm
force=n(:,2); %this is the force in N
%Part B
area=6.1*50; %in mm
stress=force./area; %in N/mm
strain=l./50;
plot(strain, stress)
hold on
xx=linspace(strain(1),strain(2),1000);
yy=polyfit(stress,strain,1);
yy1=polyval(yy,xx);
plot(xx,yy1,'r')
xlabel ('Strain')
ylabel ('Stress')
Star Strider
2018년 11월 12일
Without your file, it is difficult to provide specific code.
However, these lines:
x=strain==linspace(0,.27);
y=stress==linspace(0,.27);
will produce logical vectors, and since there is no guarantee than any of the ‘stress’ or ‘strain’ data will exactly match the values the linspace calls produce, ‘x’ and ‘y’ could be uniformly zero.
A better option is likely:
x = ismembertol(strain, linspace(0,.27), 0.01);
y = ismembertol(stress, linspace(0,.27), 0.01);
and:
z=polyfit(strain(x),stress(y),4) % code
although the same elements of both vectors would have to be returned, so the elements correspond and the vectors have equal lengths.
댓글 수: 2
Star Strider
2018년 11월 12일
편집: Star Strider
2018년 11월 12일
You have to choose either ‘x’ or ‘y’ for both your ‘stress’ and ‘strain’ vectors.
Either that, or find another way of selecting them, for example:
x = (strain >= 0) & (strain <= 0.27);
y = (stress >= 0) & (stress <= 0.27);
or whatever works for your data.
You still may have to choose one of the two ‘x’ or ‘y’ logical vectors for both your ‘stress’ and ‘strain’ vectors if they do not exactly match.
EDIT 1 — I would just do a linear approximation of that region. The slope of a linear fit is 3.466.
n = xlsread('LoadExtension.csv');
l=n(:,1); %this is the extension in mm
force=n(:,2); %this is the force in N
%Part B
area=6.1*50; %in mm
stress=force./area; %in N/mm
strain=l./50;
figure
plot(strain, stress)
xlabel ('Strain')
ylabel ('Stress')
%Part c
%part i
x = (strain >= 0) & (strain <= 0.27);
y = (stress >= 0) & (stress <= 0.27);
xy = x & y;
z=polyfit(strain(xy),stress(xy),1) % code
f = polyval(z, strain(xy));
figure
plot(strain, stress)
hold on
plot(strain(xy), f)
hold off
xlim([0 0.1])
text(0.04, 0.15, sprintf('Slope = %7.3f', z(1)))
EDIT 2 — The part of your data that you are selecting is at the very beginning. The plot of that region and the regression line through it are:

Do you intend to regress on a different region?
참고 항목
카테고리
Help Center 및 File Exchange에서 Stress and Strain에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



