How to use polyfit function

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?

Answers (2)

x=linspace(0,.27);
y=linspace(0,.27);
z=polyfit(x,y,4)

12 Comments

This is what the graph looks like now, and it is not correct. Any advice?
Sarah Hicks
Sarah Hicks on 12 Nov 2018
Edited: Walter Roberson on 12 Nov 2018
I also need it to
madhan ravi‘s reply : link doesn’t work
I need to get the slope of this linear portion.
Try putting 1 instead of 4
So provide your datas
Here is the data
whats your expected figure
It is a stress-strain graph, so it should look exactly like the graph in the pictures above, just without the orange line. I am trying to find the slope of the linear portion of this graph.
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')

Sign in to comment.

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 Comments

The vectors are not the same length so how do I make them the same?
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?

Sign in to comment.

Categories

Find more on Stress and Strain in Help Center and File Exchange

Asked:

on 12 Nov 2018

Edited:

on 12 Nov 2018

Community Treasure Hunt

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

Start Hunting!