Plot x^2+y^2=4

121 ビュー (過去 30 日間)
Mohamed Lawindy
Mohamed Lawindy 2020 年 2 月 25 日
コメント済み: Steven Lord 2022 年 6 月 24 日
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
Sky Sartorius 2020 年 2 月 25 日
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.
  1 件のコメント
Mohamed Lawindy
Mohamed Lawindy 2020 年 2 月 25 日
Thank you very much.

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

その他の回答 (3 件)

James Tursa
James Tursa 2020 年 2 月 25 日
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);
  1 件のコメント
Mohamed Lawindy
Mohamed Lawindy 2020 年 2 月 25 日
Thank you too.

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


hamza
hamza 2022 年 6 月 24 日
編集済み: Image Analyst 2022 年 6 月 24 日
Plot the contour plots of the circles x^2+y^2 of radius 1,2, 1.41,1.73.
  1 件のコメント
Image Analyst
Image Analyst 2022 年 6 月 24 日
radii = [1, 2, 1.41, 1.73];
viscircles([zeros(4,1), zeros(4,1)], radii);
axis equal
grid on;

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


Steven Lord
Steven Lord 2022 年 6 月 24 日
Another way to do this is to use the fcontour function.
f = @(x, y) x.^2+y.^2;
fcontour(f, 'LevelList', 4)
axis equal
If you want to see multiple contours, specify a non-scalar LevelList.
figure
fcontour(f, 'LevelList', 1:4:25)
axis equal
  2 件のコメント
Image Analyst
Image Analyst 2022 年 6 月 24 日
And yet another way
viscircles([0,0], 2)
ans =
Group with properties: Children: [2×1 Line] Visible: on HitTest: on Show all properties
Steven Lord
Steven Lord 2022 年 6 月 24 日
Note that viscircles is part of Image Processing Toolbox which means that not all users would have access to it.

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

カテゴリ

Help Center および File ExchangeContour Plots についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by