MATLAB Answers

How to evaluate functions for every possible input parameter combination?

조회 수: 21(최근 30일)
Craig Atkinson
Craig Atkinson 15 Feb 2021
댓글: Craig Atkinson 15 Feb 2021
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;
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.

채택된 답변

Matt J
Matt J 15 Feb 2021
편집: Matt J 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;
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
Craig Atkinson
Craig Atkinson 15 Feb 2021
Ahh, I should have known that. Thank you both

댓글을 달려면 로그인하십시오.

추가 답변(0개)




Community Treasure Hunt

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

Start Hunting!

Translated by