How to evaluate functions for every possible input parameter combination?

29 views (last 30 days)
So I have a script set up for scalar inputs currently, but I want to change them to vectors and I want to generate a solution vector for every combination of inputs. For example, I have several expressions that can be calculated based on some inputs, but I don't know which combination of those inputs gives me the value that I desire the most (I'm not looking for a specific value, exactly, but I want to plot all out the outputs so I can look at the graph and determine which I think would be a good solution). Let's say I have three input vectors X, Y, and Z, and I have have a bunch of formulas such as f = x^2 + y, g = y^2 + z, h = x + y + z^3 etc etc. I'm not really sure how to go about obtaining vectors for f, g, or h while also having those values tied to their input parameters, if that makes sense.
X = linspace(0,10,1000);
Y = linspace(0,10,1000);
Z = linspace(0,10,1000);
F = X.^2 + Y;
G = Y.^2 + Z;
H = X + Y + Z.^3;
plot(F,G);
This is what I would do for a very simple example, but here I don't want to step through X, Y, and Z at the same rate. By doing this, everytime that X is 1, so are Y and Z, and everytime X is 10, so are Y and Z. I want those values, but I also want the values for when [X, Y, Z] = [1 7 4] and any other possible combination. But, I also need to be able to note which input conditions caused an output I like. If I am searching for a specific value, and I see that F = 5 somewhere (just a random number) how would I be able to tell what the values of X, Y, and Z were that caused F = 5?
This way, I could come back and alter the plots and filter all the results so that I only see a range of F, G, or H values that I'm looking for, but that's a different issue and I will ask another question if I cannot figure it out. If a very similar question has already been asked, I could not find it and I would appreciate being pointed in that direction as well.
If you know of a toolbox that simplifies this, I can also download that. Any solution is appreciated.

Accepted Answer

Matt J
Matt J on 15 Feb 2021
Edited: Matt J on 15 Feb 2021
You can use ndgrid,
[X,Y,Z]=ndgrid( 0:5:1000);
F = X.^2 + Y;
G = Y.^2 + Z;
H = X + Y + Z.^3;
idx=(F<=5);
locations = [X(idx),Y(idx),Z(idx)] %locations where F<=5;
locations = 402×3
0 0 0 0 5 0 0 0 5 0 5 5 0 0 10 0 5 10 0 0 15 0 5 15 0 0 20 0 5 20
  7 Comments
Steven Lord
Steven Lord on 15 Feb 2021
A = 1:10;
condition1 = A > 5;
condition2 = A < 8;
A(condition1 & condition2)
ans = 1×2
6 7
The and operator is & and the or operator is |.
condition3 = A < 2;
A(condition1 | condition3)
ans = 1×6
1 6 7 8 9 10

Sign in to comment.

More Answers (0)

Categories

Find more on Graph and Network Algorithms in Help Center and File Exchange

Products


Release

R2020b

Community Treasure Hunt

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

Start Hunting!