2

I am trying to implement Runge-Kutta's 4th order integration method in matlab on a 2-d matrix (x, y). The matrix contains a height value (float) at each point. The idea is place a particle in the matrix and watch it's path as it gets 'pushed' around by the directional vectors u and v.

I have implemented Euler's integration method already using simply the 'gradient' function built in matlab. However, for RK4 integration, I need to take 4 slopes at variable points, not the predetermined slope given by the gradient.

I think the Matlab gradient function itself doesn't have these kind of parameters (from what I understand from the Matlab docs). What kind of approach should I take for this?

If I were to write my own gradient function that computes the slope at any arbitrary point, I am not quite sure how to write it, since it is not a straight-foward function. Are there any suggestions for this approach?

Thanks.

Aaron
  • 1,693
  • 4
  • 26
  • 40
  • Can you show us what it looks like? If it's a function of several variables, you can always find a Jacobian. If it's a constant matrix, the gradient is obviously 0. – Phonon Jun 20 '11 at 15:39
  • Well, that's where I'm confused I guess. It is a constant matrix, and so I'm wondering how to compute a 'derivative' at all. According to http://www.mathworks.com/help/techdoc/ref/gradient.html dx and dy are a collection of vectors pointing in the direction of increasing values of the matrix. – Aaron Jun 20 '11 at 16:11
  • Aaron, the matrix is interpreted as a function f(x,y) over the plane. The components of the gradient are (df/dx) and (df/dy). The derivative is over space, not time. – nibot Jun 20 '11 at 18:06

1 Answers1

0

If I were to write my own gradient function that computes the slope at any arbitrary point, I am not quite sure how to write it, since it is not a straight-foward function. Are there any suggestions for this approach?

I suspect interp2 may be what you're looking for. You can give it your matrix containing the gradient at discrete points, and it will interpolate to find the value of the gradient at any intermediate point.

nibot
  • 14,428
  • 8
  • 54
  • 58
  • Wow. This is exactly what I need! I believe I have a better understanding of what it is I'm trying to do, and now I can use these interpolated points to perform the RK4 algorithm. Thanks a lot! – Aaron Jun 20 '11 at 20:08
  • One more query: Since I want to take the gradient at some arbitrary point, should I do an interpolation on the predetermined gradient matrix? Or should I interpolate on the original matrix first, then do a gradient from there? – Aaron Jun 20 '11 at 20:25
  • I imagine you doing `G = gradient(M)` once, and then using `interp2` on `G` as needed. I'm not really sure how you would do it the other way around. – nibot Jun 20 '11 at 21:53
  • Ok. I was trying to think of how to do it the other way but I couldn't make sense of it. Thanks again, – Aaron Jun 20 '11 at 22:29