MathGroup Archive 2005

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

Search the Archive

Re: Do loop is faulty

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55386] Re: [mg55368] Do loop is faulty
  • From: DrBob <drbob at bigfoot.com>
  • Date: Mon, 21 Mar 2005 03:01:53 -0500 (EST)
  • References: <200503200912.EAA28772@smc.vnet.net>
  • Reply-to: drbob at bigfoot.com
  • Sender: owner-wri-mathgroup at wolfram.com

First, note that this statement has no effect or purpose whatsoever:

Array[c,10];

(except to make c a Global variable). You probably thought you were "declaring" an array, specifying its length, or some such thing. But you weren't.

Second, this statement is incomplete:

Do[c[i]=NIntegrate[a[i]*x*Sin[b[i]*x],{x,0,b[i]}]

You omitted Do's iterator, so possibly you meant something like

Do[c[i]=NIntegrate[a[i]*x*Sin[b[i]*x],{x,0,b[i]}],{i,1,10}]

That is fine if a, b, and c are functions, but you said a and b were arrays. In that case, here's a solution that makes c a function, ending with an array formed from it:

a = Range[10];
b = Range[10];
Clear[c]
Do[c[i] = NIntegrate[
     a[[i]]*x*Sin[b[[i]]*x],
     {x, 0, b[[i]]}], {i, 1, 10}]
c /@ Range[10]

{0.301169, 0.928886, 2.87076, 3.75866, -4.98248, 0.602486, -2.2404, -3.01985, \
-7.06016, -8.67383}

Here's a solution that makes c an array:

Clear@c
c = Table[NIntegrate[a[[i]]*x*Sin[b[[i]]*x], {x, 0, b[[i]]}], {i, 1, 10}]

{0.301169, 0.928886, 2.87076, 3.75866, -4.98248, 0.602486, -2.2404, -3.01985, \
-7.06016, -8.67383}

And here's another interesting method:

c = NIntegrate[#1,{x,0,#2}]&@@@Transpose@{a x Sin[b x],b}

{0.301169, 0.928886, 2.87076, 3.75866, -4.98248, 0.602486, -2.2404, -3.01985, \
-7.06016, -8.67383}

> This is sheer headache.  I will never try to use Mathematica for
> numerical analysis again.

Did you expect to master the language without study or practice?

I hope you don't give up on everything in life so quickly.

Bobby

On Sun, 20 Mar 2005 04:12:05 -0500 (EST), dumb_founded <andreajagger_8 at hotmail.com> wrote:

> 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
>
>
>
>



-- 
DrBob at bigfoot.com


  • Prev by Date: Re: Do loop is faulty
  • Next by Date: Re: Do loop is faulty
  • Previous by thread: Re: Do loop is faulty
  • Next by thread: Re: Do loop is faulty