简体   繁体   English

MATLAB:如何离散化10个变量的函数(当前使用ndgrid和arrayfun)?

[英]MATLAB: how to discretize a function of 10 variables (currently using ndgrid and arrayfun)?

I have a function 我有一个功能

function [output1 output2] = func(v1,v2,v3,v4,v5,v6,v7,v8,v9,v10)

that I want to discretize. 我想离散化。 I am going to be performing optimization that involves this function and I think the optimization's efficiency would benefit from discretizing the function and then doing spline interpolation on the data instead of having to evaluate the continuous function. 我将执行涉及此功能的优化,我认为优化的效率将受益于将函数离散化,然后对数据进行样条插值,而不必评估连续函数。 Essentially I would want a 10-D double for each of output1 and output2 that correlates with varying values of v1, v2, ... v10. 从本质上讲,我希望输出1和输出2的每一个都具有10维双精度,并且与v1,v2,... v10的变化值相关。

With infinite time and memory I would do the following: 有了无限的时间和记忆,我将执行以下操作:

n_pts = 100;

v1 = linspace(v1_min, v1_max, n_pts);
...
v10 = linspace(v10_min, v10_max, n_pts);

[v1g v2g ... v10g] = ndgrid(v1, v2, ... v10);

[output1, output2] = arrayfun(@func, v1g, v2g, ... v10g);

Time and memory (needed to execute ndgrid and arrayfun) obviously do not allow for this. 时间和内存(需要执行ndgrid和arrayfun)显然不允许这样做。 Can anyone think of work-around, or is this problem of discretizing a function of 10 variables totally intractable? 谁能想到解决方法,还是将10个变量的函数离散化的问题完全难以解决?

You are on a totally wrong path. 您走的路完全错误。 Assuming you had infinite memory, you would call your function 100^10 times in the last line. 假设您有无限的内存,那么您将在最后一行调用函数100 ^ 10次。 That would require a lot of time. 那将需要很多时间。 No reasonable optimisation strategy would call your function that many times, that's the reason why all those complicated strategies are developed. 没有合理的优化策略会多次调用您的函数,这就是开发所有这些复杂策略的原因。

You may use your strategy to pre-compute computation intensive sub terms of your function. 您可以使用您的策略来预先计算函数的计算密集型子项。 Replacing a very cost-intensive term with only three variables with a 100^3 lookup table might increase the performance significantly without using to much memory. 用100 ^ 3查找表替换只有三个变量的成本非常高的术语可能会显着提高性能,而不会占用太多内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM