Plot z = x^2 + y^2 over the interval -4 <= x <= +4, -4 <= y <= +4.

57 views (last 30 days)
I'm not sure how to plot above equation. Can anyone help me?

Accepted Answer

KSSV
KSSV on 14 Sep 2018
N = 100 ;
x = linspace(-4,4,N) ;
y = linspace(-4,4,N) ;
[X,Y] = meshgrid(x,y) ;
Z = X.^2+Y.^2 ;
surf(X,Y,Z)
  4 Comments
Dane Kim
Dane Kim on 14 Sep 2018
oh okay, so it is arbitrary. I can have 100 - 10000 or more.
Image Analyst
Image Analyst on 14 Sep 2018
Correct, but don't do more than you can see any difference for. Yours is such a smooth function it doesn't make sense to do many more points. If you do, the edge lines will ruin the colors.
You can turn off the black edge lines like this:
surf(X,Y,Z, 'EdgeColor', 'none')

Sign in to comment.

More Answers (1)

Stephan
Stephan on 14 Sep 2018
Edited: Stephan on 14 Sep 2018
Hi,
use:
syms x y
f(x,y) = x^2 + y^2;
fsurf(f, [-4 4 -4 4])
Note that this will work if you have access to th Symbolic Math Toolbox. If you dont have it, the answer from KSSV will always work.
Best regards
Stephan

Community Treasure Hunt

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

Start Hunting!