Matlab无法在yz平面中使用fill3进行绘图

时间:2018-06-26 15:51:49

标签: matlab plot 3d

我正在试图想象驻波。因此,我在3D空间中使用fill3。

我想使其看起来像图像1(image with the wrong angle)。但是那里的视角(最好说是被观看部分的旋转)是错误的,我无法用Matlab函数视图改变它的显示方式。在图像2(image with displaying error中,我可以选择自己喜欢的视角,但在某些情况下fill3不能填充蓝色曲线下方的区域。

我已经尝试使用patch命令,而不是使用'FaceAlpha'。但这没有帮助。

任何建议为什么fill3不能填充该区域以及如何纠正它? maby只能在我的PC上失败吗?

获取两个图像的代码:

% shows plot of accustic wave for different times

clc
clear

a = 0.5; % Amplitude of oscilation
b = 1; % offset
f = 1; % frequency of oscillation
v = 1; % velocity of soundwave

nX = 100; % number of x sampling points
nT = 5; % number of time sampling points
tVec = linspace(0,0.5,nT); % sampling points for time (subplots)
xVec = linspace(-1.5,1.5,nX); % sampling points for position (x-axis)

figure
hold on
for nn = 1:nT
% for nn = 1:1
    T = tVec(nn);
    s1 = a*sin(2*pi*(xVec/v)+2*pi*T*f)+b; % first signal component
    s2 = a*sin(-2*pi*(xVec/v)+2*pi*T*f+pi)+b; % second signal component
    r = mean([s1;s2], 1); % combinde signal

    s1Plot = [b, s1, b];
    s2Plot = [b, s2, b];
    rPlot = [b, r, b];
    xVecPlot = [xVec(1), xVec, xVec(end)];
    tVecPlot = T*ones(1,nX+2);

    fill3(xVecPlot, s1Plot, tVecPlot, 'b', 'FaceAlpha',.2)
    fill3(xVecPlot, s2Plot, tVecPlot, 'r', 'FaceAlpha',.2)
    plot3(xVec, s1, tVecPlot(3:end), 'b', 'LineWidth', 2)
    plot3(xVec, s2, tVecPlot(3:end), 'r', 'LineWidth', 2)
    plot3(xVec, r, tVecPlot(3:end), 'k', 'LineWidth', 3)

end % end for

xlabel('x'); ylabel('y'); zlabel('z')
grid on
axis equal
axis tight
set(gca, 'DataAspectRatio', [1 1 0.1])
view(5,-45)
title('wrong viewing angle')

figure
hold on
for nn = 1:nT
% for nn = 1:1
    T = tVec(nn);
    s1 = a*sin(2*pi*(xVec/v)+2*pi*T*f)+b; % first signal component
    s2 = a*sin(-2*pi*(xVec/v)+2*pi*T*f+pi)+b; % second signal component
    r = mean([s1;s2], 1); % combinde signal

    s1Plot = [b, s1, b];
    s2Plot = [b, s2, b];
    rPlot = [b, r, b];
    xVecPlot = [xVec(1), xVec, xVec(end)];
    tVecPlot = T*ones(1,nX+2);

    fill3(tVecPlot, xVecPlot, s1Plot, 'b', 'FaceAlpha',.2)
    fill3(tVecPlot, xVecPlot, s2Plot, 'r', 'FaceAlpha',.2)
    plot3(tVecPlot(3:end), xVec, s1, 'b', 'LineWidth', 2)
    plot3(tVecPlot(3:end), xVec, s2, 'r', 'LineWidth', 2)
    plot3(tVecPlot(3:end), xVec, r, 'k', 'LineWidth', 3)

end % end for

xlabel('x'); ylabel('y'); zlabel('z')
grid on
axis equal
axis tight
set(gca, 'DataAspectRatio', [0.1 1 1]); view(70,-40)
title('right viewing angle, displaying error')

感谢您的帮助=)

找到了解决方法

将时间T围绕每一步以某种方式有所帮助。 我在Mathworks上提交了一个错误。也许他们可以深入了解它。 工作代码:

% shows plot of accustic wave for different times

clc
clear

a = 0.5; % Amplitude of oscilation
b = 1; % offset
f = 1; % frequency of oscillation
v = 1; % velocity of soundwave

nX = 100; % number of x sampling points
nT = 5; % number of time sampling points
tVec = linspace(0,0.5,nT); % sampling points for time (subplots)
xVec = linspace(-1.5,1.5,nX); % sampling points for position (x-axis)

figure
hold on
for nn = 1:nT
    T = tVec(nn);
    s1 = a*sin(2*pi*(xVec/v)+2*pi*T*f)+b; % first signal component
    s2 = a*sin(-2*pi*(xVec/v)+2*pi*T*f+pi)+b; % second signal component
    r = mean([s1;s2], 1); % combinde signal

    s1Plot = [b, s1, b];
    s2Plot = [b, s2, b];
    rPlot = [b, r, b];
    xVecPlot = [xVec(1), xVec, xVec(end)];
    tVecPlot = T*ones(1,nX+2);

    fill3(xVecPlot, s1Plot, tVecPlot, 'b', 'FaceAlpha',.2)
    fill3(xVecPlot, s2Plot, tVecPlot, 'r', 'FaceAlpha',.2)
    plot3(xVec, s1, tVecPlot(3:end), 'b', 'LineWidth', 2)
    plot3(xVec, s2, tVecPlot(3:end), 'r', 'LineWidth', 2)
    plot3(xVec, r, tVecPlot(3:end), 'k', 'LineWidth', 3)

end % end for

xlabel('x'); ylabel('y'); zlabel('z')
grid on
axis equal
axis tight
set(gca, 'DataAspectRatio', [1 1 0.1])
view(5,-45)
title('wrong viewing angle')

figure
hold on
for nn = 1:nT
    T = round(tVec(nn)*100)/100;
    s1 = a*sin(2*pi*(xVec/v)+2*pi*T*f)+b; % first signal component
    s2 = a*sin(-2*pi*(xVec/v)+2*pi*T*f+pi)+b; % second signal component
    r = mean([s1;s2], 1); % combinde signal

    s1Plot = [b, s1, b];
    s2Plot = [b, s2, b];
    rPlot = [b, r, b];
    xVecPlot = [xVec(1), xVec, xVec(end)];
    tVecPlot = T*ones(1,nX+2);

    fill3(tVecPlot, xVecPlot, s1Plot, 'b', 'FaceAlpha',.2)
    pause(0.01)
    fill3(tVecPlot, xVecPlot, s2Plot, 'r', 'FaceAlpha',.2)
    plot3(tVecPlot(3:end), xVec, s1, 'b', 'LineWidth', 2)
    plot3(tVecPlot(3:end), xVec, s2, 'r', 'LineWidth', 2)
    plot3(tVecPlot(3:end), xVec, r, 'k', 'LineWidth', 3)

end % end for

% pause
xlabel('x'); ylabel('y'); zlabel('z')
grid on
axis equal
axis tight
set(gca, 'DataAspectRatio', [0.1 1 1]); view(70,-40)
title('right viewing angle, displaying error')

0 个答案:

没有答案