A function to draw a circle with only radius as parameter?

Hello I need help to create a function that will help me draw a circle with a given radius.
So the function can only have the radius as parameter.
For example if I type draw_circle(5) I want to have a plot of a circle with radius 5. Since the equation is y^2+x^2=r^2 ---> y=+/-sqrt(r^2-x^2) it feels like I need two parameters in the function one for x and one for r.
So basically can someone help me construct a function draw_circle(r) that plots a circle with radius 5.
Thanks!

 採用された回答

Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 12 日
編集済み: Azzi Abdelmalek 2012 年 11 月 12 日

0 投票

function cercle(r)
x=-r:.01:r
y=sqrt(r^2-x.^2)
hold on;
plot(x,y);
plot(x,-y)
call the function
cercle(5)

3 件のコメント

Peter
Peter 2012 年 11 月 12 日
I can't seem to get it to work
function circle(r)
x=-r:.01:r
y=sqrt(r^2-x.^2)
hold on;
plot(x,y);
plot(x,-y)
circle(5)
end
it returns: "not enough input arguments in line2" should I call the function without end?
Azzi Abdelmalek
Azzi Abdelmalek 2012 年 11 月 12 日
編集済み: Azzi Abdelmalek 2012 年 11 月 12 日
What circle(5) is doing inside a function?
save the function I writted above, it does'nt contain circle(5) . then in matlab command type
circle(5)
Peter
Peter 2012 年 11 月 13 日
Sorry I have been programming in python too much. Thank you it worked.

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

その他の回答 (2 件)

Matt Fig
Matt Fig 2012 年 11 月 12 日
編集済み: Matt Fig 2012 年 11 月 12 日

1 投票

function [] = plot_circle(R)
% Plots a circle of radius R
[X,Y] = cylinder(R,1000);
plot(X(1,:),Y(1,:))
axis equal
Save this as plot_circle.m then call from the command line:
plot_circle(4) % Radius 4
If you just want a function handle that can do it, try this one:
f = @(R) plot(R*cos(0:.001:2*pi),R*sin(0:.001:2*pi));
f(3)
axis equal

カテゴリ

ヘルプ センター および File ExchangeIntroduction to Installation and Licensing についてさらに検索

製品

Community Treasure Hunt

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

Start Hunting!

Translated by