Failing in creating a surface from three array

1 回表示 (過去 30 日間)
SYML2nd
SYML2nd 2022 年 9 月 2 日
編集済み: Torsten 2022 年 9 月 2 日
I am trying to create a surface from this dataset
x=[0.5 ; 0.624; 0.75;0.874; 0.9;1;1.124]
y=[0; 1.1;1; 0.9;0.6;0]
z=[0.726582; 0.629144; 0.714131; 0.499709; 0.668832; 0.664573]
I am using this code found in here
xi = unique(x) ; yi = unique(y) ;
[X,Y] = meshgrid(xi,yi) ;
Z = reshape(z,size(X)) ;
figure
surf(X,Y,Z)
but I get this error
"Error using reshape
Number of elements must not change. Use [] as one of the size inputs to automatically calculate the appropriate size for that
dimension.
Error in untitled2 (line 8)
Z = reshape(z,size(X)) ;"
I cannot understand what I am doing wrong.

採用された回答

Star Strider
Star Strider 2022 年 9 月 2 日
編集済み: Star Strider 2022 年 9 月 2 日
Use one of the interpolation functions (here griddata) to create the ‘Z’ matrix —
x=[0.5 ; 0.624; 0.75;0.874; 0.9;1.124];
y=[0; 1.1;1; 0.9;0.6;0];
z=[0.726582; 0.629144; 0.714131; 0.499709; 0.668832; 0.664573];
N = 10;
xv = linspace(min(x), max(x), N);
yv = linspace(min(y), max(y), N);
[X,Y] = ndgrid(xv, yv);
Z = griddata(x, y, z, X, Y);
figure
surfc(X, Y, Z)
grid on
xlabel('X')
ylabel('Y')
zlabel('Z')
view(70,30)
.

その他の回答 (1 件)

Torsten
Torsten 2022 年 9 月 2 日
To create a surface, you need a z value for each combination of (x/y) values. Thus instead of a vector z with 6 elements you need a matrix of size (7x6).
  2 件のコメント
SYML2nd
SYML2nd 2022 年 9 月 2 日
Can you show me how to obtain a matrix (7x6)? Do you mean (6x7) by doing Z=[Z,Z,Z,Z,Z,Z,Z] (I tried but it doesnt work)
Torsten
Torsten 2022 年 9 月 2 日
編集済み: Torsten 2022 年 9 月 2 日
x=[0.5 ; 0.624; 0.75;0.874; 0.9;1;1.124];
y=[0; 1.1;1; 0.9;0.6;0];
z=[0.726582; 0.629144; 0.714131; 0.499709; 0.668832; 0.664573];
[X,Y] = meshgrid(x,y);
Z = repmat(z,1,7);
surf(X,Y,Z)
But the surface plot only makes sense if you really have a function f with z(i,j) = f(x(i),y(j))

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

カテゴリ

Help Center および File ExchangeSurface and Mesh Plots についてさらに検索

タグ

製品

Community Treasure Hunt

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

Start Hunting!

Translated by