0

I am using contourf to generate a contour plot for a 2 variable function.

My function is Z = f(x,y).

I generate x and y through meshgrid function in matlab and generate values for Z and then plot the contour using contour(x,y,z).

I want to be able to calculate the volume under this generated contour. Can anyone please help ?

Thanks in advance

SecretAgentMan
  • 2,856
  • 7
  • 21
  • 41
Vignesh R
  • 53
  • 11
  • Area or volume? The title and question mention the opposite – Benoit_11 May 13 '15 at 20:10
  • Sorry about that. Volume not area. I'll it in the post too – Vignesh R May 13 '15 at 20:35
  • 2
    You can use [`integral2`](http://www.mathworks.com/help/matlab/ref/integral2.html) to integrate `z = @(x,y) f(x,y)` over the region you plotted. – TroyHaskin May 13 '15 at 20:45
  • integral2 should work rite ? I was planning on using trapz to calculate the volume but running it twice along the 2 dimensions. – Vignesh R May 13 '15 at 21:13
  • Isnt the integral2 function limited to planar surfaces ? My surface is not a plane it is a curved surface. – Vignesh R May 13 '15 at 21:27
  • No. `integral2` will integrate any explicit, two-variable function using an adaptive method to ensure accuracy. – TroyHaskin May 13 '15 at 22:15
  • I tried the integral2 function but I ended up getting a negative value for the volume. My function is of the form: model = @(x,y) (b(1)*(b(3) - ((b(3)-1)*exp(-b(2)*x)))) - (b(4)*y); xmin = 0; xmax = 9000; ymin = 1; ymax = 90; tox = integral2(model,xmin,xmax,ymin,ymax,'Method','iterated'); I ran this on matlab and ended up with a negative value. I can't exactly put my finger on what I am doing wrong. @TroyHaskin could you please help – Vignesh R May 19 '15 at 01:30
  • You're probably not doing anything wrong. Depending on the values of `b`, your `model` has a long-run behavior (large `x`) of `b(1)*b(3) - b(4)*y` if `b(2)` is positive. This will lead to negative volumes since `integral2` computes the **signed** volume (analogous to a negative integral in one-dimension; e.g., `int(-x^2,-1,1)`). – TroyHaskin May 19 '15 at 01:42

1 Answers1

1

couldn't you simply use a integral approximation like a riemann sum? Assuming uniform spacing for x and y something like this should work

delta_x = x(2) - x(1);
delta_y = y(2) - y(1);
vol = sum(Z(:)) * delta_x * delta_y;

This will not be the EXACT volume, but an approximation. Since you know your function you would get a more accurate answer by performing the integration of the function. But if you did not know the function you would use this method or any other numerical integration method.

From calculus we know that an actual integral is just a reimann sum where the width of each interval is infinitely small, so this should be a valid approximation

andrew
  • 2,451
  • 1
  • 15
  • 22
  • I am not sure. My surface is curved because the function involves exponential and linear terms which is why I am a little confused – Vignesh R May 13 '15 at 21:15
  • the function output `z` can be any shape. As long as your x and y have uniform spacing. meaning `x(n) - x(n-1) == x(k) - x(k-1)` for all `n and k` where `n != k and n,k are integers` the same would go for y. Basically this method places a cube of width `delta_x` length `delta_y` and height `z` at every value in z, and adds them all together to produce `vol` – andrew May 13 '15 at 21:43
  • so your suggestion is find value of the function Z at all n values of x and y. Then proceed to sum everything and multiply it by delta x and delta y correct ? @andrew – Vignesh R May 19 '15 at 02:40
  • correct @VigneshR it is exactly the same as using a reimann sum for area approximation http://mathworld.wolfram.com/RiemannSum.html but now we are doing volume, so instead of each interval being a rectangle of width `delta_x` and height `y` we have cubes as described in my previous comment. An integral is nothing more than a riemann sum where the interval (delta_x and delta_y for a double integral) is infinitely small. This will not be the EXACT volume, but an approximation. You would get a more accurate answer if you actually performed the integration of your function. – andrew May 19 '15 at 16:37