Numerical integration error for a function in x,y that can be analytically integrated!

1 view (last 30 days)
I am getting the error: "Error in integral2 (line 107)" when trying to numerically evaluate the below surface integral.
func2 = @(x,y) (50+x*cos(y))^3*(50+x*sin(y))+(50+x*cos(y))*(50+x*sin(y))^2;
integral2(func2,0,10,0,2*pi)
This integral has an analytical solution. Could someone help me understand why MATLAB is giving me this error?
I have also tried using quad2d with the same error, which at the base seems to be "Error using * - Inner matrix dimensions must agree" in both cases.

Answers (1)

unami3
unami3 on 23 Aug 2016
Hi Kobye!
I'm having problems with integral2 as well, but they're slightly different. Your problem is quite obvious & simple to fix though, so I thought I'd answer it quickly.
You need to put a period in front of multiplication, division, and power signs in func2 (i.e. you need to put .*, ./, and .^ instead of *, /, and ^). This small change is necessary because integral2 sends in arrays to func2 when it is evaluated and the periods indicate to Matlab that we are performing element-by-element operations. This works for me:
func2 = @(x,y) (50 + x .* cos(y)) .^ 3 .* (50 + x .* sin(y)) + (50 + x .* cos(y)) .*(50 + x .* sin(y)) .^2 ;
That's all. Common mistake :)

Categories

Find more on Programming in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!