Numerical integration error for a function in x,y that can be analytically integrated!
1 次查看(过去 30 天)
显示 更早的评论
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.
回答(1 个)
unami3
2016-8-23
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 :)
0 个评论
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!