http://www.mathworks.com/matlabcentral/newsreader/view_thread/325437
MATLAB Central Newsreader  1 variable Derrivative of 2 D function...but Error..why??
Feed for thread: 1 variable Derrivative of 2 D function...but Error..why??
enus
©19942014 by MathWorks, Inc.
webmaster@mathworks.com
MATLAB Central Newsreader
http://blogs.law.harvard.edu/tech/rss
60
MathWorks
http://www.mathworks.com/images/membrane_icon.gif

Thu, 27 Dec 2012 16:28:07 +0000
1 variable Derrivative of 2 D function...but Error..why??
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325437#894397
Quynh Tram Nghuyen Thi
Hello all,<br>
very glad to see the room back.<br>
May i start a question about DERRIVATIVE.<br>
Problem: Compute DERRIVATIVE of FUNCTION U(x,y) by 1 variable x , domain 2D:RxR<br>
I did:<br>
mfile diff2.m:<br>
function [du1]=diff2(Xi,Yi,Zi,X,Y,h)<br>
if nargin<6; h=1e4;end;<br>
du1=(interp2(Xi,Yi,Zi,X+h)interp2(Xi,Yi,Zi,Xh))/(2*h)<br>
<br>
EXAMPLE on command window:<br>
x=1:8;<br>
>> y=1:4;<br>
>> [u,v]=meshgrid(x,y)<br>
z=peaks(u,v)<br>
<br>
diff2(u,v,z,u,v)<br>
<br>
===>>>ERROR:Error using ==> interp2 at 140<br>
Wrong number of input arguments.<br>
<br>
Error in ==> diff2 at 6<br>
du1=(interp2(Xi,Yi,Zi,X+h)interp2(Xi,Yi,Zi,Xh))/(2*h)<br>
>> <br>
<br>
WHY?? I DONT UNDERSTAND AT ALL.<br>
ANYONE can give me any error in mfile??<br>
Im waiting, all<br>
thanks alot

Fri, 28 Dec 2012 01:15:09 +0000
Re: 1 variable Derrivative of 2 D function...but Error..why??
http://www.mathworks.com/matlabcentral/newsreader/view_thread/325437#894430
Roger Stafford
"Quynh Tram Nghuyen Thi" <lovelyeverything@yahoo.com.vn> wrote in message <kbhsun$1k$1@newscl01ah.mathworks.com>...<br>
> Problem: Compute DERRIVATIVE of FUNCTION U(x,y) by 1 variable x , domain 2D:RxR<br>
> I did:<br>
> mfile diff2.m:<br>
> function [du1]=diff2(Xi,Yi,Zi,X,Y,h)<br>
> if nargin<6; h=1e4;end;<br>
> du1=(interp2(Xi,Yi,Zi,X+h)interp2(Xi,Yi,Zi,Xh))/(2*h)<br>
> .........<br>
> diff2(u,v,z,u,v)<br>
> ===>>>ERROR:Error using ==> interp2 at 140<br>
> Wrong number of input arguments.<br>
         <br>
The error message is telling you what is wrong. The interp2 function does not accept four arguments. Check its documentation. I believe what you need is:<br>
<br>
du1=(interp2(Xi,Yi,Zi,X+h,Y)interp2(Xi,Yi,Zi,Xh,Y))/(2*h);<br>
<br>
As you have the code at present, you are not using diff2's Y input argument at all, and consequently interp2 has no way of telling what the precise y values are to be at the points where the partial derivatives with respect to x are to be taken. The values of y in Yi are in general offset from the correct locations. In any case interp2 does not know what to do here and is complaining.<br>
<br>
I would also suggest that you beware of letting X+h and Xh fall out of range of Xi  otherwise you will receive NaNs from interp2.<br>
<br>
Also I would suggest that your nomenclature for diff2 should probably be altered to show that differentiation is with respect to x and not y to prevent it from being misused in the future. Or perhaps you need to output both derivatives, one with respect to x and the other with respect to y:<br>
<br>
[dx,dy] = diff2(....)<br>
<br>
Roger Stafford