0
|
6334 Views
|
2 Replies
|
0 Total Likes
View groups...
Share
GROUPS:

# How to code FOR within Interval correctly?

Posted 10 years ago
 Help me please to write For correctly.. I've the 1-st step of algorithm and finally it outputs two intervals : 1)Interval[{1.25677,3.25203},{5.511,5.58272}] My problem is in the 2-nd step, because I've to do some conditions within previous first interval {1.25677,3.25203} and output a new interval. So For has to work from beginning of this interval to the end. X = {-2, 6} spkw = {105.056, 89.2249, 17.7361, 7.25929, 7.25929, 7.25929, 7.25929, 1.09386, 1.09386, -7.35382, -12.5073, -11.929, -11.929, -15.429, -8.63312, -6.34314, -14.3807, -16.7907, -18.933, -12.3896, -3.021, -22.0262, -25.7865, -18.8033, -9.07591, -9.18036, -8.49959, -9.24378, -7.32337, -0.271835, -0.270096, 0.123206, 0.156523, 0.465142, 4.12922, 4.23318, 8.03654, 8.20981, 12.1518, 12.3944} spkn = {73.5426, 66.8007, 24.6942, 16.4029, 0.726929, 0.314512, -1.23002, -1.23002, -3.90668, -10.8276, -14.2065, -13.0895, -18.656, -20.1709, -8.79676, -8.79676, -11.2319, -13.9771, -15.1407, -2.50312, -4.72374, -32.4496, -34.2958, -21.0455, -2.45882, -2.45882, -2.45882, -2.45882, -2.45882, -2.45882, -2.45882, -1.70357, -1.70357, -1.11799, 6.1251, 6.36752, 6.36752, 6.60995, 14.0955, 14.5803} spmw = {243.475, 213.305, 83.8004, 67.1081, 67.1081, 67.1081, 67.1081, 59.4226, 59.4226, 49.9772, 45.1635, 45.6272, 45.6272, 43.4397, 46.8376, 47.4101, 46.6214, 47.3535, 48.75, 42.5447, 33.1761, 61.6839, 68.2644, 53.4787, 29.1603, 29.4279, 27.6409, 30.1061, 22.9045, -5.30161, -5.30859, -6.88938, -7.0313, -8.37913, -24.8675, -25.3613, -44.3781, -45.2877, -66.9686, -68.3634} spmn = {180.448, 167.6, 91.3225, 78.1123, 56.5579, 55.9978, 53.9271, 53.9271, 50.6364, 42.898, 39.742, 40.6374, 36.4626, 35.5158, 41.2028, 41.2028, 40.9639, 41.7978, 42.5563, 30.5716, 32.7923, 74.3811, 77.6119, 49.5569, 3.09017, 3.09017, 3.09017, 3.09017, 3.09017, 3.09017, 3.09017, 0.0546329, 0.0546329, -2.5028, -35.0967, -36.2482, -36.2482, -37.5209, -78.6912, -81.4791} spfw = {33.3632, 43.263, 51.6709, 55.5421, 57.1266, 57.2511, 57.3756, 58.059, 58.0778, 58.1995, 56.846, 55.1903, 54.5739, 53.0828, 51.1542, 48.9959, 48.0325, 42.2533, 36.408, 30.7952, 30.1551, 28.6446, 23.138, 19.4168, 6.47053, 5.90328, 5.32951, -0.513959, -0.750527, -6.38895, -6.39157, -6.39418, -6.36456, -6.09357, -6.28599, -5.25369, -4.19539, -2.18625, -0.133803, 2.90414, 6.171} spfn = {33.3632, 40.2933, 46.5882, 51.9781, 55.5583, 55.5708, 55.5762, 55.4604, 55.4393, 55.0045, 530116, 51.1309, 50.4546, 48.1226, 45.6012, 43.402, 42.066, 37.5522, 32.6864, 28.1979, 28.0685, 25.7067, 17.5943, 13.5547, -2.97428, -3.21054, -3.36422, -5.05466, -5.1301, -6.4392, -6.76879, -6.48231, -7.20196, -7.00719, -7.53373, -6.00246, -4.41058, -2.8187, -1.16621, 2.35765, 6.04694} (*1 STEP*) myintervals1 = Reap[For[i = 1, i < Length@spfn, i++, If[! (((0 < spfn[[i]]) && (0 < spfn[[i + 1]])) || ((spfw[[i]] < 0) && (spfw[[i + 1]] < 0))), Sow[Interval[{-spmn[[i]]/spkn[[i]], -spmw[[i]]/spkw[[i]]}]]]]] [[ 2, 1]] Print["1)", myint1 = Join @@ myintervals1] myint11 = myint1[[1]]; myint12 = myint1[[2]]; Print["width of interval ", myint11, " : ", widthmyint11 = Max[myint1[[1]]] - Min[myint1[[1]]]] Print["width of interval ", myint12 , " : ", widthmyint12 = Max[myint1[[2]]] - Min[myint1[[2]]]] (*2 STEP*) myintervals2 = Reap[For[j = Min[myint1[[1]]], j <= Max[myint1[[1]]], j++ If[spfn[[j]]*spfn[[j + 1]] < 0 && spfw[[j]]*spfw[[j + 1]] < 0, Sow[ Interval[{Min[-spmn[[j]]/spkn[[j]], -spmw[[j]]/spkw[[j]]], Max[-spmn[[j]]/spkn[[j]], -spmw[[j]]/spkw[[j]]]}]] ] ] ][[2, 1]] Print["2)", myintervals2] 
2 Replies
Sort By:
Posted 10 years ago
 Firstly the (2 STEP) misses a commabut that's not the only problem.If running the second step, have a close look to the iteration variable j which is used as index too Out[10]= {Interval[{2.35475, 2.84411}], Interval[{1.25677, 3.21293}], Interval[{1.25677, 3.20553}], Interval[{1.25677, 3.25203}], Interval[{5.511, 5.58272}]} During evaluation of In[10]:= 1)Interval[{1.25677,3.25203},{5.511,5.58272}] During evaluation of In[10]:= width of interval {1.25677,3.25203} : 1.99526 During evaluation of In[10]:= width of interval {5.511,5.58272} : 0.0717154 In[17]:= (*2 STEP*)myintervals2 = Reap[For[j = Min[myint1[[1]]], j <= Max[myint1[[1]]], j++ , If[spfn[[j]]*spfn[[j + 1]] < 0 && spfw[[j]]*spfw[[j + 1]] < 0, Sow[Interval[{Min[-spmn[[j]]/spkn[[j]], -spmw[[j]]/spkw[[j]]], Max[-spmn[[j]]/spkn[[j]], -spmw[[j]]/spkw[[j]]]}]]]]][[2, 1]] During evaluation of In[17]:= Part::pkspec1: The expression 1.2567695073246514 cannot be used as a part specification. >> During evaluation of In[17]:= Part::pkspec1: The expression 2.2567695073246514 cannot be used as a part specification. >> During evaluation of In[17]:= Part::pkspec1: The expression 1.2567695073246514 cannot be used as a part specification. >> During evaluation of In[17]:= General::stop: Further output of Part::pkspec1 will be suppressed during this calculation. >> During evaluation of In[17]:= Part::partw: Part 1 of {} does not exist. >> Out[17]= {Null, {}}[[2, 1]] Print["2)", myintervals2] In[18]:= Min[myint1[[1]]] Out[18]= 1.25677 In[19]:= Max[myint1[[1]]] Out[19]= 3.25203 Do you see it? j runs from real 1.25677 to real 3.25203 and is used as index to expressions. But expressions in Mathematica do not have real indices, see Part. Interestingly enough they do not state that Part takes only integers or lists of integers but they seem to understand that by using symbols like i, j`, ... usually meaning integers in mathematics and physics.See Round, Floor, and Ceiling to make reals into integers.
Posted 10 years ago
 Thank You!
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.