3D plot changing color based on multiple z plot ranges

Hello, I am looking to find a way to specify three different ranges within a 3D plot so that they can change to those colors on the Z axis. I am currently having an error using reshape, which works in a previous script but didn't once I put everything in a matrix form.
Error is "Error using reshape, to RESHAPE the number of elements must not change."
clear all; clc; close all;
% Initial Parameters
Zmin = 0.05;
Zmax = 0.5;
Zmax = Zmax+Zmax*0.01;
d_o = 1:.1:10; % in
Z = linspace(Zmin,Zmax, length(d_o));
Z_Percent = 10; % percentage
% Tolerance Ranges %
R_Tol = 0.005;
Y_Tol = 0.01;
G_Tol = 0.03;
% Calculations
AK_Ratio = exp(Z/60); D_test = d_o.*AK_Ratio;
AK_Gap = (D_test./2)-d_o./2;
% Matrix Setup
M = zeros(5,length(d_o));
M(1,:) = d_o;
M(2,:) = Z;
M(3,:) = AK_Ratio;
M(4,:) = D_test;
M(5,:) = AK_Gap
D = zeros(5, length(d_o));
for i = 1:length(d_o)
D(2,i) = M(2,i) - M(2,i)*Z_Percent/100; % Z Lower
D(3,i) = M(2,i) + M(2,i)*Z_Percent/100; % Z Higher
D(4,i) = abs(M(1,i) - M(4,i)/(exp(D(2,i)/60))); % d High
D(5,i) = abs(M(1,i) - M(4,i)/(exp(D(3,i)/60))); % d Low
if D(5,i) <= R_Tol
D(1,i) = 1;
elseif D(5,i) <= Y_Tol
D(1,i) = 2;
else D(5,i) > Y_Tol
D(1,i) = 3;
end
end
D;
AK_Gap;
[Zz, dd] = meshgrid(M(2,:), M(1,:));
AK_Ratio = exp(Zz./60);
Dd = dd.*AK_Ratio;
AK_Gap = ((Dd./2)-(dd./2));
T_Range = abs(dd - (Dd./(exp((Zz.*Z_Percent./100))./60)));
L = D(5,:).*ones(size(Zz));
C(L <= R_Tol) = 1; % first color in colormap
C(R_Tol < L < Y_Tol) = 2;
C(L >= Y_Tol) = 3;
C
C = reshape(C,size(L))
figure()
surf(Zz,dd,L,C,'edgecolor','none');
view(-45, 45)
shading interp
cmap = [1 0 0; 1 1 0; 0 1 0];
colormap(cmap)
hold on;
contour3(Zz,dd,L, [R_Tol R_Tol],'k');
contour3(Zz,dd,L, [Y_Tol Y_Tol],'k');
contour3(Zz,dd,L, [G_Tol G_Tol],'k');
grid on;
xlabel('Impedance (ohm)');
ylabel('Diameter of Center Rod (in)');
zlabel('Tolerance');
title('Tolerance Plot');
colorbar
caxis([R_Tol G_Tol]);

Answers (1)

L is a matrix with size 91 x 91 (8281 elements)
C is a matrix with size 1 x 6188.
Therefore, when you try to reshape C to be the same size as L, you get an error message because the number of elements in C would have to change to make it work.

1 Comment

You might be making it more complicated than it needs to be. What about this? I've trimmed a lot of your code to make the changes more obvious
% Initial Parameters
...
% Tolerance Ranges %
...
% Calculations
...
% Matrix Setup
...
[Zz, dd] = meshgrid(M(2,:), M(1,:));
AK_Ratio = exp(Zz./60);
Dd = dd.*AK_Ratio;
AK_Gap = ((Dd./2)-(dd./2));
L = D(5,:).*ones(size(Zz));
figure()
surf(Zz,dd,L,'edgecolor','none');
cmap = [1 0 0; 1 1 0; 0 1 0];
colormap(cmap)
grid on;
xlabel('Impedance (ohm)');
ylabel('Diameter of Center Rod (in)');
zlabel('Tolerance');
title('Tolerance Plot');
colorbar

Sign in to comment.

Asked:

on 6 Nov 2018

Commented:

on 26 Dec 2018

Community Treasure Hunt

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

Start Hunting!