help-octave
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: plotting even function


From: Geraint Paul Bevan
Subject: Re: plotting even function
Date: Sun, 20 Mar 2005 23:11:46 +0000
User-agent: Mozilla Thunderbird 0.5 (X11/20040306)

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

John B. Thoo wrote:
> On Mar 20, 2005, at 1:25 PM, John B. Thoo wrote:
>
>> So, if you would allow me at least one more daft question, how do I
>> tell Octave to use the real root so that the plot of  y = x^(1/3)  is
>> symmetrical about the origin?
>
>
> OK, this is how I've done it.
>
>> x1 = -1:0.1:0; x2 = 0:0.1:1;
>> plot (x1, -(-x1).^(1/3), x2, x2.^(1/3))  % odd symmetry
>> plot (x1, (-x1).^(2/3), x2, x2.^(2/3))   % even symmetry
>
> But is there a more elegant way?
>
> TIA again.
> ---John.
>

There is a C++ function "cbrt" which calculates the real cube root of
its (real) argument. The attached cbrt.cc uses this function to call it
and return the result to Octave.

Compile it with:
$ mkoctfile cbrt.cc

and then use it:
octave> x = [-10:0.1:10];
octave> plot (x, cbrt(x))

- --
Geraint Bevan
http://homepage.ntlworld.com/geraint.bevan

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)

iEYEARECAAYFAkI+AzEACgkQcXV3N50QmNMFHgCfQO5sMyDYS6pBa5scQzVR6TWm
G1QAoI2S67PRh51vd1+AHLfvow8a5njj
=M7IP
-----END PGP SIGNATURE-----
//     cbrt calculates the real cube root of its arguments
//     Copyright (C) 2005  Geraint Paul Bevan

//     This program is free software; you can redistribute it and/or modify
//     it under the terms of the GNU General Public License as published by
//     the Free Software Foundation; either version 2 of the License, or
//     (at your option) any later version.

//     This program is distributed in the hope that it will be useful,
//     but WITHOUT ANY WARRANTY; without even the implied warranty of
//     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//     GNU General Public License for more details.

//     You should have received a copy of the GNU General Public License
//     along with this program; if not, write to the Free Software
//     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

#include <octave/oct.h>

DEFUN_DLD(cbrt, args, ,
  "-*- texinfo -*-\n\
@deftypefn {Loadable Function} address@hidden =} cbrt (@var{x})\n\
\n\
Finds the (real) cube root of @var{x}.\
If @var{x} is a vector or matrix, the cube root of each\n\
element is calculated individually.\n\
@end deftypefn")
{
  octave_value_list retval;

  for (int i = 0; i < args.length(); i++) {
    int nrow = args(i).rows();
    int ncol = args(i).columns();

    if (args(i).is_complex_type()) {
      error ("cbrt cannot handle complex arguments");
    }

    Matrix tmp(nrow,ncol);
    for (int row = 0; row < nrow; row++) {
      for (int col = 0; col < ncol; col++) {
        tmp(row,col) = cbrt(args(i).matrix_value()(row,col));
      }
    }
    retval(i) = tmp;
  }

  return retval;
}
  

reply via email to

[Prev in Thread] Current Thread [Next in Thread]