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')
Sarah Hicks
Sarah Hicks 2018 年 11 月 12 日

サインインしてコメントする。


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 Center および File ExchangeStress and Strain についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by