I am trying to plot a contour graph with with x,y and z points

4 ビュー (過去 30 日間)
RAPHAEL OKECHUKWU NWALOZIE
RAPHAEL OKECHUKWU NWALOZIE 2021 年 4 月 25 日
コメント済み: DGM 2021 年 4 月 26 日
Hi I am a newbie in matlab. I tried plotting x,y and z values (which are column vectors) with contourf function but the I get an error message that "Z must be at least a 2x2 matrix". How do I make z a 2x2 matrice?
  2 件のコメント
DGM
DGM 2021 年 4 月 25 日
編集済み: DGM 2021 年 4 月 25 日
If Z is a function of both X and Y, then one would expect it to be a 2D array. The fact that it's a vector leaves me to question how it was calculated and what it means conceptually. Without knowing that, there's no way to say how to fix it.
Consider the example:
x = linspace(-1,1,10);
y = linspace(-1,1,10);
z1 = x.^2 + y.^2; % this is a vector
z2 = x.^2 + y'.^2; % this is a 2D array
In this case, z2 is a 2D array describing a paraboloid over a rectangular domain. On the other hand, z1 describes the same paraboloid along the diagonal of the same domain (the orange line).
The second case (z2) is what you'd want to use with contourf().
contourf(x,y',z2)
Whereas z1 doesn't contain enough information for a contour plot. You'd normally just plot it.
plot(x,z1)
Adam Danz
Adam Danz 2021 年 4 月 25 日
> How do I make z a 2x2 matrice
Impossible to answer without more info.
z(i,j) defines the z-value at x(i) and y(j).

サインインしてコメントする。

回答 (1 件)

Clayton Gotberg
Clayton Gotberg 2021 年 4 月 26 日
編集済み: Clayton Gotberg 2021 年 4 月 26 日
It sounds like the problem is that you haven't made these functions into a grid.
You started with x and y, then found z as a function of x and y. However, I'd guess that when you did that, you just said z = f(x,y).
% What I think you have:
x = [1 2 3 4];
y = [4 3 2 1];
z = x+y; % This equals [5 5 5 5]
% This matches up each element in x with the element in the same location in y
%what I think you want:
z = x+y; % Except now it equals [5 6 7 8; 4 5 6 7; 3 4 5 6; 2 3 4 5].
% Now, every element in x is matched with every element in y.
% How to get what you want:
[X,Y] = meshgrid(x,y); % Now X = [1 2 3 4; 1 2 3 4; 1 2 3 4; 1 2 3 4]
% and Y = [4 4 4 4; 3 3 3 3; 2 2 2 2; 1 1 1 1]
Z = X+Y;
  3 件のコメント
Clayton Gotberg
Clayton Gotberg 2021 年 4 月 26 日
I glanced over the documentation to see if that would work but I missed the section that says it does! I'm also in the habit of using meshgrid but I definitely see your point about a use case for leaving the x and y vectors alone.
DGM
DGM 2021 年 4 月 26 日
Yeah, prior to R2016b, trying to do things with orthogonal vectors often devolves into a complicated mess requiring bsxfun(). Since I'm usually running R2015b still, meshgrid is a big convenience, even if it can be slow.

サインインしてコメントする。

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by