Checking Conditions on Multiple-Variable Functions

3 visualizaciones (últimos 30 días)
S H
S H el 13 de En. de 2019
Comentada: Jeff Miller el 14 de En. de 2019
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?

Respuesta aceptada

Jeff Miller
Jeff Miller el 13 de En. de 2019
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 comentarios
S H
S H el 14 de En. de 2019
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 el 14 de En. de 2019
  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.

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Matrix Indexing en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by