[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: plot data from multiple arguments
From: |
Etienne Grossmann |
Subject: |
Re: plot data from multiple arguments |
Date: |
Tue, 30 Oct 2001 08:27:52 +0000 |
From: "D. Stimits" <address@hidden>
# 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"
Right. Octave provides operators for that, such as c = a .* b
(c(i,j)=a(i,j)*b(i,j)), or ./ (term by term division), .^
(exponentiation).
# returns a single float between 0 and 1, much like sin(x) would. I'm
^^^^
In octave, it's all doubles.
# 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":
I am not sure I get your question right. But the func below could
illustrate the usage of these operators.
## x : column vector of "input values"
## c : column vector of coefficients of poly (degree 0, 1, etc)
function y = poly_foo (x, c)
x = x(:); ## Make sure x and c are columns
c = c(:);
d = length (c); ## Degree of poly + 1
l = length (x);
y = sum (((ones(d,1)*x') .^ ([0:d-1]'*ones(1,l))) .* (c*ones(1,l)));
endfunction
Here,
a = ones(d,1)*x' is a (d x l) matrix, each row is equal
to x'.
b = [0:d-1]'*ones(1,l) is also (d x l), the first row is zeros,
second is ones, third is twos, etc
e = a .^ b (d x l) : e(i,j) = x(j)^i
f = c .* (c*ones(1,l)) (d x l) : f(i,j) = c(i) * x(j)^i
g = sum (f) (1 x l). "sum" does the sum of each
column. So g(j) = sum (i=1 to d, f(i,j)) =
= c(1)*x(j)^0 + ... + c(d)*x(j)^(d-1).
(warn: sum (row_vector) returns sum of
elements of row_vector)
Hth,
Etienne
ps : You will probably save a lot of time by studying a little bit the
octave docs, for basic functions and polynomial functions.
# ####################################
# 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?
-------------------------------------------------------------
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
-------------------------------------------------------------