Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
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 Commenti
Rik
Rik il 12 Ago 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.
Abdelkader Hd
Abdelkader Hd il 12 Ago 2022
@Rik thank you for your response, Please if you can write how I can do this, because I beginner use of matlab

Accedi per commentare.

Risposta accettata

Rik
Rik il 12 Ago 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

Più risposte (0)

Categorie

Scopri di più su Loops and Conditional Statements in Help Center e File Exchange

Prodotti


Release

R2019a

Community Treasure Hunt

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

Start Hunting!

Translated by