# Plot x^2+y^2=4

182 views (last 30 days)
Mohamed Lawindy on 25 Feb 2020
Commented: Mohamed Lawindy on 25 Feb 2020
Hello, I have a little starter question about matlab. How do I plot a circle given by x^2+y^2=4?
Thank you.

Sky Sartorius on 25 Feb 2020
There are a few ways to go about this. One that is somewhat agnostic to what the equation is trying to represent (in this case, a circle) involves calculating the equation for the whole space, then plotting only an isoline of the target value.
[X,Y] = meshgrid(-3:.1:3,-3:.1:3); % Generate domain.
Z = X.^2 + Y.^2; % Find function value everywhere in the domain.
contour(X,Y,Z,[4 4]) % Plot the isoline where the function value is 4.
If you know more about your function and can turn it around into a function of only one variable (e.g., sine and cosine of t), that is preferable in most cases.
Mohamed Lawindy on 25 Feb 2020
Thank you very much.

James Tursa on 25 Feb 2020
E.g., since you know it is a circle with radius 2 centered at the origin;
ang = 0:0.01:2*pi;
x = 2*cos(ang);
y = 2*sin(ang);
plot(x,y);
Mohamed Lawindy on 25 Feb 2020
Thank you too.