MATLAB Matlab second derivative approximation

AI Thread Summary
The discussion centers around a MATLAB code snippet intended to compute the second partial derivative of a function using finite differences. The user encounters an error due to an undefined input argument "x" in the function f9, which is defined to compute the expression exp(sin(x-y)). The error arises in the calculation of f_xx, where the function f9 is incorrectly referenced without proper indexing of its input arguments. The solution provided indicates that the function call should be corrected to f9(x(i), y(j)) to properly pass the current values of x and y. This adjustment is necessary for the code to execute without errors and to compute the desired partial derivative.
splelvis
Messages
10
Reaction score
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
i have find my error,
f9should be f9(x(i),y(j))
 

Similar threads

Replies
1
Views
4K
Replies
2
Views
3K
Replies
1
Views
4K
Replies
13
Views
2K
Replies
4
Views
1K
Back
Top