0
$\begingroup$

I have the following code

fc[x_] := a + b*x^c + d*x^e

listNumber = {1,2,3,4,5}

list = {a,b, c,.. , e}

 answF = NMinimize[{Abs[fitFl], 
   fc[listNumber[[1]] ] <= 1 &&  fc[listNumber[[2]] ]  <= 1 && fc[listNumber[[3]] ] <= 1
   && fc[listNumber[[4]] ] <= 1 && fc[listNumber[[5]] ] <= 1,
   list[[1]] > 5, list [[2]] >5} , {a, b, c, d,  e, qB, qT}]

I want to apply a constraint over every element in a list. I've hard coded above , how could I thread across the whole list. Similarly for the function, how could I thread across the whole list (listNumber).

Hard coding works for a small list, but if the size of the list increases, this becomes an issue.

$\endgroup$
  • 1
    $\begingroup$ Why not And @@ Thread[fc[listNumber] <= 1]? $\endgroup$ – J. M. will be back soon Nov 14 '17 at 11:22
  • $\begingroup$ J.M. I could not get that to work for the function, I suspect I'm missing some code. My "list" array is larger. Do you have any method for a list with no function. thks $\endgroup$ – SPIL Nov 14 '17 at 15:14
1
$\begingroup$

The code provided has undefined functions and constants. However, to generate a list of constraints could be done as:

fc[x_] := a + b*x^c + d*x^e;
listNumber = {1, 2, 3, 4, 5};
list = {a, b, c, d,e};
answF = NMinimize[Flatten@{Abs[fitFl], (fc[#] <= 1) & /@ listNumber, (# > 5) & /@list}, {a, b, c, d, e, qB, qT}]
$\endgroup$
  • $\begingroup$ Jose, thanks that seems to work. Could I add a single constraint such as fc[6] in between the commas like this: NMinimize[Flatten@{Abs[fitFl], fc[6] ==1, (fc[#] <= 1) .......etc $\endgroup$ – SPIL Nov 15 '17 at 11:29
  • $\begingroup$ Yes, as the new contraint does not fulfill the others. $\endgroup$ – José Antonio Díaz Navas Nov 15 '17 at 11:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.