Matlab second derivative approximation

In summary, The code is calculating the partial derivative with respect to xx using a for loop with a given number of steps in both x and y directions. The function f9(x,y) is used to calculate the values for the derivative, and the code is set up to plot the result using the surf function. However, there was an error in the code due to an undefined input argument "x" in the function f9. This has been corrected by changing the code to f9(x(i),y(j)).
  • #1
splelvis
10
0
clear all;
nx=50;
ny=30;
hx=pi/nx;
x=linspace(0,pi,nx+1);
y=linspace(0,pi,ny+1);
x_plus_h=x+hx.*ones(1,nx+1);
x_minus_h=x-hx.*ones(1,nx+1);
for i=1:nx+1
for j=1:ny+1
f_xx(j,i)=(f9(x_plus_h(i),y(j))-2*f9+f9(x_minus_h(i),y(j)))./(hx.^2);
end;
end;
[xx,yy]=meshgrid(x,y);
surf(xx,yy,f_xx);
title('partial derivative w.r.t.xx')
,

and the other new document,f9,
function l=f9(x,y)
l=exp(sin(x-y));and then the system said have error,

? Input argument "x" is undefined.

Error in ==> f9 at 2
l=exp(sin(x-y));
Error in ==> partial_derivxx at 11
f_xx(j,i)=(f9(x_plus_h(i),y(j))-2*f9+f9(x_minus_h(i),y(j)))./(hx.^2);where should i corrected to make it work?
thanks

anyone can help
 
Last edited:
Physics news on Phys.org
  • #2
i have find my error,
f9should be f9(x(i),y(j))
 

1. What is the purpose of using a second derivative approximation in Matlab?

The purpose of using a second derivative approximation in Matlab is to estimate the rate of change of a function at a specific point. It allows for the calculation of the curvature or the concavity of a function, which can provide valuable insights into the behavior of the function.

2. How does Matlab calculate the second derivative approximation?

Matlab calculates the second derivative approximation using the three-point central difference formula. This formula uses three points on the function to estimate the second derivative at a specific point. The formula is (f(x+h)-2f(x)+f(x-h))/h^2, where h is a small step size.

3. Can the second derivative approximation be used for any type of function?

Yes, the second derivative approximation can be used for any type of function, as long as the function is continuous and differentiable at the point of interest. It is commonly used for polynomial, trigonometric, and exponential functions.

4. How accurate is the second derivative approximation in Matlab?

The accuracy of the second derivative approximation in Matlab depends on the step size used. A smaller step size will result in a more accurate approximation. However, using a very small step size can also lead to numerical errors. It is important to balance accuracy and computational efficiency when choosing a step size.

5. Are there any built-in functions in Matlab for calculating the second derivative approximation?

Yes, Matlab has a built-in function called "diff" that can be used to calculate the second derivative approximation. This function takes in the function and the step size as parameters and returns an approximation of the second derivative at the specified point. There are also other third-party functions available for more advanced calculations of the second derivative approximation.

Similar threads

  • MATLAB, Maple, Mathematica, LaTeX
Replies
5
Views
997
  • Programming and Computer Science
Replies
4
Views
614
  • MATLAB, Maple, Mathematica, LaTeX
Replies
4
Views
569
  • MATLAB, Maple, Mathematica, LaTeX
Replies
13
Views
2K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
2
Views
2K
Replies
1
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
3K
  • MATLAB, Maple, Mathematica, LaTeX
Replies
1
Views
939
Back
Top