Function handle integration with several variable, matrix dimension error
3 次查看(过去 30 天)
显示 更早的评论
Hi! I am using function handles to integrate with several variables (note: I made the same program using symbolic integration, but it takes a really long time). However, I keep getting an error in matrix dimensions in the last line of my when I plug in R(1) and I really can't figure out what is wrong. Any tips would be greatly appreciated!
FUN_1 = @(y_1,y_2,x_1,x_2)sum(heaviside(y_1-1)).*dirac(1,y_2-1).*(-1/2*log((x_1-y_1).^2+(x_2-y_2).^2))+sum(dirac(y_1-1).*dirac(y_2-1));
Q_1 = @(x_1,x_2)integral2(@(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),1,2,1,2);
FUN_2 = @(y_1,y_2,x_1,x_2)sum(heaviside(y_1-1).*dirac(1,y_2-1))+sum(dirac(y_1-1).*dirac(y_2-1))*(-1/2*log((x_1-y_1).^2+(x_2-y_2).^2));
Q_2 = @(x_1,x_2)integral2(@(y_1,y_2)FUN_1(y_1,y_2,x_1,x_2),1,2,1,2);
S = @(x_1,x_2)Q_1(x_1,x_2)+Q_2(x_1,x_2);
R = @(x_2)integral(@(x_1)S(x_1,x_2),1,2);
b = R(1);
disp(b)
0 个评论
采纳的回答
Walter Roberson
2017-6-12
integral() calls the function passing in a vector of values. So your S will be called with a vector x_1, so your Q_1 will be called with a vector x_1. Your Q_1 calls integral2, which is going to call FUN_1 with array y_1 and y_2 (the same size) and the vector x_1 (of different size). Your FUN_1 expects x_1 and y_1 to be compatible size for subtraction, but the two are not compatible.
You could use the 'ArrayValued' option of integral so that it only passes in a scalar for x_1, at a cost to efficiency. You should look more carefully at which operations can be vectorized. Sometimes it helps to use arrayfun() along the way.
4 个评论
Walter Roberson
2017-6-12
Your dirac(y_2-1) and dirac(1,y_2-1) will only ever be non-zero if y_2 == 1 exactly, which is not at all certain to occur when you are doing 2D integration.
When you use integral() calls, but not when you use integral2() calls, you can use the Waypoints option to force evaluation at particular points.
But if you did manage to force evaluation at y_2 == 1 exactly then the result of the dirac() would be inf, and the result of the dirac(1) would be -inf . Those would then be multiplied by the heaviside(y_1-1), which would be 0 for y_1 < 1 -- and inf or -inf times 0 is NaN. Having a NaN in a sum "pollutes" the sum, forcing it to be NaN, so you would end up with a NaN result.
You should, in short, never do numeric integration with a dirac() term. You also need to worry about doing numeric integration with a heaviside term because the formal definition of Heaviside leaves the value at 0 undefined -- so you need to know which value heaviside(0) is going to return. For the last couple of releases, the value of heaviside(0) can be configured by using sympref(); the default is 1/2 -- but it is common for people to use heaviside when they mean the unit step function, where u(0) has been defined as 1.
What you should do instead of numeric integration with a dirac term (and ideally, with a heaviside term too) is break the integration up into as many pieces as needed, and add the results together, taking care to handle the exact boundaries in a manner that is meaningful for your situation.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Symbolic Math Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!