Message Boards Message Boards

0
|
9843 Views
|
10 Replies
|
4 Total Likes
View groups...
Share
Share this post:

How to use the Replace Command

Posted 10 years ago

Hi everyone, I want to calculate an integral that applies to a list of value. The question is how can I replace the value in the list and get the result. For example,

Integrate[a x^2, {x,1, c}]
a={ 1, 2, 3, 4, 5, 6}
c={ 6, 5, 4, 3, 2, 1}

respectively in the order of the values in the list. Thank you very much.

POSTED BY: ANH MONG
10 Replies
Posted 10 years ago

Study Map and Transpose and especially Function in the help pages until you can understand how this works.

In[1]:=  a = {1, 2, 3, 4, 5, 6};c = {6, 5, 4, 3, 2, 1};
    Map[Integrate[First[#] x^2, {x, 1, Last[#]}] &, Transpose[{a, c}]]

Out[3]= {215/3, 248/3, 63, 104/3, 35/3, 0}
POSTED BY: Bill Simpson

As much as I like the functional programming approach, sometimes other methods are simpler:

In[4]:= Table[Integrate[a[[i]] x^2, {x, 1, c[[i]]}], {i, Length[a]}]

Out[4]= {215/3, 248/3, 63, 104/3, 35/3, 0}
POSTED BY: Frank Kampas
Posted 10 years ago

It often takes some time and concentration to understand that way of thinking.

Here is one alternative

f[{ai_,ci_}]:=Integrate[ai x^2, {x,1, ci}];
Map[f,Transpose[{a, c}]]

The shorthand of # and & is accomplishing the same thing as defining some function f.

POSTED BY: Bill Simpson
Posted 10 years ago

How about this

Table[Integrate[a[[i]] x^2, {x, 1, c[[j]]}], {i, Length@a}, {j, Length@c}]

  {{215/3, 124/3, 21, 26/3, 7/3, 0}, {430/3, 248/3, 42, 52/3, 14/3, 0}, {215, 124, 63, 26, 7, 0}, 
{860/3, 496/3, 84, 104/3, 28/3, 0}, {1075/3, 620/3, 105, 130/3, 35/3, 0}, {430, 248, 126, 52, 14, 0}}
POSTED BY: Okkes Dulgerci
Posted 10 years ago

Thanks I get it. But in the First and Last functions using # and &. Could you explain to me how to use # and & correctly. # is a variable space holder. So in Map[ f, exp], it determines the # here is a variable in f ?????

POSTED BY: ANH MONG

Consider following simple example:

In[1]:= MapThread[f[#1, #2] &, {{a, b, c}, {1, 2, 3}}]

Out[1]= {f[a, 1], f[b, 2], f[c, 3]}

Using above idea we can construct

In[2]:= MapThread[
              Integrate[#1 x^2, {x, 1, #2}] &, 
              {
                 {1, 2, 3, 4, 5, 6},
                 {6, 5, 4, 3, 2, 1}
              }
        ]

Out[2]= {215/3, 248/3, 63, 104/3, 35/3, 0}
Posted 10 years ago

Thanks you guys with a problem we have many ways to approach. I'm so happy.

POSTED BY: ANH MONG
Posted 10 years ago

No. It's not the problem statement question, you just should take out the value of a and c simultaneously with the same i-th value.

POSTED BY: ANH MONG

sometimes other methods are simpler:

But this is sooo unfunny; to bring the fun back into the house, consider

In[9]:= Inner[Integrate[#1 x^2, {x, 1, #2}] &, Range[6], Reverse[Range[6]], List]
Out[9]= {215/3, 248/3, 63, 104/3, 35/3, 0}
POSTED BY: Udo Krause

factor the factor (list a) and type

In[1]:= Range[6] (Integrate[x^2, {x, 1, #}] & /@ Reverse[Range[6]])
Out[1]= {215/3, 248/3, 63, 104/3, 35/3, 0}
POSTED BY: Udo Krause
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract