Checking Conditions on Multiple-Variable Functions

2 次查看(过去 30 天)
I have defined a few functions f1, f2, f3, f4, ... They depend on the same multiple variables x, y, z, u, v, ... Each of these variables are defined over a range of real numbers. From these ranges, I want to find a data set that satisfies various conditions such as f1 becomes minimum, f2 becomes maximum, f3 becomes a poisitve real number with no imaginary, f4 stays limited to a range, ...
I can write this code discretely by defining a grid matrix created by variables and then finding the values of all functions at all grid points and finally apply the conditions and selecting the dataset using indexing. But this method becomes extremely slow because to get accurate results, long variable points must be created that exponentially increase the number of grid points and memory.
Could you show me a code that does the job using functions? Are there options that can be applied to this code for faster and more accurate solutions?

采纳的回答

Jeff Miller
Jeff Miller 2019-1-13
Try fminsearch. Write a global error function to sum up the deviations from all conditions you want to satisfy, and fminsearch will try to minimize that function. Your function might look something like this:
error = f1 + (-f2) + abs(imag(f3)) + ...
  2 个评论
S H
S H 2019-1-14
Thanks. Two comments on your answer. First, when a functions needs to be maximized, its error can be a few order of magnitudes more that the error of a function that needs to minimized or zeroed. So adding the functions constraints might not be the best way to go. Second, applying built-in abs() or similar commands on functions generate this error:
Undefined function 'abs' for input arguments of type 'function_handle'.
Jeff Miller
Jeff Miller 2019-1-14
  1. You can use some constants to adjust the relative importance of the different function values, something like this: error = f1 + (-f2)/1000 + abs(imag(f3)) It may take some trial and error to find the right adjustments for your problem.
  2. No idea what's going on with the function_handle error. Based on your initial post, I was assuming that f3 returned a value with both real and imaginary parts, and imag(f3) should then give you the size of the imaginary part.

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Matrix Indexing 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by