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.
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}
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 ???
fkampas
(Frank Kampas)
August 8, 2015, 2:13pm
4
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}
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.
scinar
(Okkes Dulgerci)
August 9, 2015, 12:04am
6
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}}
anmolb
(Anmol Bajracharya)
August 9, 2015, 2:24am
7
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}
Thanks you guys with a problem we have many ways to approach. I’m so happy.
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.
sukrause
(Udo Krause)
August 9, 2015, 5:05pm
10
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}
sukrause
(Udo Krause)
August 11, 2015, 5:10am
11
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}