Numerically integrate over function that gives out a matrix

10 Ansichten (letzte 30 Tage)
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. :-)
  1 Kommentar
Felix Kelberlau
Felix Kelberlau am 5 Jul. 2018
I did it element by element with 9 functions. But is there no better solution?

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

John D'Errico
John D'Errico am 5 Jul. 2018
Bearbeitet: John D'Errico am 5 Jul. 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 Kommentar
Felix Kelberlau
Felix Kelberlau am 5 Jul. 2018
Thanks a lot, John. Not only for the suggestion but also the very understandable and comprehensive explanation of what's happening!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Loops and Conditional Statements finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by