getting error in mesh plot

3 views (last 30 days)
Ehtisham
Ehtisham on 15 Jan 2024
Commented: Dyuman Joshi on 15 Jan 2024
% Provided data
KFmax = [20, 40, 60, 80, 100];
KBmax = [20, 40, 60, 80, 100];
KBmax100 = [43.50297072, 46.52477297, 44.39002998, 41.32192544, 38.27960907];
KBmax80 = [41.24659136, 42.59908012, 39.69352292, 36.31204481, 33.18494278];
KBmax60 = [37.40881927, 36.83773483, 33.27098581, 29.76062133, 26.73705543];
KBmax40 = [30.46172094, 27.88434423, 24.07279092, 20.89331763, 18.36534748];
% Create a meshgrid
[X, Y] = meshgrid(KFmax, KBmax);
% Create a 3D surface mesh plot
figure;
surf(X, Y, KBmax100, 'FaceAlpha', 0.5);
hold on;
surf(X, Y, KBmax80, 'EdgeColor', 'k');
surf(X, Y, KBmax60, 'EdgeColor', 'r');
surf(X, Y, KBmax40, 'EdgeColor', 'g');
% Customize the plot
title('R*peak');
xlabel('KFmax');
ylabel('KBmax');
zlabel('R*peak');
grid on;
% Set axis limits
xlim([20, 100]);
ylim([20, 100]);
zlim([0, 100]);
% Set custom x and y ticks
xticks([20, 40, 60, 80, 100]);
yticks([20, 40, 60, 80, 100]);
% Display the legend
legend('KBmax100', 'KBmax80', 'KBmax60', 'KBmax40', 'Location', 'Best');
% Display the grid
grid on;
  1 Comment
Dyuman Joshi
Dyuman Joshi on 15 Jan 2024
surf(X, Y, Z)
When using this syntax where X and Y are matrices, Z must be a matrix of the same size.
Which is not the case here, thus you get the error.
A general solution is to ensure that Z is of the same size as X and Y.
A particular solution might be to use scatteredInterpolant or griddedInterpolant to generate the corresponding Z matrix.

Sign in to comment.

Answers (1)

Hassaan
Hassaan on 15 Jan 2024
@Ehtisham Try this:
% Provided data
KFmax = [20, 40, 60, 80, 100];
KBmax = [20, 40, 60, 80, 100];
KBmax100 = [43.50297072, 46.52477297, 44.39002998, 41.32192544, 38.27960907];
KBmax80 = [41.24659136, 42.59908012, 39.69352292, 36.31204481, 33.18494278];
KBmax60 = [37.40881927, 36.83773483, 33.27098581, 29.76062133, 26.73705543];
KBmax40 = [30.46172094, 27.88434423, 24.07279092, 20.89331763, 18.36534748];
% Create a meshgrid
[X, Y] = meshgrid(KFmax, KBmax);
% Repeat KBmax100 to match the dimensions of X and Y
KBmax100_matrix = repmat(KBmax100, [5, 1]);
% Create a 3D surface mesh plot
figure;
surf(X, Y, KBmax100_matrix, 'EdgeColor', 'k');
colormap('jet'); % Adjust the colormap as needed
% Customize the plot
title('R*peak');
xlabel('KFmax');
ylabel('KBmax');
zlabel('R*peak');
grid on;
% Set axis limits
xlim([20, 100]);
ylim([20, 100]);
zlim([min(KBmax100_matrix(:)), max(KBmax100_matrix(:))]);
% Set custom x and y ticks
xticks([20, 40, 60, 80, 100]);
yticks([20, 40, 60, 80, 100]);
% Display the color bar
colorbar;
% Display the grid
grid on;
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2023a

Community Treasure Hunt

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

Start Hunting!