How to evaluate functions for every possible input parameter combination?

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.

 Respuesta aceptada

Matt J
Matt J el 15 de Feb. de 2021
Editada: Matt J el 15 de Feb. de 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 comentarios

Matt J
Matt J el 15 de Feb. de 2021
Editada: Matt J el 15 de Feb. de 2021
If you have R2016b or higher, you can conserve some memory by using ndgridVecs instead,
[X,Y,Z]=ndgridVecs( linspace(0,10,1000) );
Interesting, will this also let me extract all of the indices where F < 5, or where F > 5 for example? How would the output be formatted in that case since there would be multiple indices?
Matt J
Matt J el 15 de Feb. de 2021
Editada: Matt J el 15 de Feb. de 2021
None of the scenarios you've described require a substantive change in the code. I have edited my answer to run a case where F<=5, just as an illustration. The final result lists 402 such cases when the search grid points are 0:5:1000.
Matt J
Matt J el 15 de Feb. de 2021
Editada: Matt J el 15 de Feb. de 2021
Note that if you know that one of the functions depends on fewer than 3 variables, it is much more efficient to set up ndgrid in terms of those variables only. For example,
[X,Y,Z]=ndgrid( 0:5:1000);
F = X.^2 + Y;
idx=F<=5;
locations = [X(idx),Y(idx)],
I see, and is there a way to obtain the indices where multiple constraints are applied at the same time? That was the ultimate goal that I had in mind when asking the question.
[X,Y,Z] = ndgrid(0:5:1000);
F = X.^2 + Y;
G = X.^3 - 2.*Z;
H = 4.*Z + 3./Y
idx1 = F<=5;
idx2 = G<=10;
idx3 = H>=3;
% would like something like (F<=5 && G<=10 && H>=3)
locations1 = [X(idx1),Y(idx1), Z(idx1)];
locations2 = [X(idx2),Y(idx2), Z(idx2)];
locations3 = [X(idx3),Y(idx3), Z(idx3)];
% would like a single output that satisfies all of those constraints
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
Ahh, I should have known that. Thank you both

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2020b

Preguntada:

el 15 de Feb. de 2021

Comentada:

el 15 de Feb. de 2021

Community Treasure Hunt

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

Start Hunting!

Translated by