Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2005

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Do loop is faulty

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55389] Re: Do loop is faulty
  • From: "Hans Michel" <hmichel at sdc.cox.net>
  • Date: Mon, 21 Mar 2005 03:01:56 -0500 (EST)
  • References: <d1jfsj$sa4$1@smc.vnet.net>
  • Reply-to: "Hans Michel" <hmichel at sdc.cox.net>
  • Sender: owner-wri-mathgroup at wolfram.com

DF:

You have given up as predicted. Or fallen off the Mathematica band wagon.

If you state your problem in just plain english or pure math you will find 
that the kind people in this newsgroup may be willling to help more. Because 
then they will have more info as to what you are trying to do or solve. You 
will probably get more answers than you can deal with and if you do I would 
suggest trying all the replies, because one of them may fit your style. The 
code can sometimes be obsfucate, but it can always be deconstructed.

You are trying to Numerically Integrate

a*x*Sin[b*x] as x goes from 0 to b

what is a and b?

Every one is telling you to stay away from Do loops, For Loops. Mathematica 
is an impure functional language (But only the people at Wolfram can tell 
you what it is, "A system for doing ..."). It provides flow controls that 
are similar to the way things are done in an imperative language. But now 
you are trying to combine the two methods, but you are unwilling to learn 
the syntax. The Array[] function generates a List. List are the basis of 
most things in Mathematica.

It took a few seconds of cut and paste to produce this.

{NIntegrate[1*x*Sin[(1/10)*x] , {x, 0, (1/10)}],
  NIntegrate[2*x*Sin[(2/10)*x] , {x, 0, (2/10)}],
  NIntegrate[3*x*Sin[(3/10)*x] , {x, 0, (3/10)}],
  NIntegrate[4*x*Sin[(4/10)*x] , {x, 0, (4/10)}],
  NIntegrate[5*x*Sin[(5/10)*x] , {x, 0, (5/10)}],
  NIntegrate[6*x*Sin[(6/10)*x] , {x, 0, (6/10)}],
  NIntegrate[7*x*Sin[(7/10)*x] , {x, 0, (7/10)}],
  NIntegrate[8*x*Sin[(8/10)*x] , {x, 0, (8/10)}],
  NIntegrate[9*x*Sin[(9/10)*x] , {x, 0, (9/10)}],
  NIntegrate[10*x*Sin[(10/10)*x] , {x, 0, (10/10)}]}

{0.000033333, 0.0010665, 0.00809344, 0.034046, 0.103517, 0.255856, 0.546897, 
\
1.04818, 1.84215, 3.01169}

a few more seconds to produce

a = Range[10];
b = Range[10]/10;
Table[NIntegrate[a[[i]]*x*Sin[b[[i]]*x], {x, 0, b[[i]]}], {i, 1, 10}]

{0.000033333, 0.0010665, 0.00809344, 0.034046, 0.103517, 0.255856, 0.546897, 
\
1.04818, 1.84215, 3.01169}

Notice the "[[" "]]" double square brackets.

It is not Mathematica or you that is at fault here it is your approach. Most 
people in this group are telling you to abandon the approach. Because for 
the few minutes you practice other problems in the book or help files you 
will reap it back in productivity.

Don't just try to get your thing done. You don't need Mathematica to get 
your thing done. There are lots of other programs out there. But you will 
run into the same problem of learning how to use them efficiently.

Hans

"dumb_founded" <andreajagger_8 at hotmail.com> wrote in message 
news:d1jfsj$sa4$1 at smc.vnet.net...
>I define array a[i] and array b[i] of dimension 10.  Then I go through
> the following Do loop, or try to, that is.
>
>
> Array[c,10];
> Do[c[i]=NIntegrate[a[i]*x*Sin[b[i]*x],{x,0,b[i]}]
>
> I get numerous error messages saying that b[i] is not a valid limit of
> integration.
>
> This is sheer headache.  I will never try to use Mathematica for
> numerical analysis again.
>
> All your efforts on this matter are sincerely appreciated.
>
>
>
> Thanks
> 


  • Prev by Date: Re: plotting weighted graphs
  • Next by Date: More J/Link examples pls
  • Previous by thread: Re: Do loop is faulty
  • Next by thread: Re: Do loop is faulty