Function handle integration with several variable, matrix dimension error

6 views (last 30 days)
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)

Accepted Answer

Walter Roberson
Walter Roberson on 12 Jun 2017
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 Comments
Walter Roberson
Walter Roberson on 12 Jun 2017
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.
Pro B
Pro B on 14 Jun 2017
Yes, after posting my question I quickly realized what was wrong with numeric integration for the dirac and heaviside functions. I was able to eventually finish the program, by performing the integrations involving the Dirac delta and Heaviside function by hand and putting the result of the integration directly into the program. Thank you very much for all your help!

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox 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!