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

4 views (last 30 days)
Bodhi Ruffels on 10 Aug 2022
Edited: Matt J on 10 Aug 2022
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
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)") ##### 1 CommentShowHide None
KSSV on 10 Aug 2022

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));