1
$\begingroup$

Okay I get that this should be simple but I'm at my wit's end. Very simple problem:

I have an expression

f[x_] := 5 x + 1

wherein x can be any whole number $0$, $1$, $2$, $3$, $\dots$

I want the list of all possible values up to/including that at the maximum point of the function's allowed range of output.

Obviously we're just incrementing the prior value from and including 1 (when x = 0) until the next incrementation would exceed the maximum value allowed by the restricted range (not the same as the range function in Mathematica of course; I'm talking about the domain/range concepts of functions generally), which I want to be able to assign as a variable (let's say m).

If I were to say the maximum (inclusive) value of the range of the function f is m = 30, then my output should be a list of all values from and including 1, to and including the last that's less than or equal to m = 30, as below. Please help me write the proper code for this.

Expected output: {1, 6, 11, 16, 21, 26} (more than this would exceed 30 so it terminates).

We could use While or Do, but the important point is I can't limit the evaluation based on number of increments because I won't necessarily know how many will be required, only how large a maximal value that the final output must be less than (e.g., m = 30).

$\endgroup$
  • $\begingroup$ Maybe MaxValue[{5 x + 1, 5 x + 1 <= 30}, x \[Element] Integers] $\endgroup$ – corey979 Sep 6 '16 at 20:34
  • $\begingroup$ That would work if I only needed the maximum value, but I need the list of all values within the range in order, so 26 needs to be preceded by 1,6,11,16,21 $\endgroup$ – Travis Arlen McCracken Sep 6 '16 at 20:37
  • $\begingroup$ I see. Do you want it only over over integers? May I assume that the functions f are not something ill or extremely complex numerically? $\endgroup$ – corey979 Sep 6 '16 at 20:39
  • 1
    $\begingroup$ f[x_] := 5 x + 1; m = 30; x = 0; Flatten@Rest@Reap[While[f[x] <= m, Sow[f[x]]; x++]] ? $\endgroup$ – corey979 Sep 6 '16 at 20:57
  • 1
    $\begingroup$ Solve[5 x + 1 == y && 0 <= y <= 30, y, Integers] $\endgroup$ – chuy Sep 6 '16 at 21:57
1
$\begingroup$

Let's take the function

f[x_] := 5 x + 1

and set the maximum value m that it is not allowed to exceed:

m = 30;

The domain of x is Integers; Sow and Reap will gather the instances fulfilling the condition f[x]<=m until While is executed:

x = 0;
Flatten@Rest@Reap[While[f[x] <= m, Sow[f[x]]; x++]]

{1, 6, 11, 16, 21, 26}

A different approach would be to find the maximal value of f[x], the Solve to get a corresponding xmax, and finally make a suitable Table:

max = MaxValue[{f[x], f[x] <= m}, x \[Element] Integers]

26

xmax = x /. First@Solve[f[x] == max, x]

5

Table[f[x], {x, 0, xmax}]

{1, 6, 11, 16, 21, 26}

$\endgroup$
1
$\begingroup$
f[x_] = 5 x + 1;

f[x] /. Solve[{f[x] <= 30, x >= 0}, x, Integers]

(*  {1, 6, 11, 16, 21, 26}  *)
$\endgroup$
1
$\begingroup$

You can use ArgMax:

f[x_] := 5 x + 1

f /@ Range[ArgMax[{f[x], f[x] <= 30}, x, Integers]]
(* {6, 11, 16, 21, 26} *)
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.