Stacking on a multi-variable function

So, I am trying to build a plot from a multi-variable function. I plan to hold 2 variables constant while indexing the 3rd variable, but then for each index of the 3rd variable, i hold that 3rd variable constant and index the 2nd, so on and so forth for the first variable.
For example,
f(x,y,z) = x + y + z
f = x(1) + y(1) + z(1:10)
then, for each z index...
f = x(1) + y(1:10) + z(1)
and then...
f = x(1:10) + y(1) + z(1)
so essentially for this example, I would have 1000 data points. My idea was to do nested for loops, however I don't think this is the right approach.

2 Comments

I'm not quite sure what the inputs and outputs should be.
Is the input only the function itself? But you say you "have 1000 data points".
What is the output? A single plot? Three plots, one for each variable? Or do you need a plot for
f = x(1:10) + y(1) + z(1)
and
f = x(1:10) + y(2) + z(1)
and so on such that you have a thousand plots in total?
I, for one, need a lot of clarification before I could provide any help.
Brett
Brett on 3 Jul 2019
Edited: Brett on 3 Jul 2019
The inputs will be the 3 variables with specific ranges. The output will be the function itself, a singular number from the result of the 3 inputs.
You described it correctly, in that I will have 1000 points total for this example. From the plots I am ultimately trying to optimize the best combination of x,y,z for the highest output achievable.
I believe this might be possible in a mesh/surf plot and the highest point on the 3-d plot will be the most optimized combination, but I am not sure.

Sign in to comment.

Answers (1)

[X, Y, Z] = ndgrid(x, y, z);
F = X + Y + Z;
F will now be numel(x) by numel(y) by numel(z) . You can extract parts of it such as
squeeze(F(7,:,2))
You might also want to look at slice() or isosurface()

5 Comments

Optimizing:
[maxF, maxidx] = max(F(:));
best_x = X(maxidx);
best_y = Y(maxidx);
best_z = Z(maxidx);
I'm confused where I would put my range of inputs and the equation in this
Before the line
[X, Y, Z] = ndgrid(x, y, z);
you would have set x, y, and z to be the values you were talking about in z(1:10) and so on -- the coordinates that you want to evaluate at.
Your equation is expressed at the line
F = X + Y + Z;
If you are writing a function that is passed a function handle such as
f = @(x,y,z) x+y+z;
then
F = f(X, Y, Z);
provide that f is vectorized. For example,
f = @(x,y,z) x.^2 - 3.*y.*z + x.*z;
Thank you, this has worked. Is there a way to plot the matrix I have recieved? It has given me a 2x24x24
You can extract parts of it such as
squeeze(F(2,:,7))
You might also want to look at slice() or isosurface()

Sign in to comment.

Asked:

on 3 Jul 2019

Commented:

on 3 Jul 2019

Community Treasure Hunt

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

Start Hunting!