# Basic question about loops in mathematica

I am very new to Mathematica programming and am trying to find the optimum of a numerical integral for various values of a parameter. Here is the basic code for one parameter value that runs fine - I have defined a list of values for the parameter, p2g:

cl = 0;
ch = 1;
el = -.1;
eh = .1;
F[e_] = Max[0, Min[(e - el)/(eh - el), 1]];
d = .1;
pi = .2;
p2g = .05 Array[# &, 6];
p1sg = ConstantArray[0, 6];
i = 1;
Obj1[p_?NumericQ] :=
NIntegrate[(c + d - p) (1 - F[(1 - pi) p2g[[i]] + pi c - p]), {c,
cl, p2g[[i]]}] +
NIntegrate[(c + d - p) (1 - F[c - p]), {c, p2g[[i]], ch}];
p1s = p /. Last[NMaximize[{Obj1[p]}, {p}]];
p1sg[[i]] = p1s;


My naive way of doing this was to put the whole thing in a for loop and change $i$. Once I do this, the code returns an error. basically it cannot assign the value in NMaximize. Does anyone know how to deal with this.

Thanks.

• See the preferred function: Table. Also, your code simply cannot run because it has several syntax errors, for instance missing semicolons at the end of every line. Also, please show all necessary code, such as your definitions of F, p2g, etc. – David G. Stork Jan 27 '16 at 19:33
• When using NIntegrate, one must assign numerical values to all quantities. Here, pi and p2g are undefined. This needs to be corrected. – bbgodfrey Jan 27 '16 at 19:34
• Oh sorry forgot to mention that pi and p2g are defined before – Ali Jan 27 '16 at 19:35
• Please edit your question to provide a complete code that can be copied and executed by readers. – bbgodfrey Jan 27 '16 at 19:36
• Welcome to Mathematica.SE! 1) As you receive help, try to give it too, by answering questions in your area of expertise. 2) Take the tour and check the faqs! 3) When you see good questions and answers, vote them up by clicking the gray triangles, because the credibility of the system is based on the reputation gained by users sharing their knowledge. Also, please remember to accept the answer, if any, that solves your problem, by clicking the checkmark sign! – user9660 Jan 27 '16 at 19:39

The following minimal change to the code produces results.

l = 0;
ch = 1;
el = -.1;
eh = .1;
F[e_] = Max[0, Min[(e - el)/(eh - el), 1]];
d = .1;
pi = .2;
p2g = .05 Array[# &, 6];
p1sg = ConstantArray[0, 6];
Do[
Obj1[p_?NumericQ] :=
NIntegrate[(c + d - p) (1 - F[(1 - pi) p2g[[i]] + pi c - p]), {c,
cl, p2g[[i]]}] +
NIntegrate[(c + d - p) (1 - F[c - p]), {c, p2g[[i]], ch}];
p1s = p /. Last[NMaximize[{Obj1[p]}, {p}]];
p1sg[[i]] = p1s, {i, 6}];

p1sg
(* {0.104881, 0.118322, 0.13784, 0.161245, 0.187083, -1.23481} *)


Table, as suggested above by David G. Stork, also works.

Obj2[i_, p_?NumericQ] :=
NIntegrate[(c + d - p) (1 - F[(1 - pi) p2g[[i]] + pi c - p]), {c,
cl, p2g[[i]]}] +
NIntegrate[(c + d - p) (1 - F[c - p]), {c, p2g[[i]], ch}];
Table[p /. Last[NMaximize[{Obj2[i, p]}, {p}]], {i, 6}]


producing the same results.

• Great thanks a lot @bbgodfrey. Still puzzling to me why 'for' didn't work. I tried 'While' and that had the same issue. Also how would you do this in Table? Sorry I am pretty new to mathematica - mainly a matlab coder. – Ali Jan 27 '16 at 19:51
• Fantastic @bbgodfrey. Thanks very much – Ali Jan 27 '16 at 19:55