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!