How to Calculate the Second Derivative of a Curve in Matlab?

In summary, the poster is asking how to calculate the second derivative of a curve obtained from data. They provide code to calculate the second derivative of a curve with respect to x. They then ask how to calculate the second derivative of r with respect to x.
  • #1
anahita
39
0
I have a set of data as follows, How can I calculate the second derivative of the curve obtained from these data.

x=[0.1;0.07;0.05;0.03;0]; r=[-98.9407;-105.7183;-111.2423;-116.0320;-120.0462];
 
Physics news on Phys.org
  • #2
How about fitting it to a curve, then calculating the second derivative analytically from the fitted curve formula? Like this:

Code:
x=[0.1;0.07;0.05;0.03;0];
r=[-98.9407;-105.7183;-111.2423;-116.0320;-120.0462];
plot(x, r, 'r*-', 'LineWidth', 2);
% Fit to a cubic
coefficients = polyfit(x, r, 4)
xFit = linspace(min(x), max(x), 100);
rFit = polyval(coefficients, xFit);
hold on;
plot(xFit, rFit, 'b-', 'LineWidth', 2);
grid on;
legend('Actual', 'fit');

% Set up figure properties:
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'OuterPosition', [0 0 1 1]);
% Get rid of tool bar and pulldown menus that are along top of figure.
set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
set(gcf, 'Name', 'Demo by ImageAnalyst', 'NumberTitle', 'Off')

% Equation is a1*x^4 + a2*x^3 + a3*x^2 + a4*x + a5
% First derivative is 4*a1*x^3 + 3*a2*x^2 + 2*a3*x + a4
% Second derivative is 12*a1*x^2 + 6*a2*x + 2*a3
a1 = coefficients(1)
a2 = coefficients(2)
a3 = coefficients(3)
deriv2 = 12*a1*xFit.^2 + 6*a2*xFit + 2*a3
 
  • #3
Thanks.
But how to calculate second derivative graph x on r?
 
  • #4
What does that mean? I showed you how to calculate the second derivative of r with respect to x. Is that not what you want? Is r the independent variable instead of x? If so, just swap x and r in my code. Or do you just need to know how to call plot()?

Code:
plot(xFit, deriv2, 'b-');
 
  • #6
Irene Kaminkowa said:
anahita,
you can siply use finite differencies: https://en.wikipedia.org/wiki/Finite_difference
You can implement this method with conv(), though with such a small array as the poster posted, the edge effects will reduce the valid region to about one element. However, for a longer vector (more elements), conv() is a good way to take the numerical/empirical derivative (vs. the analytical derivative as in my answer).
 

What is the second derivative in Matlab?

The second derivative in Matlab refers to the rate of change of the slope of a function. It is a measure of the curvature or concavity of a function at a specific point.

How do I calculate the second derivative in Matlab?

To calculate the second derivative in Matlab, you can use the "diff" function twice on the original function. The first "diff" function will give you the first derivative, and the second "diff" function on the result will give you the second derivative.

Can I plot the second derivative in Matlab?

Yes, you can plot the second derivative in Matlab using the "plot" function. You will need to specify the range of values for the independent variable and the calculated second derivative values as the dependent variable.

Why is the second derivative important?

The second derivative is important because it can provide information about the shape and behavior of a function. It can help identify maximum and minimum points, inflection points, and the overall concavity of a function.

Are there any built-in functions for finding the second derivative in Matlab?

Yes, besides using the "diff" function, there are other built-in functions in Matlab that can be used to find the second derivative, such as "gradient", "hessian", and "polyder". These functions may have different applications and may be more efficient for certain types of functions.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
9
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
788
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
846
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
6
Views
1K
Back
Top