Numerically integrate over function that gives out a matrix

12 visualizzazioni (ultimi 30 giorni)
How can I solve the following problem?
I have a function that gives a 3X3 matrix as a function of 2 variables. It could look like this
if true
function func_value=tensor_test(k2,k3)
k1=1;
func_value=[k1.*k1 k1.*k2 k1.*k3
k2.*k1 k2.*k2 k2.*k3
k3.*k1 k3.*k2 k3.*k3]
end
end
And I want to integrate over both variables. I tried to do so with integral2:
if true
F=integral2(@tensor_test,-inf,inf,-inf,inf));
end
Maybe I need to use arrayfun for doing so but I don't get it to work. Any help would be very much appreciated. :-)

Risposta accettata

John D'Errico
John D'Errico il 5 Lug 2018
Modificato: John D'Errico il 5 Lug 2018
While integral offers the option of integrating an array valued function, integral2 does not.
However, nothing makes a loop a terrible thing here. Remember that integral2 will typically call the kernel function with MANY points, all at once. If the function was itself array valued, things would get confusing fast. Yes, they COULD have implemented it. But one thing that I avoid when I write code is creating options that will be difficult to understand or use for the user.
As well, the adaptive numerical integration of an array valued function will itself be far less efficient than it would be to integrate each element of the array in turn. This is because that adaptive integration will target regions where is sees something of importance happening. Those regions may easily change when you have many different functions. The result would be to force every function to be evaluated at every point, when many of those function evaluations would be known to be wasted.
So, just use a loop. Nothing stops you from creating an array of function handles, then integrate each element of the array of handles.
  1 Commento
Felix Kelberlau
Felix Kelberlau il 5 Lug 2018
Thanks a lot, John. Not only for the suggestion but also the very understandable and comprehensive explanation of what's happening!

Accedi per commentare.

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by