Please How I can get the figure like in the Picture below
1 次查看(过去 30 天)
显示 更早的评论
hello community I look to get the figure like in the picture below using contourf and Thank you
clc
clear all
x=[]; y=[]; z=[];
for n=1:1001
x1=0.01*(n-1);
x2=0.01*(n-4);
B=[0,x1,4,x2;x1,3,x2,x2;x1,0,5,x2;0,5,x2,x1];
Pd=eig(B);
if max(real(Pd))<0
disp('fail');
disp(n);
end
B1=[B(1,1),B(1,2);B(2,1),B(2,2)];
B2=[B(3,3),B(3,4);B(4,3),B(4,4)];
B3=[B(1,3),B(1,4);B(2,3),B(2,4)];
Sum=det(B1)+det(B2)+2.*det(B3);
Et=sqrt(Sum-sqrt(Sum.^2-4.*det(B)))./sqrt(2);
E=2*max(0,real(2*Et));
x(n)=x1; y(n)=x2; z(n)=E;
n=n+1;
end
[X,Y] = meshgrid(x,y);
contourf(X,Y,Z,100, 'edgecolor','none');
plot(x,y)
2 个评论
Rik
2022-8-12
You should calculate a z for each pair of x and y. I suspect the easiest way to do this is to use the meshgrid before your loop. That way you can also easily pre-allocate your arrays.
采纳的回答
Rik
2022-8-12
You first need to define your variables:
n=(1:1001);
x=0.01*(n-1);
y=0.01*(n-4);
Now we have vectors, but you want the 2D grid they define:
[X,Y] = meshgrid(x,y);
Now we can create a Z array of the correct size to hold the output and loop through all elements of these arrays by using linear indexing.
Z=zeros(size(X));
for n=1:numel(X)
Z(n)=YourCode(X(n),Y(n));
end
contourf(X,Y,Z,100, 'edgecolor','none');
function E=YourCode(x1,x2)
% Don't forget to write comments to explain what this code does. You will
% have forgotten in 6 months, making it impossible to find any bugs.
B=[0,x1,4,x2;x1,3,x2,x2;x1,0,5,x2;0,5,x2,x1];
Pd=eig(B);
if max(real(Pd))<0
disp('fail');
disp(n);
end
B1=[B(1,1),B(1,2);B(2,1),B(2,2)];
B2=[B(3,3),B(3,4);B(4,3),B(4,4)];
B3=[B(1,3),B(1,4);B(2,3),B(2,4)];
Sum=det(B1)+det(B2)+2.*det(B3);
Et=sqrt(Sum-sqrt(Sum.^2-4.*det(B)))./sqrt(2);
E=2*max(0,real(2*Et));
end
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Matrix Indexing 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!