필터 지우기
필터 지우기

Please How I can get the figure like in the Picture below

조회 수: 1 (최근 30일)
Abdelkader Hd
Abdelkader Hd 2022년 8월 12일
댓글: Rik 2022년 8월 12일
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
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.
Abdelkader Hd
Abdelkader Hd 2022년 8월 12일
@Rik thank you for your response, Please if you can write how I can do this, because I beginner use of matlab

댓글을 달려면 로그인하십시오.

채택된 답변

Rik
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 CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by