How to use polyfit function

2 次查看(过去 30 天)
Sarah Hicks
Sarah Hicks 2018-11-12
编辑: Star Strider 2018-11-12
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?

回答(2 个)

madhan ravi
madhan ravi 2018-11-12
x=linspace(0,.27);
y=linspace(0,.27);
z=polyfit(x,y,4)
  12 个评论
madhan ravi
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
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 个评论
Sarah Hicks
Sarah Hicks 2018-11-12
The vectors are not the same length so how do I make them the same?
Star Strider
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 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