R: Local iterator
- To: mathgroup at smc.vnet.net
- Subject: [mg29398] R: Local iterator
- From: "Vincenzo Vasta" <vvasta at btinternet.com>
- Date: Sat, 16 Jun 2001 22:43:48 -0400 (EDT)
- References: <9gevtc$7l0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
Given a function y = f[x] I would like to write a function that returns,
for a given interval and a
given step, the maximum value of y, and the x that generated this value. For
example:
In[4] := funMax[ Log[x]/x^3 ,x ,1 ,3 ,0.001 ]
Out[4]= {0.122626, 1.396}
With my very little knowledge of Mathematica I wrote funMax like this:
Clear[funMax];
SetAttributes[funMax, HoldAll];
funMax[fun_, v_, x1_, x2_, step_] := Block[
{v, maxVal = {0, 0} , fval},
For[v = x1, v <= x2, v += step,
fval = fun;
If [maxVal[[1]] < fval,
maxVal = { fval, v}, 0]
];
maxVal
]
This function works and I'm happy with that but I would like to know how can
I write it using an
iterator. It is just a curiosity about the language.
Thanks for your help
Vincenzo