Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Using functions with parameters

19 views
Skip to first unread message

Herman Rubin

unread,
Apr 11, 2013, 6:12:26 PM4/11/13
to
Suppose one has a procedure P which takes a function or functions
and produces a number or numbers. The numbers produced are NOT
integrals. Now suppose one has a function with a parameter (I
know that Maple does not have this, but does it as a function
of two arguments) and I want to produce a graph of the values
of P on the function as a function of the parameter. How to do
this without rewriting everything?


--
This address is for information only. I do not claim that these views
are those of the Statistics Department or of Purdue University.
Herman Rubin, Department of Statistics, Purdue University
hru...@stat.purdue.edu Phone: (765)494-6054 FAX: (765)494-0558

Axel Vogt

unread,
Apr 12, 2013, 2:27:02 AM4/12/13
to
On 12.04.2013 00:12, Herman Rubin wrote:
> Suppose one has a procedure P which takes a function or functions
> and produces a number or numbers. The numbers produced are NOT
> integrals. Now suppose one has a function with a parameter (I
> know that Maple does not have this, but does it as a function
> of two arguments) and I want to produce a graph of the values
> of P on the function as a function of the parameter. How to do
> this without rewriting everything?

Do you mean the function F(p,.) = a |---> F(p,a) ?

F:= proc(x,a) sin(a*x) end proc; # example
plot( F(1, a), a= 0 .. 4*Pi);



Herman Rubin

unread,
Apr 12, 2013, 1:29:15 PM4/12/13
to
It is not that simple. The procedure uses the function
at a finite number of arguments, computed recursively and
depending on the functions, to get two numbers.

Axel Vogt

unread,
Apr 12, 2013, 2:53:45 PM4/12/13
to
On 12.04.2013 19:29, Herman Rubin wrote:
> On 2013-04-12, Axel Vogt <&nor...@axelvogt.de> wrote:
>> On 12.04.2013 00:12, Herman Rubin wrote:
>>> Suppose one has a procedure P which takes a function or functions
>>> and produces a number or numbers. The numbers produced are NOT
>>> integrals. Now suppose one has a function with a parameter (I
>>> know that Maple does not have this, but does it as a function
>>> of two arguments) and I want to produce a graph of the values
>>> of P on the function as a function of the parameter. How to do
>>> this without rewriting everything?
>
>> Do you mean the function F(p,.) = a |---> F(p,a) ?
>
>> F:= proc(x,a) sin(a*x) end proc; # example
>> plot( F(1, a), a= 0 .. 4*Pi);
>
>
> It is not that simple. The procedure uses the function
> at a finite number of arguments, computed recursively and
> depending on the functions, to get two numbers.

Ok, but I can not imagine what is meant exactly. Would you mind
to provide a formal example (i.e. a procedure, which works like
your actual problem) ?

Herman Rubin

unread,
Apr 12, 2013, 4:22:53 PM4/12/13
to
Here is the procedure which I constructed; it works.
In practice, n will be 128 or 256.

eff:=proc(f,n,start,F)
local s1,s2,i,x,h;
s2:=0;x:=start;
for i from 1 to n do
h:=f(x);
x:= x+ 1/(n*f(x));
s2:=s2+(1-f(x)/h);
end do;
s2:=s2/n;
s1:=F(x);
return s1,s2;
end proc;

Axel Vogt

unread,
Apr 13, 2013, 6:17:59 AM4/13/13
to
On 12.04.2013 22:22, Herman Rubin wrote:
> On 2013-04-12, Axel Vogt <&nor...@axelvogt.de> wrote:
>> On 12.04.2013 19:29, Herman Rubin wrote:
>>> On 2013-04-12, Axel Vogt <&nor...@axelvogt.de> wrote:
>>>> On 12.04.2013 00:12, Herman Rubin wrote:
>>>>> Suppose one has a procedure P which takes a function or functions
>>>>> and produces a number or numbers. The numbers produced are NOT
>>>>> integrals. Now suppose one has a function with a parameter (I
>>>>> know that Maple does not have this, but does it as a function
>>>>> of two arguments) and I want to produce a graph of the values
>>>>> of P on the function as a function of the parameter. How to do
>>>>> this without rewriting everything?
...
> Here is the procedure which I constructed; it works.
> In practice, n will be 128 or 256.
>
> eff:=proc(f,n,start,F)
> local s1,s2,i,x,h;
> s2:=0;x:=start;
> for i from 1 to n do
> h:=f(x);
> x:= x+ 1/(n*f(x));
> s2:=s2+(1-f(x)/h);
> end do;
> s2:=s2/n;
> s1:=F(x);
> return s1,s2;
> end proc;

I would amend it to return a pair (and personally only return s2, since s1 = F(s2)).
Likewise I would use a subsequent proc to do modify the output.

An 'option hfloat' will speed up computations (the code can be improved a bit more).

Eff:=proc(f,n,start,F)
option hfloat; # to speed up numerics
local s1,s2,i,x,h;
s2:=0;x:=start;
for i from 1 to n do
h:=f(x);
x:= x+ 1/(n*f(x));
s2:=s2+(1-f(x)/h);
end do;
s2:=s2/n;
s1:=F(x);
return [s1,s2]; # return as pair
end proc;


# a small test
Eff(exp, 8, 2.1, 'x -> x^2');

[4.91205739100430, -0.0146458557860588]

Then I can plot (using only few data points by option 'numpoints')

nTst:=8; fTst:=ln; FTst:='x -> x';
plot( Eff(fTst, nTst, s, FTst), s=1 ..4, numpoints=5);


Herman Rubin

unread,
Apr 13, 2013, 3:09:48 PM4/13/13
to
The pair idea may or may not be useful. However, s1 is NOT F(s2); s1
will always be less than s2, and in most cases, I believe about half.

> Likewise I would use a subsequent proc to do modify the output.



> An 'option hfloat' will speed up computations (the code can be improved a bit more).

> Eff:=proc(f,n,start,F)
> option hfloat; # to speed up numerics
> local s1,s2,i,x,h;
> s2:=0;x:=start;
> for i from 1 to n do
> h:=f(x);
> x:= x+ 1/(n*f(x));
> s2:=s2+(1-f(x)/h);
> end do;
> s2:=s2/n;
> s1:=F(x);
> return [s1,s2]; # return as pair
> end proc;


> # a small test
> Eff(exp, 8, 2.1, 'x -> x^2');

> [4.91205739100430, -0.0146458557860588]

A test, but not appropriate It was designed for f a decreasing
probability density, and F the corresponding tail probability.

> Then I can plot (using only few data points by option 'numpoints')

> nTst:=8; fTst:=ln; FTst:='x -> x';
> plot( Eff(fTst, nTst, s, FTst), s=1 ..4, numpoints=5);

This is a misconception of what is wanted. What is wanted
is to have a parametric family of densities, and plot the
values as a function of the parameters. This means that
eff has to be entered with a different function for each
value of the parameter.
0 new messages