How to use polyfit function

2 visualizaciones (últimos 30 días)
Sarah Hicks
Sarah Hicks el 12 de Nov. de 2018
Editada: Star Strider el 12 de Nov. de 2018
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?

Respuestas (2)

madhan ravi
madhan ravi el 12 de Nov. de 2018
x=linspace(0,.27);
y=linspace(0,.27);
z=polyfit(x,y,4)
  12 comentarios
madhan ravi
madhan ravi el 12 de Nov. de 2018
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 el 12 de Nov. de 2018

Iniciar sesión para comentar.


Star Strider
Star Strider el 12 de Nov. de 2018
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 comentarios
Sarah Hicks
Sarah Hicks el 12 de Nov. de 2018
The vectors are not the same length so how do I make them the same?
Star Strider
Star Strider el 12 de Nov. de 2018
Editada: Star Strider el 12 de Nov. de 2018
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?

Iniciar sesión para comentar.

Categorías

Más información sobre Stress and Strain en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by