Tag Info

New answers tagged

3

Table and Sum scope their variables in the manner of Block. Effectively your code is like this: f[x_] := Block[{i = 5}, x] f /@ {a, i} {a, 5} This is actually a very useful aspect of Table but in this case it is also the source of your problems. Since you must localize the iterators one of the best solutions is the one you reject out of hand which ...


2

If I understand correctly, your problem is that you do not want to have to comb through this block of code manually to find which variables are being used as iterators. Fortunately, you can do this with pattern matching. Say we have this definition: ClearAll@"Global`*"; function[x_] := Module[{}, notanswer = Sum[j^2, {j, 10}]; answer = Table[x, {i, ...


5

Make use of Module's capability to localize variables. f[x_] := Module[{i}, Table[x, {i, 1, 3}]] f[i] {i, i, i} Also, with i localized, you don't need to use distinct iterator names in different iteration constructs. g[x_] := Module[{i, a, b}, a = Table[x, {i, 3}]; b = Table[x^3, {i, 2}]; {a, b}] {g[i], g[a], g[b]} {{{i, i, i}, {i^3, ...


3

It is not clear to me what you desire. You wrote: I know I can do this using Block wrapped around the call to f and g Yet you did not articulate why this is not the solution that you want. Rather than wrapping the call to (use of) f and g I would put Block inside those functions: f[x_] := Block[{h = Plus}, h[5, x]] g[x_] := Block[{h = Power}, h[5, ...


5

You are correct here: it would appear to be appropriate only when the precise pattern of h within f or g is known. The reason for this is that the pattern engine generally moves outside-in as it is rewriting expressions. A simple example is this: a[b[c[d]]] /. s_Symbol :> (Sow@s; s) // Reap {a[b[c[d]]], {{a, b, c, d}}} By contrast, actual ...


1

I am not sure I completely understand, but perhaps something like (using TagSetDelayed): h /: f[h[s_]] := f[s^2 + a] h /: g[h[s_]] := g[s^3 + b] or perhaps more simply (but I personally find harder to "read") f[h[s_]] ^:= f[s^2 + a] g[h[s_]] ^:= f[s^3 + b] now h has been defined as follows: f[h[x]] (* f[a + x^2] *) and g[h[x]] (* g[b + x^3] *) ...


1

There are lots of ways to do what you want. I would not use Module. Here are three, all of which use methods other than Module to localize variables: SeedRandom @ 42; With[{rand = RandomInteger[10, {5, 5, 2}]}, Manipulate[ ListPlot[rand[[i]], PlotRange -> {{-1, 11}, {-1, 11}}], {i, 1, Length[rand], 1, Appearance -> "Labeled"}]] SeedRandom @ ...


1

if you need to keep the definition of l inside manipulate, I think you can try this Manipulate[l = RandomInteger[10, {3, 5, 2}]; ListPlot[l[[i]], PlotRange -> {{-1, 11}, {-1, 11}}], {i, 1, Dynamic@Length@l, 1}] you need to know that for every i, l will be computed again and again. if you want to do 3 plot per each l then you can do it like this ...


2

Here is one approach to using a more reasonable stopping criterion. For clarity we separately define a function that effects one step of the Newton-Raphson procedure. The first argument will be a function rather than a function expression. newtonStep[{f_, x_}] := {f, x - f[x]/f'[x]} newton[f_, start_, \[Delta]_] := Last /@ NestWhileList[newtonStep, ...


7

"Is this a better way?" Yes, but I think there are alternatives: Nest newton1[fun_, xi_, n_] := With[{f = fun/D[fun, x]}, Nest[# - f /. x -> # &, 2., n]] newton1[x^3 - 2, 2., 10] 1.25992 NestList newton2[fun_, xi_, n_] := With[{f = fun/D[fun, x]}, NestList[# - f /. x -> # &, 2., n]] ListLinePlot[newton2[x^3 - 2, 2., 10], Mesh ...



Top 50 recent answers are included