Numerically integrate over function that gives out a matrix

조회 수: 12 (최근 30일)
Felix Kelberlau
Felix Kelberlau 2018년 7월 4일
댓글: Felix Kelberlau 2018년 7월 5일
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
Felix Kelberlau
Felix Kelberlau 2018년 7월 5일
I did it element by element with 9 functions. But is there no better solution?

댓글을 달려면 로그인하십시오.

채택된 답변

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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by