Error using contour (Z must be at least a 2x2 matrix. What changes should I make to my code to make Z a 2x2 matrix?

99 vues (au cours des 30 derniers jours)
alpha=linspace(1,8);
beta=linspace(0,8);
a=(1/5).*(sqrt((2.*pi.*beta)./((alpha.^2)-1)));
u= sqrt((alpha.^2)+(beta.^2));
t= sqrt(1+(beta).^2);
G=real(a.*reallog((alpha+u)./(1+t)));
[X,Y] = meshgrid(alpha,beta);
contour(X,Y,G)
The final plot of the above code should resemble something like this:
I have double chekced, the formula is correct and the value of G at a given alpha and beta match with the experimental results. However, I face a problem with the dimensions of Z as it should be a 2x2 matrix.
Thank you in advance.

Réponse acceptée

Cris LaPierre
Cris LaPierre le 30 Jan 2020
Modifié(e) : Cris LaPierre le 31 Jan 2020
The solution is to ensure X, Y and G are all the same size. The meshgrid operation makes X and Y 100 x 100. The problem, then, is when you try to use contour, there is not a value in G for each X,Y pair.
The simplest way to do this is call the meshgrid function immediately after creating alpha and beta. That way, all the subsequent calculations are performed on the 100x100 variables, resulting in G being 100x100 as well.
alpha=linspace(1,8);
beta=linspace(0,8);
[alpha,beta] = meshgrid(alpha,beta);
a=(1/5).*(sqrt((2.*pi.*beta)./((alpha.^2)-1)));
u= sqrt((alpha.^2)+(beta.^2));
t= sqrt(1+(beta).^2);
G=real(a.*reallog((alpha+u)./(1+t)));
contour(alpha,beta,G)

Plus de réponses (0)

Catégories

En savoir plus sur Contour Plots dans Help Center et File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!

Translated by