Please How I can get the figure like in the Picture below
1 Ansicht (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Rik
am 12 Aug. 2022
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.
Akzeptierte Antwort
Rik
am 12 Aug. 2022
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
2 Kommentare
Weitere Antworten (0)
Siehe auch
Kategorien
Mehr zu Matrix Indexing finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!