help-octave
[Top][All Lists]
Advanced

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

Re: plot data from multiple arguments


From: D. Stimits
Subject: Re: plot data from multiple arguments
Date: Mon, 29 Oct 2001 14:19:54 -0700

Everyone's replies got me a lot further along, I was able to figure out
more closely what the problem is. My "non-test" version of function that
fails requires the square of a scalar, the "x" that I am essentially
trying to iterate through from 0.0 to 1.0 in steps of 0.1. I'm used to
"strongly typed" languages, and failed to see that:
x = (0.0:0.1:1.0)';
data = [x, MyFunc(x, 3, 2)];

...does not pass a scalar through multiple passes to the "x" of
MyFunc...instead it is passing a matrix one time. I was making a bad
assumption that MyFunc would be passed one array element at a time from
x, iterating throug it until each element, in order, had been passed,
and accumulate a return value of each pass. So when I expected "x ** 2"
to square a single value of x from a single index, it instead was dying
with:
error: operator *: nonconformant arguments (op1 is 11x1, op2 is 11x1)
error: evaluating binary operator `*' near line 21, column 17
error: evaluating assignment expression near line 21, column 13

Now I fear that to build "data" I won't be able to use a simple one-line
formula unless I modify MyFunc to handle "x" as a matrix...is this
correct? At which point I wonder what else might break. Btw, "MyFunc"
returns a single float between 0 and 1, much like sin(x) would. I'm
interested only in the input value of "x" (also between 0 and 1) and
associated value that MyFunc returns as pairs of xy coordinates on a
simple graph (MyFunc is a smooth polynomial curve). Can anyone suggest
my options? Here is some code, where "MyFunc" is actually
"BernsteinPoly":
####################################
function retval = BinomialCoefficient( degree, instance )
    retval = prod( 1:degree );
    retval = retval / ( prod(1:instance) * prod(1:(degree - instance))
);
endfunction

# x between 0.0 and 1.0. degree and instance whole numbers. degree
# between 1 and n, instance betwee 0 and n-1.
function retval = BernsteinPoly( x, degree, instance )
   coefficient = BinomialCoefficient( degree, instance );
#   printf( "x, degree, instance: %1.1f, %d, %d\n", \
#      [x'; [degree;instance]*ones(1,rows(x))] );
   left_weight = x ** instance;
   right_weight = (( 1 - x ) ^ ( degree - instance ));
   blend = left_weight * right_weight;
   retval = coefficient * blend;
endfunction
####################################

You'll see that "left_weight" of BernsteinPoly uses "x ** instance".
This is where I'm messing up by causing "x" to be a matrix instead of a
scalar passed multiple times. Since I might be mixing functions later
and looking at piecewise construction of a larger curve from multiple
segments, with each segment using a different forumula, I'm worried that
adding direct matrix handling abilities inside of BernsteinPoly (versus
expecting "x" to be scalar) might cause other troubles in the future.
Any advice?

D. Stimits, address@hidden

"D. Stimits" wrote:
> 
> I'm trying to plot a function that is similar to sin(). I can easily do
> this:
> x = (0.0:0.1:1.0)';
> data = [x, sin(x)];
> 
> ...and then plot via "data".
> 
> However, I have a function that takes 3 arguments, not 1. Two of the
> arguments will be held constant and only the first argument is being
> used with "x":
> function retval = TestMe( x, y, z )
>    printf( "\nx, y, z: %f, %d, %d\n", x, y, z );
>    retval = x + y;
> endfunction
> 
> (it'll always be called with x as a float between 0.0 and 1.0, y will
> always be 3, and z will always be 2)
> 
> Unfortunately, only the "x" argument is behaving properly. The "y" and
> "z" arguments should always be printed out as 3 and 2, but they are
not.
> I don't seem to understand this, I thought that "[x, Function(x,3,2)]"
> would result in 10 rows of data (since x was assigned 10 values), and
be
> two columns wide (since "x" takes one column, and "Function" returns
> another single value). The second and third arguments to Function
should
> *ALWAYS* be 3 and 2, since those are explicitly stated, and variables
> are not used. But they are accurate only once. Then they change:
> octave:1> x = ( 0.0:0.1:1.0 )';
> octave:2> function retval = TestMe( x, y, z )
> >    printf( "\nx, y, z: %f, %d, %d\n", x, y, z );
> >    retval = x + y;
> > endfunction
> octave:3> data = [x, TestMe(x,3,2)];
> 
> x, y, z: 0.000000, 0, 0
> 
> x, y, z: 0.300000, 0, 0
> 
> x, y, z: 0.600000, 0, 0
> 
> x, y, z: 0.900000, 1, 3
> 
> x, y, z: 2.000000, octave:4>
> 
> What also looks strange is that the final printf() statement is never
> completely printed. The final printf never prints out anything for
> arguments 2 or 3, and fails to print the newline. The 2nd and 3rd
> arguments are never "3" and "2", I would expect them to always be 3 and
> 2. And since there are 10 "x" values, not 5, it appears that every
other
> one is completely missing, other than the newline being printed. I
> verified that "x" consists of 10 values in increments of 0.1. Are these
> bugs? Is there another way to fill up "data" without manually looping
> and expicitly appending an answer to a matrix each loop?
> 
> Platform is Linux, Redhat 7.1. octave version is 2.1.33.
> 
> D. Stimits, address@hidden
> 
> -------------------------------------------------------------
> Octave is freely available under the terms of the GNU GPL.
> 
> Octave's home on the web:  http://www.octave.org
> How to fund new projects:  http://www.octave.org/funding.html
> Subscription information:  http://www.octave.org/archive.html
> -------------------------------------------------------------



-------------------------------------------------------------
Octave is freely available under the terms of the GNU GPL.

Octave's home on the web:  http://www.octave.org
How to fund new projects:  http://www.octave.org/funding.html
Subscription information:  http://www.octave.org/archive.html
-------------------------------------------------------------



reply via email to

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