Help with contour plot

2 次查看(过去 30 天)
Hello veryone,
i am trying do a contour plot of the stability of a mechanical system: so given a meshgrid X-Y and for a varying parameter k1, i calculate Z based on two conditions (B and D) and plot for Z=0. As k1 can be one of five different values i would like to have five different curves on the same plot.
but i only get two curves (which are also not right) and in the command windows this warning appears:
"Warning: Contour not rendered for constant ZData"
could anybody help me find the problem? thank you very much
here ist the script i am using
clear; clc; clf;
M = 700; J = 400; a1 = 1.85; a2 = 1.85; L = 3.7;
n = 101;
m = 50000;
k_a12 = linspace(-m, m, n);
k_a21 = linspace(-m, m, n);
[X, Y] = meshgrid(k_a12, k_a21);
v = [0 0];
clr = ['r','g','b','c','k'];
b = 100000;
a = 5;
k1 = linspace(-b, b, a);
k2 = 0;
LegendsStrings = cell(numel(k1),1);
Z = zeros(n, n);
for q = 1:a
for i = 1:n
for j = 1:n
B = k1(q)*(J + M*a1^2) + k2*(J + M*a2^2) + (X + Y)*(J - M*a1*a2);
D = k1(q)*k2 - X.*Y;
if B(i, j) >= 0 && D(i, j) >= 0
Z(i, j) = D(i, j);
else
Z(i, j) = -1;
end
end
end
LegendsStrings{q} = ['k_1 = ', num2str(k1(q))];
contour(X, Y, Z, v, 'LineWidth', 1.2, 'LineColor', clr(q), 'DisplayName', LegendsStrings{q});
hold on
end
legend()
xlabel('k_a_1_2')
ylabel('k_a_2_1')
grid on

采纳的回答

Aquatris
Aquatris 2024-7-17
You actually do have 5 lines but they are on top of each other thats why you do not see them. So your Z matrix for k1(1) and k1(2) are the same. Also Z matrix for k1(3) to k1(5) are the same.
So here is you code for 4 points instead of 101 to visaulize easily. You can also click and delete the lines in your plot to see the underlying lines:
clear; clc; clf;
M = 700; J = 400; a1 = 1.85; a2 = 1.85; L = 3.7;
n = 4;
m = 50000;
k_a12 = linspace(-m, m, n);
k_a21 = linspace(-m, m, n);
[X, Y] = meshgrid(k_a12, k_a21);
v = [0 0];
clr = ['r','g','b','c','k'];
b = 100000;
a = 5;
k1 = linspace(-b, b, a);
k2 = 0;
LegendsStrings = cell(numel(k1),1);
Z = zeros(n, n);
for q = 1:a
for i = 1:n
for j = 1:n
B = k1(q)*(J + M*a1^2) + k2*(J + M*a2^2) + (X + Y)*(J - M*a1*a2);
D = k1(q)*k2 - X.*Y;
if B(i, j) >= 0 && D(i, j) >= 0
Z(i, j) = D(i, j);
else
Z(i, j) = -1;
end
end
end
fprintf('Z for k1 = %.2f is\n',k1(q))
disp(Z)
LegendsStrings{q} = ['k_1 = ', num2str(k1(q))];
contour(X, Y, Z, v, 'LineWidth', 1.2, 'LineColor', clr(q), 'DisplayName', LegendsStrings{q});
hold on
end
Z for k1 = -100000.00 is
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Warning: Contour not rendered for constant ZData
Z for k1 = -50000.00 is
-1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1 -1
Warning: Contour not rendered for constant ZData
Z for k1 = 0.00 is
1.0e+09 * -0.0000 -0.0000 0.8333 2.5000 -0.0000 -0.0000 0.2778 -0.0000 0.8333 0.2778 -0.0000 -0.0000 2.5000 -0.0000 -0.0000 -0.0000
Z for k1 = 50000.00 is
1.0e+09 * -0.0000 -0.0000 0.8333 2.5000 -0.0000 -0.0000 0.2778 0.8333 0.8333 0.2778 -0.0000 -0.0000 2.5000 0.8333 -0.0000 -0.0000
Z for k1 = 100000.00 is
1.0e+09 * -0.0000 -0.0000 0.8333 2.5000 -0.0000 -0.0000 0.2778 0.8333 0.8333 0.2778 -0.0000 -0.0000 2.5000 0.8333 -0.0000 -0.0000
legend()
xlabel('k_a_1_2')
ylabel('k_a_2_1')
grid on

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Contour Plots 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by