Plot the slope of a parabola with only the data points being known

21 views (last 30 days)
Deflection (in meters)
The deflection is given in the following line. The data was acquired experimentally.
Thus, no equation is available.
x = linspace(-0.5,0.5,25); %length in (meters)
def_3mm_no_grav = -[0.00 5.82e-1 1.08 1.53 1.94 2.30 2.62 2.89 3.12 3.29 3.41 3.49 3.51 3.49 3.41 3.29 3.12 2.89 2.62 2.30 1.94 1.53 1.08 5.82e-1 0.0]*(10^(-3));
Slope
I tried using polyfit, but I'm not that familiar with the function so I could be using it wrong
slope_3_no_grav = polyfit(x,def_3mm_no_grav,1)
How should I go about acquiring the values/ equation of the slope in order to plot it?

Answers (3)

Alan Stevens
Alan Stevens on 24 Oct 2021
Like this?
x = linspace(-0.5,0.5,25); %length in (meters)
def_3mm_no_grav = -[0.00 5.82e-1 1.08 1.53 1.94 2.30 2.62 2.89 3.12 ...
3.29 3.41 3.49 3.51 3.49 3.41 3.29 3.12 2.89 2.62 2.30 1.94 1.53 ...
1.08 5.82e-1 0.0]*(10^(-3));
coeffs = polyfit(x,def_3mm_no_grav,2);
fit_3mm_no_grav = polyval(coeffs,x);
slope_coeffs = [2*coeffs(1) coeffs(2)];
slope_fit = polyval(slope_coeffs,x);
plot(x,def_3mm_no_grav,'o',x,fit_3mm_no_grav),grid
legend('data','curve fit')
xlabel('x'), ylabel('3mm no grav')
figure
plot(x,slope_fit),grid
xlabel('x'), ylabel('slope of 3mm no grav')

Ive J
Ive J on 24 Oct 2021
Before fitting, it's a good practice to visualize your data to better understand the rough relationship between your variables. In your case, a 2nd order polynomial function seems a reasonable choice:
x = linspace(-0.5,0.5,25); %length in (meters)
y = -[0.00 5.82e-1 1.08 1.53 1.94 2.30 2.62 2.89 3.12 3.29 3.41 3.49 3.51 3.49 3.41 3.29 3.12 2.89 2.62 2.30 1.94 1.53 1.08 5.82e-1 0.0]*(10^(-3));
% fit a curve
coef = polyfit(x, y, 2); % equation would be f(x) = coef(1)*x^2 + coef(2)*x + coef(3)
% now draw the fit
newx = linspace(min(x), max(x), 100);
newy = polyval(coef, newx);
plot(x, y, 'o', newx, newy, 'linewidth', 1.5)

Sargondjani
Sargondjani on 24 Oct 2021
clc;
clear all;
close all;
x = linspace(-0.5,0.5,25); %length in (meters)
def_3mm_no_grav = -[0.00 5.82e-1 1.08 1.53 1.94 2.30 2.62 2.89 3.12 3.29 3.41 3.49 3.51 3.49 3.41 3.29 3.12 2.89 2.62 2.30 1.94 1.53 1.08 5.82e-1 0.0]*(10^(-3));
pp=polyfit(x,def_3mm_no_grav,2);% fit polynomial
p_prime = polyder(pp);%take derivative of this polynomial
plot(x,def_3mm_no_grav,'LineWidth',1.5);%data
hold all;
x_acc = linspace(x(1),x(end),1000);
y_acc = polyval(pp,x_acc);
plot(x_acc,y_acc,'--','LineWidth',1.5);%fitted polynomial
%First order approximation around x1:
x1 = 0.1;
y1 = polyval(pp,x1);%function value;
slope_x1 = polyval(p_prime,x1);%slope at x1
x_dev = -0.1:0.01:0.1;
y_slope = y1+ slope_x1*x_dev;
plot(x1 + x_dev,y_slope,':','LineWidth',1.5)
legend('Data','Fitted poly.','First order approx.')

Products


Release

R2021a

Community Treasure Hunt

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

Start Hunting!