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)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1094855/image.jpeg)
댓글 수: 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!