Numerically integrate over function that gives out a matrix
조회 수: 12 (최근 30일)
이전 댓글 표시
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. :-)
채택된 답변
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.
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!