plot surface using different x,y vector increment and got different graph

3 ビュー (過去 30 日間)
MacGyver Pan
MacGyver Pan 2020 年 11 月 26 日
コメント済み: MacGyver Pan 2020 年 11 月 27 日
Hi, I using follow code to generate a surface, got fig 1
=========================================================
clear;
x=0:0.01:1;
y=0:0.01:1;
[x1,y1]=meshgrid(x,y);
z1=-1.448-0.477*x1-0.4757*y1+0.1473*x1.^2-0.1952*x1*y1+0.1334*y1.^2;
surf(x1,y1,z1,'FaceColor','red','LineStyle','none');
hold on;
=========================================================
fig 1
and I change x and y vector step from 0.01 to 0.1, got fig 2
=========================================================
clear;
x=0:0.1:1;
y=0:0.1:1;
[x1,y1]=meshgrid(x,y);
z1=-1.448-0.477*x1-0.4757*y1+0.1473*x1.^2-0.1952*x1*y1+0.1334*y1.^2;
surf(x1,y1,z1,'FaceColor','red','LineStyle','none');
hold on;
=========================================================
fig 2
My question is why Z-index is not same range. fig1 Z-index range is [-9,-8] and fig 2 Z-index range is [-3,-2]
which plot is correct ? and what causes this problem? How do I modify the code to produce the same plot?
Thanks....

採用された回答

Théophane Dimier
Théophane Dimier 2020 年 11 月 26 日
Hi
In your formula, there is an operation that is not element wise:
z1=-1.448-0.477*x1-0.4757*y1+0.1473*x1.^2-0.1952*x1*y1+0.1334*y1.^2;
If you correct it to
z1=-1.448-0.477*x1-0.4757*y1+0.1473*x1.^2-0.1952*x1.*y1+0.1334*y1.^2;
The two codes give the same results
  1 件のコメント
MacGyver Pan
MacGyver Pan 2020 年 11 月 27 日
it's work.
I read many similar examples, not to point this operation problem.
Thanks very much.

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

その他の回答 (1 件)

Stephan
Stephan 2020 年 11 月 26 日
編集済み: Stephan 2020 年 11 月 26 日
You did not multiply elementwise, also you can shorten your code a little bit:
fun = @(x,y) -1.448-0.477*x-0.4757*y+0.1473*x.^2-0.1952*x.*y+0.1334*y.^2;
xl=0:0.1:1;
yl=0:0.1:1;
[x1,y1]=meshgrid(xl,yl);
z1=fun(x1,y1);
figure
surf(xl,yl,z1,'FaceColor','red','LineStyle','none');
xs=0:0.01:1;
ys=0:0.01:1;
[x2,y2]=meshgrid(xs,ys);
z2=fun(x2,y2);
figure
surf(xs,ys,z2,'FaceColor','red','LineStyle','none');

カテゴリ

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