Error using surf (line 71) Z must be a matrix, not a scalar or vector.

Hi there!
I currently have a tiledLayout plot with six tiles; the tiles are plotted against two variables alpha and beta, and I am using the x-axis for alpha, and colors for beta (using a colormap). Now I want to experiment with a 3D surface plot, using the x- and y-axes for alpha and beta. However, when I run the code
[alpha, beta] = meshgrid(alpha,beta);
surf(alpha,beta,FL_hat);
I am getting the error message that FL_hat must be a matrix, not a scalar or vector.
How can I fix this bug?
In my code, I loop through beta, but I set alpha = linspace(0, 2*pi, 1001), for example.
So, I tried not looping through beta, and just setting beta = linspace(0, 2*pi,1001), but that didn't seem to help.
I also tried using the contour3 function, writing:
[alpha, beta] = meshgrid(alpha,beta);
contour3(alpha,beta,FL_hat);
but then I get the error message, "Error using contour3 (line 44) Z must be at least a 2x2 matrix."
Thanks in advance,

2 Comments

The problem is that FL_hat is not a 2D array with at least 2 elements in each of the dimensions.
Looping through beta is not a problem, provided you use a structure similar to
for beta_index = 1 : number_of_betas
current_beta = beta(beta_index);
FL_hat(beta_index, :) = values_calculated_from_current_beta_and_alpha;
end
Hi @Walter Roberson! My little FL_hat function is in a separate function file, where beta doesn't show up.
Beta only shows up in my data-plotting file, where I use it to vary the actual model inputs to FL_hat, and then make figures.
While beta is not an actual model input, it is an important parameter for my work, so I wanted to plot FL_hat against alpha, an actual model input, and beta, not a model input, but an important descriptive parameter.

Sign in to comment.

 Accepted Answer

Works for me (assuming that FL_hat is 1001x1001 in your case):
alpha = linspace(0, 2*pi, 51);
beta = linspace(0, 2*pi,21);
FL_hat = rand(21,51);
surf(alpha,beta,FL_hat)

12 Comments

Hi Torsten!
Thanks for your quick response!
FL_hat is scalar-valued function. Do I have to change this?
Let's say FL_hat = vx(omega(beta)) + vy(vrel(alpha)), which gives a scalar, as I sweep through beta and alpha.
If alpha and beta are your independent variables, FL_hat(i,j) must be surf-variable, evaluated at (beta(i),alpha(j)).
Did you read the documentation for "surf" ?
for alpha_idx = 1 : numel(alpha)
current_alpha = alpha(alpha_idx);
for beta_idx = 1 : numel(beta)
current_beta = beta(beta_idx);
FL_hat(beta_idx, alpha_idx) = vx(omega(current_beta)) + vy(vrel(current_alpha));
end
end
surf(alpha, beta, FL_hat)
Chances are that there are efficiency improvements available. For example, possibly it might be as simple as
FL_hat = vx(omega(beta(:))) + vy(vrel(alpha(:).'));
Hi Torsten,
I think I might have to add beta as an input to FL_hat.
As is, it is not.
Varying beta changes input values to FL_hat, if that makes sense.
So, I want to vary input values to FL_hat using a parameter called beta.
Hi @Walter Roberson FL_hat's little function file doesn't specify beta as a model input. Should I?
My workflow is:
Vary the inputs to FL_hat by varying beta, and then, make figures with the values of FL_hat.
So, let's say beta goes from 0 to pi/2.
Then alpha, an actual input to FL_hat, might be something like alpha = cos(beta).
Do you think I should go back to the function file for FL_hat, and enlarge the inputs to include beta?
As is, beta only shows up in my data-plotting file, not in the function file that encodes FL_hat.
Thanks!
If FL_hat depends on two independent variables, you can use "surf" for the plot.
If FL_hat depends only on one variable, you should simply use "plot".
Hi Torsten,
Yes, I have to think this through. Beta is not an input to FL_hat, but beta is important to me.
I want to somehow plot FL_hat against alpha (an actual input to FL_hat) and beta (not an actual input, but used to vary values of alpha and other actual model inputs).
Let me talk a walk and think about it; thanks!
The ‘FL_hat’ argument must be a matrix. Ideally, calculate it from the other arguments, although that is not strictly required.
betav = linspace(0, pi/2,50);
alphav = cos(betav);
[alpha, beta] = meshgrid(alphav,betav);
FL_hat = alpha .* beta;
surf(alpha,beta,FL_hat);
colormap(turbo)
xlabel('$\alpha$', Interpreter='LaTeX', FontSize=12)
ylabel('$\beta$', Interpreter='LaTeX', FontSize=12)
zlabel('$\widehat{FL}$', Interpreter='LaTeX', FontSize=12)
.
Hi Star Strider! Am working on this; thanks!
My pleasure!
Whatever takes the place of β in has to have the same dimensions as β.
Hi @Walter Roberson! In your nested for loops, do you have to pre-allocate FL_hat, and then write values to it? If so, how can I do so?
You do not have to pre-allocate FL_hat... it is just a good idea.
FL_hat = zeros(numel(beta), numel(alpha));

Sign in to comment.

More Answers (0)

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Products

Release

R2024b

Asked:

on 30 Dec 2024

Commented:

on 3 Jan 2025

Community Treasure Hunt

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

Start Hunting!