Hello everyone. I hope you can help me solve this problem. I want to find if the functions fA1, fB2, fC3 are within the range LowerLimit <= hF1[fA1, fB2, fC3] <= UpperLimit and save the matching values into the list selectedData. The code must work for arbitrary functions.
Running the code shows the following error:
Set::setraw: Cannot assign to raw object 1.80609586246897`.
Set::setraw: Cannot assign to raw object 1.4410360862456661`.
Set::setraw: Cannot assign to raw object 1.0125069354448308`.
General::stop: Further output of Set::setraw will be suppressed during this calculation.
below the code: (* Step 1.- Construction of test functions *)
fA1[w1_,w2_,w3_,w4_]:= 10+w1+w2+w3+w4
fB2[r1_,r2_,r3_,r4_]:= 1+r1+r2+r3+r4
fC3[t1_]:=5*t1
(* Step 2.- Cconstruction of the rule where the functions will be evaluated*)
hF1[fA1_,fB2_,fC3_]:= (2*fA1)*(3*fB2)*(4*fC3)
(* Step 3.- Construction of the Main function *)
GT5[fA1_,fB2_,fC3_,x1_,x1min_,x1max_,x2_,x2min_,x2max_,x3_,x3min_,x3max_,x4_,x4min_,x4max_,NN_]:=
Module[
{
p1, p1min, p1max,
p2, p2min, p2max,
p3, p3min, p3max,
p4, p4min, p4max,
n, selectedData,LowerLimit,UpperLimit
},
LowerLimit = 1;
UpperLimit = 10000000;
selectedData={};
p1=x1; p1min=x1min; p1max=x1max;
p2=x2; p2min=x2min; p2max=x2max;
p3=x3; p3min=x3min; p3max=x3max;
p4=x4; p4min=x4min; p4max=x4max;
n=1;
Do[
{
x1 = RandomReal[{p1min, p1max}];
x2 = RandomReal[{p2min, p2max}];
x3 = RandomReal[{p3min, p3max}];
x4 = RandomReal[{p4min, p4max}];
If[
LowerLimit <= hF1[fA1, fB2, fC3] <= UpperLimit,
selectedData = AppendTo[selectedData, {p1,p2,p3,p4,hF1[fA1, fB2, fC3]}],
0
];
}, {n, NN}
];
selectedData
]
Now, run the instruction:
GT5[
fA1[D1+D2,D3,D1,D2],
fB2[D1+D2,D3,D1,D2],
fC3[2*D1],
D1,1,2,
D2,1,2,
D3,1,2,
D4,1,2,
10
]
and I get:
During evaluation of In[18]:= Set::setraw: Cannot assign to raw object 1.80609586246897`.
During evaluation of In[18]:= Set::setraw: Cannot assign to raw object 1.4410360862456661`.
During evaluation of In[18]:= Set::setraw: Cannot assign to raw object 1.0125069354448308`.
During evaluation of In[18]:= General::stop: Further output of Set::setraw will be suppressed during this calculation.
Out[18]= {{1.8061, 1.44104, 1.01251, 1.13217, 64553.9}, {1.8061,
1.44104, 1.01251, 1.13217, 64553.9}, {1.8061, 1.44104, 1.01251,
1.13217, 64553.9}, {1.8061, 1.44104, 1.01251, 1.13217,
64553.9}, {1.8061, 1.44104, 1.01251, 1.13217, 64553.9}, {1.8061,
1.44104, 1.01251, 1.13217, 64553.9}, {1.8061, 1.44104, 1.01251,
1.13217, 64553.9}, {1.8061, 1.44104, 1.01251, 1.13217,
64553.9}, {1.8061, 1.44104, 1.01251, 1.13217, 64553.9}, {1.8061,
1.44104, 1.01251, 1.13217, 64553.9}}
I will appreciate your help or comments.
Precisely, I want to use functions whose inputs will not always be like this fA1[x1 + x2, x3, x1, x2], fB2[x1 + x2, x3, x1, x2], fC3[2*x1]
I've been able to get it to work by removing Module and adding Clear[x1,x2,x3,x4] to the end, but I think there must be a better way to solve it.
GT5[fA1_,fB2_,fC3_,x1_,x1min_,x1max_,x2_,x2min_,x2max_,x3_,x3min_,x3max_,x4_,x4min_,x4max_,NN_]:=
{
selectedData = {};
LowerLimit = 1;
UpperLimit = 10000000;
selectedData = {};
Do[
{
x1 = RandomReal[{x1min, x1max}];
x2 = RandomReal[{x2min, x2max}];
x3 = RandomReal[{x3min, x3max}];
x4 = RandomReal[{x4min, x4max}];
If[
LowerLimit <= hF1[fA1, fB2, fC3] <= UpperLimit,
selectedData = AppendTo[selectedData, {x1,x2,x3,x4,hF1[fA1, fB2, fC3]}],
0]
}, {n, NN}
];
Print[selectedData];
Clear[x1,x2,x3,x4];
}
Attachments: