hi, I'm about plotting a 3D graph from below funcion
(X - 3.14)^2 + (Y - 2.72)^2 + sin(3*X + 1.41) + sin(4*Y - 1.73)
I've tried using but it's not like what I expected. Could someone help
[X, Y] = meshgrid(-10:10);
Z = (X - 3.14).^2 + (Y - 2.72).^2 + sin(3*X + 1.41) + sin(4*Y - 1.73);
surf(X, Y, Z)
Edit 1 (added more context):
hi, I'm making the Optimization for Machine Learning. The post (from the internet) show the plot is very different from what I got in MATLAB (origin image can be found here). I think my code is wrong, and I'm trying to understand how to fix it to get more MATLAB experience
thanks

2 件のコメント

Cris LaPierre
Cris LaPierre 2021 年 10 月 20 日
Without knowing what it is you expected, it's hard to know what is wrong.
Nhan Tran
Nhan Tran 2021 年 10 月 20 日
hi, I'm making the Optimization for Machine Learning. The post (from the internet) show the plot is very different from what I got in MATLAB (origin image can be found here). I think my code is wrong, and I'm trying to understand how to fix it to get more MATLAB experience
thanks

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

 採用された回答

Cris LaPierre
Cris LaPierre 2021 年 10 月 20 日
編集済み: Cris LaPierre 2021 年 10 月 20 日

0 投票

Your code is correct. You just need to increase the resolution of your mesh. Remember that the colon operator steps by 1 by default. Also, the linked figure has an X and Y range of 0-5.
[X, Y] = meshgrid(0:0.1:5);
Z = (X - 3.14).^2 + (Y - 2.72).^2 + sin(3*X + 1.41) + sin(4*Y - 1.73);
surf(X, Y, Z)

1 件のコメント

Nhan Tran
Nhan Tran 2021 年 10 月 20 日
thank you a lot

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

その他の回答 (1 件)

Walter Roberson
Walter Roberson 2021 年 10 月 20 日

0 投票

You were not sampling finely enough, and you were also plotting outside the desired range.
[X, Y] = meshgrid(linspace(0,2*pi));
Z = (X - 3.14).^2 + (Y - 2.72).^2 + sin(3*X + 1.41) + sin(4*Y - 1.73);
surf(X, Y, Z)

1 件のコメント

Nhan Tran
Nhan Tran 2021 年 10 月 20 日
thank you a lot

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

カテゴリ

ヘルプ センター および File Exchange2-D and 3-D Plots についてさらに検索

タグ

質問済み:

2021 年 10 月 20 日

コメント済み:

2021 年 10 月 20 日

Community Treasure Hunt

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

Start Hunting!

Translated by