13

In Matlab you can draw a circle by just specifying the center and the radius like this:

R = 10;
Center = [5,8];
circle(Center,R,1000,'b-');
hold on
plot(Center(1),Center(2),'g.')

The same code for MatLab won't work for GNU Octave. What octave code would draw a circle given a center x,y coordinates and a radius?

0

8 Answers 8

18
t = linspace(0,2*pi,100)'; 
circsx = r.*cos(t) + x; 
circsy = r.*sin(t) + y; 
plot(circsx,circsy); 
2
10

Using the octave extra package octave-geometry you can use the function drawCircle as

drawCircle(x_position, y_position, radius)
  1. Download the package from https://octave.sourceforge.io/geometry/
  2. In Octave:
    • pkg install path_to_file.tar.gz
    • pkg load geometry
4

If you would like a reusable function, here's one possibility:

function [h, hc] = circles(x,y,r,cmrk)
% CIRCLES   plot 2-D circles, given a set of center coordinates and radii.
%
% Description:
%
%   Plot 2-D circles, given a set of center coordinates and radii. Values
%   can be vectors or matrices, as long as dimensions are consistent. If
%   a marker type (e.g. '+') is also given, circle centers will be marked
%   with it. The function returns a vector of handles for each circle and
%   a handle for all the center markers, if plotted.

assert(size(x)==size(y), 'Mismatching sizes')
assert(size(y)==size(r), 'Mismatching sizes')

if (nargin==4)
    hc = scatter(x,y,[],[],cmrk);
end
axis([min(x-r) max(x+r) min(y-r) max(y+r)], 'equal');

a = linspace(0, 2*pi, 12);
dx = sin(a); dy = cos(a);
hold on
for i=1:numel(x);
    h(i) = line(x(i)+dx*r(i), y(i)+dy*r(i));
end
hold off

Here's an example of usage:

x = 0:.1:2*pi; y = sin(x); r = rand(size(x))*.3;
circles(x, y, r, '+')
1
  • why not contribute this function to Octave? It's well written and may be useful for future users. I would just rename it to be compatible with Matlab.
    – juliohm
    Dec 3, 2013 at 9:21
3

How to draw a circle in gnu octave version 3.8:

enter image description here

Code:

octave> x = -1:0.01:1;
octave> y = (1 - x .^ 2) .^ 0.5;
octave> plot(x,y, "linewidth", 4, x,-y, "linewidth", 4);

Verbalization:

Create a list between -1 and 1 in increments of .01 to represent the x axis. The y axis is the diameter of the circle minus the value at each index of x squared, all raised to the 0.5.

Plot x and y (blue), which gives the upper half of the circle, then plot x to -y, which inverts the top (green), creating the bottom half of the circle.

Alternatively, use linspace:

enter image description here

Code:

octave> r = 1;
octave> t = linspace(0,2.*pi,1);
octave> circsx = r.*cos(t) + x;
octave> circsy = r.*sin(t) + y;
octave> plot(circsx,circsy, "linewidth", 4, circsx, -circsy, "linewidth", 4); 

Verbalization:

Draws a circle.

1

Plot a circle in GNU Octave:

x = -3:0.01:3;
y = (4 - x .^ 2) .^ 0.5;
figure; plot(x,y); hold on; plot(x,-y);

That should draw a circle having an equation x^2 + y^2 = 4;

1

There is a built in function in the package "geometry" that can be used to draw different geometric shapes. Here is the code for a circle:

pkg load geometry
drawCircle (x0, y0, r)
0

Ocatve have several way to draw circle,that is one of easy way.

pkg load geometry
x=0;
y=0; #center point ordered pair
r1=10; #radius of circule
h1=drawCircle(x,y,r1);

if you want drow more circle over,

hold on;
r2=8;
h2=drawCircle(x,y,r2);

enter image description here

another way is

angle=linspace(0,2*pi,360);

r=5; #radius

x=2;

y=5; #x and y is hear center point 

horizantalValue=r*cos(angle)+x;

verticalValue=r*sin(angle)+y;

plot(verticalValue,horizantalValue);
0
radius = 5;
x = cosd([0:360]) * radius;
y = sind([0:360]) * radius;
plot(x,y)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.