Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #2107 > unrolled thread
| Started by | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| First post | 2011-05-04 23:48 +0000 |
| Last post | 2011-05-10 00:20 +0000 |
| Articles | 2 — 2 participants |
Back to article view | Back to comp.soft-sys.math.mathematica
Re: Replacements and NIntegrate Oliver Ruebenkoenig <ruebenko@wolfram.com> - 2011-05-04 23:48 +0000
Re: Replacements and NIntegrate Giacomo Ciani <jackspam79@gmail.com> - 2011-05-10 00:20 +0000
| From | Oliver Ruebenkoenig <ruebenko@wolfram.com> |
|---|---|
| Date | 2011-05-04 23:48 +0000 |
| Subject | Re: Replacements and NIntegrate |
| Message-ID | <ipsokh$8cj$1@smc.vnet.net> |
On Wed, 4 May 2011, Giacomo wrote:
> Wonderful!
>
> Simple idea but... I didn't have it! My bad...
>
> Actually, I was also trying to crate a table with the NIntegrate
> function called with different value of the parameters, and run into the
> same problem (Mathematica trying to evaluate the function before the
> values of the parameters are assigned). This solution makes everything
> much easier!
>
> Thanks!
>
> Giacomo
>
> On 03-May-11 17:15, DrMajorBob wrote:
>> Here it is in TWO replacements:
>>
>> dummy[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}] /. vals /.
>> dummy -> NIntegrate
>>
How about this:
NIntegrate @@ ({h[z] z/Sqrt[L^2 + z^2], {z, -L, L}} /. vals)
Oliver
>> Bobby
>>
>> On Tue, 03 May 2011 15:36:34 -0500, Giacomo <jackspam79@gmail.com> wrote:
>>
>>> On 03-May-11 13:41, DrMajorBob wrote:
>>>> You answered your own question, since
>>>>
>>>> NIntegrate[ h[z] z / Sqrt[L^2 + z^2] /.vals, {z, -L /.vals, L/.vals}]
>>>>
>>>> does the replacements before trying to integrate.
>>>>
>>>
>>> I know, but having to specify three time the same replacement rule in
>>> the same expression doesn't look very elegant. :-)
>>>
>>>> Or, you could properly define h[z_,a_,b_....] as a function of its
>>>> arguments and parameters and L[a_,b_, ...] as a function of ITS
>>>> arguments, rather than leaving most of them out.
>>>
>>> Well, L is just a parameter by itself, whose numerical value is
>>> specified in the set of replacement rules "vals" defined at the very
>>> beginning of the notebook. h is indeed a function, but depends on may
>>> "parameters" that are not really "variables". I don't see it
>>> practical (nor clear from a logical point of view) to specify them as
>>> variables...
>>>
>>> Thanks anyway!
>>>
>>> Giacomo
>>>
>>>>
>>>>
>>>> It's generally a good idea to define functions with ALL their
>>>> dependencies obvious in the definition. It leads to less confusion.
>>>>
>>>> Bobby
>>>>
>>>> On Tue, 03 May 2011 04:44:43 -0500, Giacomo Ciani
>>>> <jackspam79@gmail.com> wrote:
>>>>
>>>>> Hi all,
>>>>>
>>>>> I've been reading quite a bit in the Mathematica docs and in this
>>>>> newsgroup, but didn't find (or didn't recognize...) an answer to my
>>>>> problem.
>>>>>
>>>>> I want to evaluate the following expression:
>>>>>
>>>>> NIntegrate[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}]
>>>>>
>>>>> where h[z] has a delayed value set previously in the notebook. Also, I
>>>>> have previously defined a set of replacement rules in the form:
>>>>>
>>>>> vals = {a->1, b->2, ec....}
>>>>>
>>>>> to be used to specify the numerical values of the various parameters
>>>>> (including those present in the delayed value of h[z]).
>>>>>
>>>>> As for now, the only (brute force) way I found to have my expression
>>>>> correctly evaluated is to apply replacement rules separately to each
>>>>> argument of NIntegrate (including integration limits):
>>>>>
>>>>> NIntegrate[ h[z] z / Sqrt[L^2 + z^2] /.vals, {z, -L /.vals, L/.vals}]
>>>>>
>>>>> I think you agree with me that this does not look very elegant.
>>>>> Instead, I would like to be able to write something like this:
>>>>>
>>>>> NIntegrate[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}]/.vals
>>>>>
>>>>> I know this can't work, as Mathematica tries to evaluate NIntegrate
>>>>> and then apply the replacement rules... but how can I ask Mathematica
>>>>> to apply all the replacement rules and delayed values to an expression
>>>>> without (or before) actually trying to evaluate it?
>>>>>
>>>>> I found a lot of commands to hold the function from evaluating the
>>>>> arguments, while I need pretty much the opposite...
>>>>>
>>>>> Maybe there is something very simple I am overlooking...
>>>>>
>>>>> Thanks
>>>>>
>>>>> Giacomo
>>>>>
>>>>
>>>>
>>>
>>
>>
>
>
>
[toc] | [next] | [standalone]
| From | Giacomo Ciani <jackspam79@gmail.com> |
|---|---|
| Date | 2011-05-10 00:20 +0000 |
| Message-ID | <iqa0cf$d90$1@smc.vnet.net> |
| In reply to | #2107 |
On May 4, 7:48 pm, Oliver Ruebenkoenig <ruebe...@wolfram.com> wrote:
> On Wed, 4 May 2011, Giacomo wrote:
> > Wonderful!
>
> > Simple idea but... I didn't have it! My bad...
>
> > Actually, I was also trying to crate a table with the NIntegrate
> > function called with different value of the parameters, and run into th=
e
> > same problem (Mathematica trying to evaluate the function before the
> > values of the parameters are assigned). This solution makes everything
> > much easier!
>
> > Thanks!
>
> > Giacomo
>
> > On 03-May-11 17:15, DrMajorBob wrote:
> >> Here it is in TWO replacements:
>
> >> dummy[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}] /. vals /.
> >> dummy -> NIntegrate
>
> How about this:
>
> NIntegrate @@ ({h[z] z/Sqrt[L^2 + z^2], {z, -L, L}} /. vals)
>
> Oliver
>
>
Even better! Thanks.
However, using Plot I'm experiencing a case in which the substitution
rule works, and the apply doesn't. And I can't figure out why...
...just in case you are curious (but no one needs to waste his time on
this if it's not for personal enjoyment!), an expression closer to the
real one I'm working with is:
f[R] + NIntegrate[ h[z] z / Sqrt[R^2 + z^2], {z, -L , L}]
what I need to do is plot this expression as a function of L for
different values of R.
To do that I need to create a table in which the appropriate delayed
values for f and h are replaced, my list of substitution rules for
numerical values ("vals") is applied and an entry is created for each
value of R. However, NIntegrate need no to be evaluated, as L is only
assigned a value when the function is plotted.
Obviously, I would also like Plot to recognize the table as such, so
that it plots the functions in different colors.
After experimenting with all your suggestions and ideas, I came up
with this:
Plot[
Evaluate[
Table[
Hold@@{
f[L] + nIntegrate[ h[z] z / Sqrt[L^2 + z^2], {z, -L , L}]
} /.vals /.nIntegrate->NIntegrate
,{R,{1000,2000}}]
]
,{L,10,100}]
And this works as expected! :-)
In an early implementation, to apply the Hold I was using the same
technique I'm using here for NIntegrate (as suggested by DrMajorBob).
Then I switched to your suggestion that is more compact. However, if I
try to do something similar for Nintregrate, it doesn't work anymore.
As a first step, I tried to "hold" only the NIntegrate expression
(istead of f too), so to be able to apply NIntegrate to that portion
only. Here, the plot function stopped working:
Plot[
Evaluate[
Table[
(f[L] + Hold@@{
nIntegrate[ h[z] z / Sqrt[L^2 + z^2], {z, -L , L}]
}) /.vals /.nIntegrate->NIntegrate
,{R,{1000,2000}}]
]
,{L,10,100}]
If I use a block function to evaluate the value of the table for a
specific value of L (as Plot should do according to the
documentation), both versions works. But when I try to plot, the first
one works and this second one doesn't. Nor does the expression I was
aiming for (it should be next step):
Plot[
Evaluate[
Table[
(f[L] + Apply[
NIntegrate,
Hold@@{{
h[z] z / Sqrt[L^2 + z^2], {z, -L , L}
}}) /.vals
,{R,{1000,2000}}]
]
,{L,10,100}]
Giacomo
>
>
>
>
>
> >> Bobby
>
> >> On Tue, 03 May 2011 15:36:34 -0500, Giacomo <jackspa...@gmail.com> wro=
te:
>
> >>> On 03-May-11 13:41, DrMajorBob wrote:
> >>>> You answered your own question, since
>
> >>>> NIntegrate[ h[z] z / Sqrt[L^2 + z^2] /.vals, {z, -L /.vals, L/.va=
ls}]
>
> >>>> does the replacements before trying to integrate.
>
> >>> I know, but having to specify three time the same replacement rule in
> >>> the same expression doesn't look very elegant. :-)
>
> >>>> Or, you could properly define h[z_,a_,b_....] as a function of its
> >>>> arguments and parameters and L[a_,b_, ...] as a function of ITS
> >>>> arguments, rather than leaving most of them out.
>
> >>> Well, L is just a parameter by itself, whose numerical value is
> >>> specified in the set of replacement rules "vals" defined at the very
> >>> beginning of the notebook. h is indeed a function, but depends on may
> >>> "parameters" that are not really "variables". I don't see it
> >>> practical (nor clear from a logical point of view) to specify them as
> >>> variables...
>
> >>> Thanks anyway!
>
> >>> Giacomo
>
> >>>> It's generally a good idea to define functions with ALL their
> >>>> dependencies obvious in the definition. It leads to less confusion.
>
> >>>> Bobby
>
> >>>> On Tue, 03 May 2011 04:44:43 -0500, Giacomo Ciani
> >>>> <jackspa...@gmail.com> wrote:
>
> >>>>> Hi all,
>
> >>>>> I've been reading quite a bit in the Mathematica docs and in this
> >>>>> newsgroup, but didn't find (or didn't recognize...) an answer to my
> >>>>> problem.
>
> >>>>> I want to evaluate the following expression:
>
> >>>>> NIntegrate[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}]
>
> >>>>> where h[z] has a delayed value set previously in the notebook. Also=
, I
> >>>>> have previously defined a set of replacement rules in the form:
>
> >>>>> vals = {a->1, b->2, ec....}
>
> >>>>> to be used to specify the numerical values of the various parameter=
s
> >>>>> (including those present in the delayed value of h[z]).
>
> >>>>> As for now, the only (brute force) way I found to have my expressio=
n
> >>>>> correctly evaluated is to apply replacement rules separately to eac=
h
> >>>>> argument of NIntegrate (including integration limits):
>
> >>>>> NIntegrate[ h[z] z / Sqrt[L^2 + z^2] /.vals, {z, -L /.vals, L/.v=
als}]
>
> >>>>> I think you agree with me that this does not look very elegant.
> >>>>> Instead, I would like to be able to write something like this:
>
> >>>>> NIntegrate[h[z] z/Sqrt[L^2 + z^2], {z, -L, L}]/.vals
>
> >>>>> I know this can't work, as Mathematica tries to evaluate NIntegrate
> >>>>> and then apply the replacement rules... but how can I ask Mathemati=
ca
> >>>>> to apply all the replacement rules and delayed values to an express=
ion
> >>>>> without (or before) actually trying to evaluate it?
>
> >>>>> I found a lot of commands to hold the function from evaluating the
> >>>>> arguments, while I need pretty much the opposite...
>
> >>>>> Maybe there is something very simple I am overlooking...
>
> >>>>> Thanks
>
> >>>>> Giacomo
[toc] | [prev] | [standalone]
Back to top | Article view | comp.soft-sys.math.mathematica
csiph-web