Drawing Circle using simple equation of circle

898 views
Skip to first unread message

Priyanka Kapoor

unread,
May 7, 2012, 4:58:56 AM5/7/12
to sage-s...@googlegroups.com
I plotted circle using parametric equation, using circle() function.
But i want to plot x^2+y^2=4
I tried this:
var('x,y')
b=[x^2+y^2==4]
c=plot(b,(x,-2,2),(y,-2,2),color='blue')
c.save('ram.png')

I am getting the following error
TypeError: float() argument must be a string or a number

Can anyone help me. I am new to sage that's why unable to solve errors



--
Priyanka Kapoor
http://kapoorpriyanka.in
Linux User Group, Ludhiana

Hakan Granath

unread,
May 7, 2012, 7:01:19 AM5/7/12
to sage-s...@googlegroups.com
You might want to use implicit_plot instead:

var('x,y')
b = x^2+y^2==4
c = implicit_plot(b,(x,-2,2),(y,-2,2),color='blue')
c.save('ram.png')

/Håkan

Priyanka Kapoor

unread,
May 7, 2012, 7:24:26 AM5/7/12
to sage-s...@googlegroups.com
It gives the following error
TypeError: 'tuple' object is not callable

Hakan Granath

unread,
May 7, 2012, 7:35:49 AM5/7/12
to sage-s...@googlegroups.com
On Mon, May 7, 2012 at 1:24 PM, Priyanka Kapoor
<anjalicool...@gmail.com> wrote:
>> You might want to use implicit_plot instead:
>>
>> var('x,y')
>> b = x^2+y^2==4
>> c = implicit_plot(b,(x,-2,2),(y,-2,2),color='blue')
>> c.save('ram.png')
>>
> It gives the following error
> TypeError: 'tuple' object is not callable

Please note that I wrote

b = x^2+y^2==4

Priyanka Kapoor

unread,
May 7, 2012, 11:27:56 AM5/7/12
to sage-s...@googlegroups.com
> Please note that I wrote
>
> b = x^2+y^2==4

I got a plot.thank you. Sir , can't we draw it using plot() function.

Jason Grout

unread,
May 7, 2012, 11:55:22 AM5/7/12
to sage-s...@googlegroups.com
On 5/7/12 10:27 AM, Priyanka Kapoor wrote:
>> Please note that I wrote
>>
>> b = x^2+y^2==4
>
> I got a plot.thank you. Sir , can't we draw it using plot() function.

No, because it's not a y=... function.

If all you want is a circle, you can also use the circle function:

circle((0,0), 4)

Thanks,

Jason

Slumberland

unread,
Jun 24, 2012, 10:09:59 PM6/24/12
to sage-s...@googlegroups.com
Okay, that's maybe not the answer you're looking for.

What he means is that you can't plot it explicitly without solving for y.
"Implicit" is another way of saying 
"not in the form y = "
(or z= , x =   .... etc)

But it can be instructive to figure out how to plot the functions which define a circle.  Is that what you want?

Sage will solve the equation for you.  You can type

sage: x,y = var('x,y')
sage: b = x^2 + y^2 == 4
sage: c = solve(b,y)
sage: c
[y == -sqrt(-x^2 + 4), y == sqrt(-x^2 + 4)]

The command "solve(b,y)" solves your equation for the variable y.
Plotting this way is awkward but you can do it.  In this case, we need each of the functions
    -sqrt(-x^2+4),
    sqrt(-x^2+4)

Both of these are stored in c.  
To get them, you can tell python what parts you want:
c[0].rhs() gives you the right-hand-side{ ;-) of the first equality stored in c
c[1].rhs() grabs the second.
So, you could do this:

sage: bottom = plot( c[0].rhs(), xmin=-3, xmax=3, aspect_ratio=1)
sage: top = plot(c[0].rhs(), xmin=-3, xmax=3)
sage: show(bottom+top)

and the graph will show, but Python complains that it can't evaluate the function at some points.

 implicit_plot() is easier to use, that's all.

Priyanka Kapoor

unread,
Jun 25, 2012, 12:12:35 AM6/25/12
to sage-s...@googlegroups.com
Thanks for explaining the things . :-) It executed the code and it
gives half circle. Anyways if it gives warnings then we should not do
this way.
Thanks once again.



--
Priyanka Kapoor
priyankacool10.wordpress.com
Linux User Group, Ludhiana

Slumberland

unread,
Jun 25, 2012, 5:41:23 AM6/25/12
to sage-s...@googlegroups.com
The warnings aren't really errors.  The tangent to a circle is vertical in two places, and that's the same thing as saying "it has to be broken up into two pieces."  Sage will tell you those points can't be evaluated.  Many other programs (such as Mac OSX's free Grapher) will suppress these warnings because there is nothing wrong with the function.

To fix the problem, you can add another command which tells SAGE only to evaluate your circle from -2 to 2.  You can still set the xmin and xmax on your graph wherever you want.

The problem with what I wrote is that SAGE is actually trying to plot points at, say, x=2.4.  But, as you already know, there is no circle there!

Sorry if I made it more confusing.  Didn't want to add a lot of new commands and make it hard to see how they fit together.

In any case, there is nothing wrong with plotting functions one piece at a time, and really there's no way to avoid it.  You'll be stuck dealing with these issues every time you work with math.  Personally, I like to know what's going on, so that I can handle them myself, just in case the program doesn't want to play along.

For example, suppose you had a program that wasn't very accurate, and even though you SAID "can you please evaluate this from -2 to 2?" because of the way it divided up the interval [-2, 2], the last number it chose for x was 2.000000000001 instead of 2.  The square root is negative, here's no circle there, and the program gives you an error.

This would not be your fault, and it would be a mistake to assume you "should not do it this way."

Right?

I mention it because Python is exactly such a language.  It tends to give you too many decimal places; some of which are no longer accurate.  The way to fix the problem, would be to evaluate the circle from, say
[  -1.9999    to   1.9999]

hahahahahaha, see?  Then the graph will be fine, and there will be no errors.  Even if it misses by a little bit, it won't pass 2, and everything is fine.  Sometimes this is the difference between a working graph and an error!

Hope that helps! 
Reply all
Reply to author
Forward
0 new messages