Matlab Second Derivative Ploting Problem

In summary, the programmer is trying to plot the second derivative of a given equation, but is stuck because plotting the derivative is necessary. He has found some resources on the internet and is working on the program, but is stuck.
  • #1
el_pohl
3
0
Hello, this is my first post in this site, so here goes:

Im working on a Matlab program. I have to calculate the second derivative of a given equation. The problem is that plotting the derivative is necessary, and that's where I'm stuck.

Here's what I've got so far:

% Evaluar f(x) y f'(x) usando diferencias hacia atras.
%
x = -10:0.1:10;
f = x.^3 - 5*x.^2 + 2*x + 8;
df = diff(f);
dx = diff(x);
df_dx = df./dx;
xd = x(2:length(x));
d2f = diff(f,2);
d2x = diff(f,2);
d2f_d2x = d2f./d2x;
%
% Graficar lo anterior.
%
subplot(2,1,1),...
plot(x,f,xd,df_dx,xd,d2f_d2x),...
title('Derivada de un polinomio de quinto grado'),...
xlabel('x'),grid,...
ylabel('f(x)'),...
axis([-4,5,-500,1500]);

On the internet I found out that diff(f,2) would calculate the second diff, I guess that's true. Ignore the spanish text :P.

When I try to run the program, the following appears:

? Error using ==> plot
Vectors must be the same lengths.

Error in ==> C:\MATLAB6p5\work\segunda_diferencial.m
On line 16 ==> plot(x,f,xd,df_dx,xd,d2f_d2x),...

Hope you can help.

Thanks!
 
Physics news on Phys.org
  • #2
De donde eres? Casi no se nota que hablas español.


Well, the command line "diff" shortens one dimension the array:

[tex] diff (\overline{x})=[x_2-x_1, x_3-x_2,...x_n-x_{n-1}] [/tex]

you will obtain a n-1 array dimension.

You should interpolate the last component from interior ones, in order to have n components.

With diff2 is the same, surely you will obtain n-2 components.
 
  • #3
Gracias! Soy del norte de México.

In fact, I can graph the first diff, though what you say does make sense, as with every consecutive derivative one constant term will dissappear.

Interpolate? As with 'interp1'? The last component? d2f_d2x in my code?

Casi no se nada de Matlab :(

Thanks for that, I will try it out, hope I understood you...
 
  • #4
Ok, I got it! Thanks though.
 

Related to Matlab Second Derivative Ploting Problem

1. What is the purpose of plotting the second derivative in Matlab?

The second derivative plot in Matlab is used to analyze the rate of change of a function. It can provide insight into the concavity and inflection points of a curve, and can be helpful in determining the maximum and minimum points of a function.

2. How do I plot the second derivative of a function in Matlab?

To plot the second derivative in Matlab, you can use the diff function to find the derivative of the function, and then use the plot function to plot the resulting derivative. Alternatively, you can use the fplot function to directly plot the second derivative without having to find it first.

3. Can I customize the appearance of the second derivative plot in Matlab?

Yes, you can customize the appearance of the plot by using various options in the plot or fplot functions. This includes changing the color, line style, and adding labels and titles to the plot.

4. How can I verify the accuracy of my second derivative plot in Matlab?

To verify the accuracy of your second derivative plot, you can use the diff function to find the derivative of the function again and compare it to the original second derivative plot. Additionally, you can manually calculate the second derivative using the definition of the derivative and compare it to the plot.

5. Are there any tips for troubleshooting issues with plotting the second derivative in Matlab?

If you are experiencing issues with plotting the second derivative in Matlab, here are a few tips to help troubleshoot: 1) double-check the syntax of your code, 2) check for any errors in the function you are trying to plot, 3) try plotting a simpler function to see if the issue persists, and 4) consult the Matlab documentation or online resources for further assistance.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
4K
  • Special and General Relativity
2
Replies
36
Views
4K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
3K
Replies
2
Views
1K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
5K
  • Calculus and Beyond Homework Help
Replies
7
Views
2K
  • Calculus and Beyond Homework Help
Replies
9
Views
3K
  • Calculus and Beyond Homework Help
Replies
1
Views
3K
Back
Top