Matlab Second Derivative Ploting Problem

Click For Summary

Discussion Overview

The discussion revolves around a problem related to calculating and plotting the second derivative of a polynomial function using Matlab. Participants explore the technical aspects of using the 'diff' function and address issues encountered during plotting.

Discussion Character

  • Technical explanation
  • Homework-related

Main Points Raised

  • The original poster describes their attempt to calculate the second derivative and plot it, encountering an error due to vector length mismatches.
  • One participant explains that using the 'diff' function reduces the array size, leading to mismatched lengths when plotting.
  • The original poster acknowledges the issue with array dimensions and considers using interpolation to address the problem.
  • Another participant confirms that the same dimensionality issue applies to the second derivative calculation.

Areas of Agreement / Disagreement

Participants generally agree on the dimensionality issue caused by the 'diff' function, but the discussion does not reach a consensus on the best approach to resolve the plotting error.

Contextual Notes

The discussion highlights the need for interpolation to match vector lengths for plotting, but specific methods or implementations are not fully resolved.

Who May Find This Useful

Individuals working with Matlab for numerical analysis, particularly those dealing with derivatives and plotting functions.

el_pohl
Messages
3
Reaction score
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
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.
 
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 disappear.

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...
 
Ok, I got it! Thanks though.
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
5K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 5 ·
Replies
5
Views
6K
  • · Replies 36 ·
2
Replies
36
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 7 ·
Replies
7
Views
2K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 1 ·
Replies
1
Views
3K