Draw the surface of a two variables function

When I run the code below I get the figure below.
[X,Y] = meshgrid(-10:1:10);
Z = -X.*exp(-X.*Y) + cos(X);
surf(X,Y,Z,'FaceAlpha',0.75)
What wrong scaling or errors could be contribute to MATLAB's failure in the accuratecy rendering the surface of the function defined by the code above? The question arose after plotting the surface in Geogebra (see below). In general, what is the best way to plot a 3d surface which has complicated shape like the one above. I have the same problem with the function f = @(x,y) ln(1+x.^2+y.^2) - (x-1).^2 - y.^2

3 commentaires

fsurf(@(x, y) -x .* exp(-x .*y) + cos(x), [-10, 10], 'FaceAlpha', .75)
"What wrong scaling or errors could be contributing to MATLAB's failure in accurately rendering the surface of the function defined by the code above?"
What makes you think that the plot is inaccurate?
"In general, what is the best way to plot a 3d surface which has complicated shape like the one above. I have the same problem with the function f = @(x,y) ln(1+x.^2+y.^2) - (x-1).^2 - y.^2 "
If you are working with an explicit equation, you can use fsurf, as @madhan ravi has shown above.
Otherwise, surf works good as well.
Teodor
Teodor le 11 Déc 2023
I updated my question.

Connectez-vous pour commenter.

Réponses (3)

[X,Y] = meshgrid(-1:0.1:1);
Z = -X.*exp(-X.*Y) + cos(X);
surf(X,Y,Z,'FaceAlpha',0.75)
f = @(x,y) log(1+x.^2+y.^2) - (x-1).^2 - y.^2;
fsurf(f,'FaceAlpha',0.75)
All the variation occurs near the centre of the surface. You can begin to see that in the contour plot using surfc, however to see the full effect, plot with a higher resolution over a smaller range —
[X,Y] = meshgrid(-10:10);
Z = -X.*exp(-X.*Y) + cos(X);
figure
surfc(X,Y,Z,'FaceAlpha',0.75, 'EdgeColor','interp', 'FaceColor','interp')
colormap(turbo)
[X,Y] = meshgrid(-1:0.01:1);
Z = -X.*exp(-X.*Y) + cos(X);
figure
surfc(X,Y,Z,'FaceAlpha',0.75, 'EdgeColor','interp', 'FaceColor','interp')
colormap(turbo)
.
Let's look at the values your function takes on at the corners and the center of the region you're plotting.
format longg
Z = @(X, Y) -X.*exp(-X.*Y) + cos(X);
center = Z(0, 0)
center =
1
corners = Z([-10 -10 10 10], [-10 10 -10 10])
corners = 1×4
1.0e+00 * -0.839071529076452 2.68811714181614e+44 -2.68811714181614e+44 -0.839071529076452
The two corners where the X and Y values have the same sign give small values, pretty close (relatively speaking) to the value at the center. The two corners where they have different signs differ by 44 orders of magnitude from the other corners! If you drew your graph with 1 unit of distance corresponding to 1 millimeter in the real world, those two corners would be outside the observable universe (1e41 meters, while the diameter of the observable universe is about 8.8e26 meters.)
As others have stated, drawing it over a smaller region would help show more of the variation without those variations being swamped by the huge magnitude of the corner points.

Produits

Version

R2023b

Tags

Question posée :

le 8 Déc 2023

Commenté :

le 11 Déc 2023

Community Treasure Hunt

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

Start Hunting!

Translated by