http://www.mathworks.com/matlabcentral/newsreader/view_thread/320283
MATLAB Central Newsreader  Defining x and y in 2D matrix
Feed for thread: Defining x and y in 2D matrix
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

Mon, 21 May 2012 20:08:09 +0000
Defining x and y in 2D matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320283#877488
Saud Alkhaldi
Hi All,<br>
<br>
This is my first post in this website and I had to post here because I couldn't find the answer anywhere else so please help.<br>
<br>
I have a question that's asking me to ise the 4 point central difference to calculate the partial derivatives of the function z=f(x,y). However, I'm only given a set of data in a 100x101 matrix. How can I define my x and y vectors so I can proceed with finding the derivatives.<br>
<br>
Thanks a lot!

Mon, 21 May 2012 21:54:06 +0000
Re: Defining x and y in 2D matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320283#877497
Roger Stafford
"Saud Alkhaldi" wrote in message <jpe7b9$phh$1@newscl01ah.mathworks.com>...<br>
> I have a question that's asking me to ise the 4 point central difference to calculate the partial derivatives of the function z=f(x,y). However, I'm only given a set of data in a 100x101 matrix. How can I define my x and y vectors so I can proceed with finding the derivatives.<br>
         <br>
If you're being asked to use the four point central difference formula to calculate derivatives in a 100 x 101 matrix of z = f(x,y) values, the presumption I would make is that the x and y values correspond linearly to the indices of the matrix. In other words the xinterval between two horizontally adjacent matrix values z(ix,iy) and z(ix+1,iy) is understood to be some constant value h and between two vertically adjacent values z(ix,iy) and z(ix,iy+1) is a constant k in ydifference.<br>
<br>
That means at each point z(ix,iy) the approximate first partial derivative with respect to x will be given by the fourpoint central difference using z(ix2,iy), z(ix1,iy), z(ix+1,iy) and z(ix+2,iy), and doesn't involve z(ix,iy). It is up to you to decide what h, the xdifference, is to be for the formula. That formula would be:<br>
<br>
dz/dx(ix,iy) = (z(ix2,iy)8*z(ix1,iy)+8*z(ix+1,iy)z(ix+2,iy))/(12*h)<br>
<br>
A similar statement holds for the first partial with respect to y using k and differences in iy.<br>
<br>
You will note that you cannot apply this formula at the four edges of your matrix since for example you have no values of z for xindices 0 and 1 or for 101 and 102. You will have to decide to either derive the appropriate third order approximation at the edges or use a cruder difference method there.<br>
<br>
Roger Stafford

Mon, 21 May 2012 22:25:06 +0000
Re: Defining x and y in 2D matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320283#877498
Saud Alkhaldi
"Roger Stafford" wrote in message <jpedhu$mri$1@newscl01ah.mathworks.com>...<br>
> "Saud Alkhaldi" wrote in message <jpe7b9$phh$1@newscl01ah.mathworks.com>...<br>
> > I have a question that's asking me to ise the 4 point central difference to calculate the partial derivatives of the function z=f(x,y). However, I'm only given a set of data in a 100x101 matrix. How can I define my x and y vectors so I can proceed with finding the derivatives.<br>
>          <br>
> If you're being asked to use the four point central difference formula to calculate derivatives in a 100 x 101 matrix of z = f(x,y) values, the presumption I would make is that the x and y values correspond linearly to the indices of the matrix. In other words the xinterval between two horizontally adjacent matrix values z(ix,iy) and z(ix+1,iy) is understood to be some constant value h and between two vertically adjacent values z(ix,iy) and z(ix,iy+1) is a constant k in ydifference.<br>
> <br>
> That means at each point z(ix,iy) the approximate first partial derivative with respect to x will be given by the fourpoint central difference using z(ix2,iy), z(ix1,iy), z(ix+1,iy) and z(ix+2,iy), and doesn't involve z(ix,iy). It is up to you to decide what h, the xdifference, is to be for the formula. That formula would be:<br>
> <br>
> dz/dx(ix,iy) = (z(ix2,iy)8*z(ix1,iy)+8*z(ix+1,iy)z(ix+2,iy))/(12*h)<br>
> <br>
> A similar statement holds for the first partial with respect to y using k and differences in iy.<br>
> <br>
> You will note that you cannot apply this formula at the four edges of your matrix since for example you have no values of z for xindices 0 and 1 or for 101 and 102. You will have to decide to either derive the appropriate third order approximation at the edges or use a cruder difference method there.<br>
> <br>
> Roger Stafford<br>
<br>
Thanks for the reply, Roger. <br>
<br>
I understand what you said but I'm having problem assigning the x and y values using for loops.<br>
<br>
How would you write the script to define x and y?<br>
<br>
Thanks!

Mon, 21 May 2012 23:05:08 +0000
Re: Defining x and y in 2D matrix
http://www.mathworks.com/matlabcentral/newsreader/view_thread/320283#877502
Roger Stafford
"Saud Alkhaldi" wrote in message <jpefc2$rr$1@newscl01ah.mathworks.com>...<br>
> I understand what you said but I'm having problem assigning the x and y values using for loops.<br>
> How would you write the script to define x and y?<br>
         <br>
I can't answer the question you have asked, Saud. The idea I was expressing to you was that you don't need full information about x and y to invoke the fourpoint central difference method, provided you make the assumption that x and y are linear functions of the two respective indices ix and iy of your data matrix: x = h*ix+a and y = k*iy+b. You don't need to know the a and b values, but only h and k. If this is a homework assignment and they didn't specify this, probably they assumed h and k were both equal to 1  that is, they may have considered the indices themselves to be the actual x and y values. I'm afraid this is something you will have to decide for yourself.<br>
<br>
Roger Stafford