Fitting a line of best fit on a plot only between a restricted domain.

3 views (last 30 days)
I have gathered data from a experiment and wish to put a line of best fit on a plot that is similar to that of an logarithmic curve. My code plots a stress strain curve shown below, and I need the gradient of the linear potion of the line which is equal to Young Modulus. Instead of just guessing the gradient I wanted to try put a line of best fit (with its equation) considering only the data between x values from [-2,0]. Can this be done? My code is as follows:
%% Insert and plot data
clc
clear
clearvars
Steel1020 = readmatrix('Excelsteeldata.txt');
Time = Steel1020(:,1); %Selects column 1 of file: in Seconds
Force = Steel1020(:,2).*1000; %Selects column 2 of files converts kN=N
Elongation = Steel1020(:,3); %Selects column 3 of files in mm
%% FIGURES
figure(1)
plot(Elongation, Force)
title("Load-Extension Curve for 1020 Carbon Steel")
xlabel("Force(N)")
ylabel("Elongation (mm)")
Stress = Force / (pi.*((0.5.*10.023)^2)); %since stress = force/crosss sectional area
Strain = Elongation / 50; % since strain = elongation / inital length
figure(2)
plot(Strain, Stress)
title("Stress-Strain Curve for 1020 Carbon Steel")
xlabel("Strain (%)")
ylabel("Stress (MPa)")

Answers (1)

Matt J
Matt J on 10 Aug 2022
Edited: Matt J on 10 Aug 2022
You can do it interactively with the brush tool and basic fitting menu options on the figure toolbar.
Or, programmatically, you could use polyfit()
region=-2<=Strain & Strain<=2;
p=polyfit(Strain(region), Stress(region),1);
fun=@(x) polyval(p,x);
hold on;
plot(Strain,Stress,Strain,fun(Strain));

Categories

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

Community Treasure Hunt

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

Start Hunting!