Clear Filters
Clear Filters

Error using mesh - Z must be a matrix, not a scalar or vector, using mesh command

2 views (last 30 days)
Dear community,
I am using this code to create a scatter plot in three dimensions using the attached data. However, I encountered the following error: "Error using mesh - Z must be a matrix, not a scalar or vector." It appears to be related to the values of the vector p21. When I multiply it by 100, the code works fine. However, I require the original data. Could anyone assist me with this? Additionally, I would like to make the interpolating plane transparent.
Thank you in advance!
clc; clear;close all;
load ('data')
x1 = p21;
x2 = vp;
y = ucs;
X = [ones(size(x1)) x1 x2 x1.*x2];
b = regress(y,X)
scatter3(x1,x2,y,'filled')
hold on
x1fit = min(x1):100:max(x1);
x2fit = min(x2):10:max(x2);
[X1FIT,X2FIT] = meshgrid(x1fit,x2fit);
YFIT = b(1) + b(2)*X1FIT + b(3)*X2FIT + b(4)*X1FIT.*X2FIT;
mesh(X1FIT,X2FIT,YFIT)
xlabel('P21 [m/m2]')
ylabel('Vp [m/s]')
zlabel('UCS [MPa]')
view(50,10)
hold off
[~,~,~,~,stats] = regress(y,X)

Accepted Answer

Dyuman Joshi
Dyuman Joshi on 26 Mar 2024
Edited: Dyuman Joshi on 26 Mar 2024
The difference between min(x1) and max(x1) is not 100, thus the vector x1fit generated via colon() is just min(x1). Consequently, YFIT is not a 2x2 matrix as expected by mesh() (or surf() for that matter)
To rectify that issue, use linspace instead of colon, : -
load('data.mat')
x1 = p21;
x2 = vp;
y = ucs;
X = [ones(size(x1)) x1 x2 x1.*x2];
b = regress(y,X)
b = 4x1
27.9661 -5.4556 0.0050 -0.0002
scatter3(x1,x2,y,'filled')
hold on
%Values for verification
min(x1)
ans = 0.2267
max(x1)
ans = 3.4000
%Generate 50 points in between min and max values using linspace()
x1fit = linspace(min(x1), max(x1), 50);
x2fit = linspace(min(x2), max(x2), 50);
[X1FIT,X2FIT] = meshgrid(x1fit,x2fit);
YFIT = b(1) + b(2)*X1FIT + b(3)*X2FIT + b(4)*X1FIT.*X2FIT;
mesh(X1FIT,X2FIT,YFIT)
xlabel('P21 [m/m2]')
ylabel('Vp [m/s]')
zlabel('UCS [MPa]')
view(50,10)
hold off
[~,~,~,~,stats] = regress(y,X)
stats = 1x4
0.8807 17.2248 0.0013 31.8573

More Answers (0)

Community Treasure Hunt

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

Start Hunting!