Results 1 to 2 of 2

Math Help - Matlab - Plotting Circles

  1. #1
    Bar0n janvdl's Avatar
    Joined
    Apr 2007
    From
    Meh
    Posts
    1,630
    Thanks
    6

    Matlab - Plotting Circles

    Hey guys

    I have the following problem:

    Write a function m-file, PlotCircles.m, that can accept as input a vector, r,
    having positive entries, and plots circles of radius r(i) on the same axis. If no input value for r is specified, the default is to plot a single circle with radius r = 1. Test your function by using r = 1:5.

    Hints: Matlab commands that might be useful for this problem include max, min and any.
    My solution so far:

    Code:
    function PlotCircles(r)
    
    theta = linspace(0,2*pi,1000);
    
    if nargin == 0
        r = 1;
    end
    
    for i = min(r):max(r)
       x = i*cos(theta);
       y = i*sin(theta);
       plot(x,y)
       axis square
       axis([-(r+1) (r+1) -(r+1) (r+1)])
    end
    However when I input a vector as follows:
    Code:
    EDU>> r = 1:5
    
    r =
    
         1     2     3     4     5
    
    EDU>> PlotCircles(r)
    ??? Error using ==> axis>LocSetLimits at 236
    Vector must have 4, 6, or 8 elements.
    
    Error in ==> axis at 96
                    LocSetLimits(ax(j),cur_arg);
    
    Error in ==> PlotCircles at 14
       axis([-(r+1) (r+1) -(r+1) (r+1)])
    And then it seems to draw only the first circle. Any help is appreciated.
    Follow Math Help Forum on Facebook and Google+

  2. #2
    Grand Panjandrum
    Joined
    Nov 2005
    From
    someplace
    Posts
    14,972
    Thanks
    4
    Quote Originally Posted by janvdl View Post
    Hey guys

    I have the following problem:



    My solution so far:



    However when I input a vector as follows:


    And then it seems to draw only the first circle. Any help is appreciated.
    The simple answer is hold('on')/hold('off'), the following seems to work:

    Code:
    function rv=PlotCircles(r)
      rv=[]
      theta = linspace(0,2*pi,100);
     
      if nargin == 0
        r = 1;
      end
     
    
      for i = 1:length(r)
        x = r(i)*cos(theta);
        y = r(i)*sin(theta);
        plot(x,y)
        axis square
        r0=max(r);
        axis([-(r0+1) (r0+1) -(r0+1) (r0+1)])
        hold('on');
      end
      hold('off');
    CB
    Follow Math Help Forum on Facebook and Google+

Similar Math Help Forum Discussions

  1. Plotting multiple circles in Octave from arrays
    Posted in the Math Software Forum
    Replies: 1
    Last Post: October 27th 2010, 09:43 PM
  2. matlab plotting graph need help
    Posted in the Math Software Forum
    Replies: 1
    Last Post: August 7th 2010, 12:27 AM
  3. plotting a graph in matlab
    Posted in the Math Software Forum
    Replies: 3
    Last Post: March 21st 2009, 03:11 PM
  4. MATLAB: Plotting MATRIX?
    Posted in the Math Software Forum
    Replies: 3
    Last Post: January 25th 2009, 01:21 PM
  5. MATLAB - plotting a quadratic
    Posted in the Math Software Forum
    Replies: 2
    Last Post: June 15th 2008, 06:25 PM

Search Tags


/mathhelpforum @mathhelpforum