# draw a 3d graph with mat function

3 views (last 30 days)
Nhan Tran on 20 Oct 2021
Commented: Nhan Tran on 20 Oct 2021
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)
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 CommentsShowHide 1 older comment
Nhan Tran on 20 Oct 2021
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 on 20 Oct 2021
Edited: Cris LaPierre on 20 Oct 2021
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)
Nhan Tran on 20 Oct 2021
thank you a lot

Walter Roberson on 20 Oct 2021
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)
Nhan Tran on 20 Oct 2021
thank you a lot