Got Questions? Get Answers.
Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
1 variable Derrivative of 2 D function...but Error..why??

Subject: 1 variable Derrivative of 2 D function...but Error..why??

From: Quynh Tram Nghuyen Thi

Date: 27 Dec, 2012 16:28:07

Message: 1 of 2

Hello all,
very glad to see the room back.
May i start a question about DERRIVATIVE.
Problem: Compute DERRIVATIVE of FUNCTION U(x,y) by 1 variable x , domain 2D:RxR
I did:
m-file diff2.m:
function [du1]=diff2(Xi,Yi,Zi,X,Y,h)
if nargin<6; h=1e-4;end;
    du1=(interp2(Xi,Yi,Zi,X+h)-interp2(Xi,Yi,Zi,X-h))/(2*h)

EXAMPLE on command window:
x=1:8;
>> y=1:4;
>> [u,v]=meshgrid(x,y)
z=peaks(u,v)

diff2(u,v,z,u,v)

===>>>ERROR:Error using ==> interp2 at 140
Wrong number of input arguments.

Error in ==> diff2 at 6
    du1=(interp2(Xi,Yi,Zi,X+h)-interp2(Xi,Yi,Zi,X-h))/(2*h)
>>

WHY?? I DONT UNDERSTAND AT ALL.
ANYONE can give me any error in m-file??
Im waiting, all
thanks alot

Subject: 1 variable Derrivative of 2 D function...but Error..why??

From: Roger Stafford

Date: 28 Dec, 2012 01:15:09

Message: 2 of 2

"Quynh Tram Nghuyen Thi" <lovelyeverything@yahoo.com.vn> wrote in message <kbhsun$1k$1@newscl01ah.mathworks.com>...
> Problem: Compute DERRIVATIVE of FUNCTION U(x,y) by 1 variable x , domain 2D:RxR
> I did:
> m-file diff2.m:
> function [du1]=diff2(Xi,Yi,Zi,X,Y,h)
> if nargin<6; h=1e-4;end;
> du1=(interp2(Xi,Yi,Zi,X+h)-interp2(Xi,Yi,Zi,X-h))/(2*h)
> .........
> diff2(u,v,z,u,v)
> ===>>>ERROR:Error using ==> interp2 at 140
> Wrong number of input arguments.
- - - - - - - - - -
  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:

 du1=(interp2(Xi,Yi,Zi,X+h,Y)-interp2(Xi,Yi,Zi,X-h,Y))/(2*h);

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.

  I would also suggest that you beware of letting X+h and X-h fall out of range of Xi - otherwise you will receive NaNs from interp2.

  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:

 [dx,dy] = diff2(....)

Roger Stafford

Tags for this Thread

No tags are associated with this thread.

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us