How can I graph a circle using a nested function in matlab?

2 ビュー (過去 30 日間)
Chelsea
Chelsea 2014 年 4 月 25 日
回答済み: Image Analyst 2014 年 4 月 25 日
I am trying to plot a circle using a function function, but cannot seem to make it work. This is the code I have so far.
function cirpnts(P)
A=[-2*P(1,1) -2*P(1,2) 1; -2*P(2,1) -2*P(2,2) 1; -2*P(3,1) -
2*P(3,2) 1];
B=[-(P(1,1)^2+P(1,2)^2); -(P(2,1)^2+P(2,2)^2); -
(P(3,1)^2+P(3,2)^2)];
C = A\B;
a=C(1);
b=C(2);
r=sqrt(C(1)^2+C(2)^2-C(3));
circleplot(a,b,r)
axis equal
xlabel('x'), ylabel('y')
hold on
plot(P(:,1),P(:,2),'*','markersize',10)
hold off
function circleplot(x,y,R)
th=linspace(0,2*pi,100);
xp=x+R*sin(th);
yp=y+R*cos(th);
plot(xp,yp)
Can someone please help make this work?
  2 件のコメント
Andrew Newell
Andrew Newell 2014 年 4 月 25 日
What exactly is going wrong? The function circleplot works fine if I use it separately, but I'm not sure what cirpnts is supposed to do.
Chelsea
Chelsea 2014 年 4 月 25 日
It is supposed to put little stars on the outside of the circle. Basically, when I try to use it as a function-function it doesn't work. I need to have them in the same script. Am I saving it wrong?

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

回答 (2 件)

the cyclist
the cyclist 2014 年 4 月 25 日
編集済み: the cyclist 2014 年 4 月 25 日
You have to do one of two things with the function circleplot:
  1. Have it within the same *.m file as cirpnts
  2. Have it in a different file but that file must be named circleplot.m
If the subfunction is not with the same *.m file, MATLAB is going to look for the filename.

Image Analyst
Image Analyst 2014 年 4 月 25 日
I don't know what a "function-function" is. You can have both of those functions in the same m-file if you call it cirpnts.m. But when you call cirpnts(), you must pass in P. What is a typical value you are wanting to use for P?

カテゴリ

Help Center および File Exchange2-D and 3-D Plots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by