1
$\begingroup$

I am attempting to create a loop (either Do, For or While) that will continue to take antiderivatives of f(x)= x until the area under it between 0 and 10 is greater than 1000. And whichever function that is, the output should be that specific function.. I've done some leg work but I am certainly confused a bit..

I know that the integral of x^5/120 (which is the antiderivative of x, 4 times) is the function of desire because this antiderivative is x^6/720. Evaluating this from 0 to 10 yields 1388.8 which is greater than 1000.

I also know that:

Clear[f, x];
f[x_] = x;
TableForm[Rest@NestList[Integrate[#, x] &, f[x], 5]]

would produce a nice output of all the antiderivatives, which ends at x^6/120..

This was my best attempt at creating a loop that could solve this task..It doesn't produce anything, but its something I am working on..

Clear[f, x];
f[x_] = x;
Do[If[Integrate[f[x] > 1000, x],
Print[ToString[f[x] <> "area between 0 and 10 is not greater than 1000"],
Print[ToString[f[x] <> "area between 0 and 10 is greater than 1000"]],
{x, 1, 5}]]

The last part of the code is probably what's wrong. I'm trying to somehow iterate the function until that IF condition is satisfied..

Honestly I'm just throwing some things together but nothing is working the way I want it to. Any suggestions would be very helpful. Usually after some trial and error I will get the answer I need but I sure do spend a lot of time doing so!

Thanks again, Brandon

$\endgroup$

1 Answer 1

2
$\begingroup$

We discourage using loops in Mathematica. This is a functional way of doing the same thing:

f[x_] = x;

NestWhile[Integrate[#, x] &, f[x], (If[Integrate[#, {x, 0, 10}] < 1000, 
Print[Integrate[#, x], " area between 0 and 10 is not greater than 1000"], 
Print[Integrate[#, x], " area between 0 and 10 is greater than 1000"]]; 
Integrate[#, {x, 0, 10}] < 1000) &];

If you insist on being procedural, you could do this:

f[x_] = x;
Module[{f = f},
 Do[
  If[Integrate[f[x], {x, 0, 10}] < 1000, 
    Print[Integrate[f[x], x], " area between 0 and 10 is not greater than 1000"],
    Print[Integrate[f[x], x], " area between 0 and 10 is greater than 1000"]; Return[]];
  f[x_] = Integrate[f[x], x];
 ,10000]
]
$\endgroup$
4
  • $\begingroup$ This is perfect. I suppose I was a ways off, but I was certainly in the right direction I would like to think...Thank you! $\endgroup$
    – Brandon
    Oct 16, 2016 at 5:24
  • $\begingroup$ An alternative: NestWhile[Integrate[#, x] &, x, ((# /. x -> 10) <= 10^3) &, 1, ∞, -1]. $\endgroup$ Oct 16, 2016 at 5:29
  • $\begingroup$ Hey @J.M. That's just as great! You guys rock. $\endgroup$
    – Brandon
    Oct 16, 2016 at 5:43
  • $\begingroup$ Yet another alternative: D[NestWhile[Integrate[#,x]&,x,(#/.x->10)<=1000&],x] $\endgroup$
    – dan7geo
    Oct 16, 2016 at 6:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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