Checking Conditions on Multiple-Variable Functions
3 views (last 30 days)
Show older comments
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?
0 Comments
Accepted Answer
Jeff Miller
on 13 Jan 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 Comments
Jeff Miller
on 14 Jan 2019
- 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.
- 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.
More Answers (0)
See Also
Categories
Find more on Logical in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!