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:
Defining x and y in 2D matrix

Subject: Defining x and y in 2D matrix

From: Saud Alkhaldi

Date: 21 May, 2012 20:08:09

Message: 1 of 4

Hi All,

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.

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.

Thanks a lot!

Subject: Defining x and y in 2D matrix

From: Roger Stafford

Date: 21 May, 2012 21:54:06

Message: 2 of 4

"Saud Alkhaldi" wrote in message <jpe7b9$phh$1@newscl01ah.mathworks.com>...
> 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.
- - - - - - - - - -
  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 x-interval 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 y-difference.

  That means at each point z(ix,iy) the approximate first partial derivative with respect to x will be given by the four-point central difference using z(ix-2,iy), z(ix-1,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 x-difference, is to be for the formula. That formula would be:

 dz/dx(ix,iy) = (z(ix-2,iy)-8*z(ix-1,iy)+8*z(ix+1,iy)-z(ix+2,iy))/(12*h)

A similar statement holds for the first partial with respect to y using k and differences in iy.

  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 x-indices 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.

Roger Stafford

Subject: Defining x and y in 2D matrix

From: Saud Alkhaldi

Date: 21 May, 2012 22:25:06

Message: 3 of 4

"Roger Stafford" wrote in message <jpedhu$mri$1@newscl01ah.mathworks.com>...
> "Saud Alkhaldi" wrote in message <jpe7b9$phh$1@newscl01ah.mathworks.com>...
> > 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.
> - - - - - - - - - -
> 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 x-interval 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 y-difference.
>
> That means at each point z(ix,iy) the approximate first partial derivative with respect to x will be given by the four-point central difference using z(ix-2,iy), z(ix-1,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 x-difference, is to be for the formula. That formula would be:
>
> dz/dx(ix,iy) = (z(ix-2,iy)-8*z(ix-1,iy)+8*z(ix+1,iy)-z(ix+2,iy))/(12*h)
>
> A similar statement holds for the first partial with respect to y using k and differences in iy.
>
> 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 x-indices 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.
>
> Roger Stafford

Thanks for the reply, Roger.

I understand what you said but I'm having problem assigning the x and y values using for loops.

How would you write the script to define x and y?

Thanks!

Subject: Defining x and y in 2D matrix

From: Roger Stafford

Date: 21 May, 2012 23:05:08

Message: 4 of 4

"Saud Alkhaldi" wrote in message <jpefc2$rr$1@newscl01ah.mathworks.com>...
> I understand what you said but I'm having problem assigning the x and y values using for loops.
> How would you write the script to define x and y?
- - - - - - - - - -
  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 four-point 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.

Roger Stafford

Tags for 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