Skip to main content
Routine clean-up
Source Link
m_goldberg
  • 108.2k
  • 16
  • 104
  • 259

Passing a loop index value into NIntegrate[]NIntegrate

I would like to use the NIntegrate[] function to calculate an integral within a Do[] Loop, and pass in the value of the index into the integrand.

As a simple, hypothetical example I would like to calculate the following integrals:

$C_1[T]$= $\int_0^1 \frac{T}1 dx $

$C_2[T]$= $\int_0^1 \frac{T}2 dx $

$C_3[T]$= $\int_0^1 \frac{T}3 dx $

...

$C_j[T]$= $\int_0^1 \frac{T}j dx $

Let's just say j$j$ goes from 1 to 3.

Then I would expect you could easily compute these three integrals in a Do[] loopDo-loop.

For example

ClearAll["Global`*"]
 
Do[Subscript[C, j][T_ ]j][T_] :=  NIntegrate[(T/j), {x, 0, 1}], {j, 3}]

And let's say you want to plot $C_1[T]$...

Plot[Subscript[C, 1][T], {T, 0, 1}]

When you evaluate the cell, you get the following error:

NIntegrate::inumr: The integrand 0.0000204286/j has evaluated to non non-numerical values for all sampling points in the region with boundaries boundaries {{0,1}}. >>

This indicates that jj is not being "seen" as it's numerical value, just the variable jj, and therefore the integrand cannot be numerically integrated.

...Does Does anyone know of a way to pass in the numerical value of the index "j"j into the NIntegrate[] functionNIntegrate when you're using it in a loop?

Thanks!

Passing a loop index value into NIntegrate[]

I would like to use the NIntegrate[] function to calculate an integral within a Do[] Loop, and pass in the value of the index into the integrand.

As a simple, hypothetical example I would like to calculate the following integrals:

$C_1[T]$= $\int_0^1 \frac{T}1 dx $

$C_2[T]$= $\int_0^1 \frac{T}2 dx $

$C_3[T]$= $\int_0^1 \frac{T}3 dx $

...

$C_j[T]$= $\int_0^1 \frac{T}j dx $

Let's just say j goes from 1 to 3.

Then I would expect you could easily compute these three integrals in a Do[] loop.

For example

ClearAll["Global`*"]
 
Do[Subscript[C, j][T_ ] := NIntegrate[(T/j), {x, 0, 1}], {j, 3}]

And let's say you want to plot $C_1[T]$...

Plot[Subscript[C, 1][T], {T, 0, 1}]

When you evaluate the cell, you get the following error:

NIntegrate::inumr: The integrand 0.0000204286/j has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

This indicates that j is not being "seen" as it's numerical value, just the variable j, and therefore the integrand cannot be numerically integrated.

...Does anyone know of a way to pass in the numerical value of the index "j" into the NIntegrate[] function when you're using it in a loop?

Thanks!

Passing a loop index value into NIntegrate

I would like to use the NIntegrate[] function to calculate an integral within a Do[] Loop, and pass in the value of the index into the integrand.

As a simple, hypothetical example I would like to calculate the following integrals:

$C_1[T]$= $\int_0^1 \frac{T}1 dx $

$C_2[T]$= $\int_0^1 \frac{T}2 dx $

$C_3[T]$= $\int_0^1 \frac{T}3 dx $

...

$C_j[T]$= $\int_0^1 \frac{T}j dx $

Let's just say $j$ goes from 1 to 3.

Then I would expect you could easily compute these three integrals in a Do-loop.

For example

ClearAll["Global`*"]
Do[Subscript[C, j][T_] :=  NIntegrate[(T/j), {x, 0, 1}], {j, 3}]

And let's say you want to plot $C_1[T]$...

Plot[Subscript[C, 1][T], {T, 0, 1}]

When you evaluate the cell, you get the following error:

NIntegrate::inumr: The integrand 0.0000204286/j has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

This indicates that j is not being "seen" as it's numerical value, just the variable j, and therefore the integrand cannot be numerically integrated.

Does anyone know of a way to pass in the numerical value of the index j into NIntegrate when you're using it in a loop?

Source Link
Ethan
  • 15
  • 6

Passing a loop index value into NIntegrate[]

I would like to use the NIntegrate[] function to calculate an integral within a Do[] Loop, and pass in the value of the index into the integrand.

As a simple, hypothetical example I would like to calculate the following integrals:

$C_1[T]$= $\int_0^1 \frac{T}1 dx $

$C_2[T]$= $\int_0^1 \frac{T}2 dx $

$C_3[T]$= $\int_0^1 \frac{T}3 dx $

...

$C_j[T]$= $\int_0^1 \frac{T}j dx $

Let's just say j goes from 1 to 3.

Then I would expect you could easily compute these three integrals in a Do[] loop.

For example

ClearAll["Global`*"]

Do[Subscript[C, j][T_ ] := NIntegrate[(T/j), {x, 0, 1}], {j, 3}]

And let's say you want to plot $C_1[T]$...

Plot[Subscript[C, 1][T], {T, 0, 1}]

When you evaluate the cell, you get the following error:

NIntegrate::inumr: The integrand 0.0000204286/j has evaluated to non-numerical values for all sampling points in the region with boundaries {{0,1}}. >>

This indicates that j is not being "seen" as it's numerical value, just the variable j, and therefore the integrand cannot be numerically integrated.

...Does anyone know of a way to pass in the numerical value of the index "j" into the NIntegrate[] function when you're using it in a loop?

Thanks!