'Custom' CountryData Entries in Mathematica - wolfram-mathematica

I have the following dataset – which may not be formatted correctly for this task – which refers to values that I want to indicate on a map, using the map as a sort of graph:
Dat2Countries = {{"Argentina", 32.63969016}, {"Australia",
65.6986192}, {"Bolivia", 13.19444444}, {"Brazil",
27.55511788}, {"Canada", 66.49547068}, {"Chile",
27.14539357}, {"China", 17.94765891}, {"Czech Republic",
58.45414706}, {"Egypt", 18.07371757}, {"El Salvador",
23.94366197}, {"France", 63.86621123}, {"Germany",
58.68321579}, {"Ghana", 21.39232133}, {"Greece",
57.06734961}, {"Indonesia", 17.46457699}, {"Israel",
90.84112638}, {"Italy", 57.48221752}, {"Japan",
54.9704008}, {"Jordan", 22.93290207}, {"Kenya",
23.60066645}, {"Lebanon", 19.74186819}, {"Malaysia",
16.5620438}, {"Mexico", 31.72258783}, {"Nigeria",
24.38177507}, {"Pakistan" , 20.4777854}, {"Poland",
60.54096673}, {"Russia", 28.77358356}, {"Senegal",
21.91780822}, {"South Africa", 19.03936205}, {"South Korea",
53.20728589}, {"Spain", 58.12532901}, {"Tunisia",
24.67799831}, {"Turkey", 45.59516911}, {"Uganda",
19.08127926}, {"United Kingdom", 66.5450176}, {"Venezuela",
14.66666667}}
And I want to 'overlay' this index on a map using something like this (sorry, I know this is far from elegant):
Graphics[{If[MemberQ[Map[First, Dat2Countries, {1}], #],
If[Map[Last, Dat2Countries, {1}] < 20,
RGBColor[0.0352941, 0.254902, 0.352941],
If[Map[Last, Dat2Countries, {1}] < 40 &&
Map[Last, Dat2Countries, {1}] >= 20,
RGBColor[0.305882, 0.498039, 0.592157],
If[Map[Last, Dat2Countries, {1}] < 60 &&
Map[Last, Dat2Countries, {1}] >= 40,
RGBColor[0.0705882, 0.568627, 0.835294],
If[Map[Last, Dat2Countries, {1}] < 80 &&
Map[Last, Dat2Countries, {1}] >= 60,
RGBColor[0.372549, 0.776471, 0.952941],
If[Map[Last, Dat2Countries, {1}] < 100 &&
Map[Last, Dat2Countries, {1}] >= 80,
RGBColor[0.368627, 0.694118, 0.701961], LightBrown]]]]]],
CountryData[#, "SchematicPolygon"]} & /# CountryData[]]
But I am not so familiar with the CountryData package – is it possible to use the package to indicate values on a map that aren't built into the package?
Thanks,
arebearit

Using your data list Dat2Countries as given, the map can be drawn like so:-
colourfunction[country_String] := Module[{cases, value},
Catch[
cases = Cases[Dat2Countries, {country, _}];
If[cases == {}, Throw[LightGray]];
value = cases[[1, 2]];
Which[
value < 20, RGBColor[0.0352941, 0.254902, 0.352941],
value < 40, RGBColor[0.305882, 0.498039, 0.592157],
value < 60, RGBColor[0.0705882, 0.568627, 0.835294],
value < 80, RGBColor[0.372549, 0.776471, 0.952941],
value < 100, RGBColor[0.368627, 0.694118, 0.701961],
True, LightBrown]]]
Graphics[{colourfunction[CountryData[#, "Name"]],
CountryData[#, "SchematicPolygon"]} & /# CountryData[]]

Related

Mathematica integration does not return value

If I integrate the following, I get answer:
Integrate[(nor*x^ap*(1 - x)^bp*(1 - cp*x)*Exp[-t*al]), {x, 0.001, 1},
Assumptions -> {-1 < nor < 1, 0 < ap < 3, 0 < bp < 5, 0 < cp < 5,
0 < t < 2, 0 < al < 1}]
and the answer is
E^(-al t) nor ((1. Gamma[1 + ap] Gamma[1 + bp])/Gamma[2 + ap + bp] - (
1. cp Gamma[2 + ap] Gamma[1 + bp])/Gamma[3 + ap + bp] +
0.001^ap (-((0.001 Hypergeometric2F1[1 + ap, -bp, 2 + ap, 0.001])/(
1. + ap)) + (
1.*10^-6 cp Hypergeometric2F1[2 + ap, -bp, 3 + ap, 0.001])/(
2. + ap)))
But if I want to do this,
Integrate[(nor*x^ap*(1 - x)^bp*(1 - cp*x)*Exp[-t*al*(1 - x)]), {x,
0.001, 1},
Assumptions -> {-1 < nor < 1, 0 < ap < 3, 0 < bp < 5, 0 < cp < 5,
0 < t < 2, 0.5 < al < 1}]
it doesn't give an final expression. It gives:
Integrate[(nor*x^ap*(1 - x)^bp*(1 - cp*x)*Exp[-t*al*(1 - x)]), {x,
0.001, 1},
Assumptions -> {-1 < nor < 1, 0 < ap < 3, 0 < bp < 5, 0 < cp < 5,
0 < t < 2, 0.5 < al < 1}]
which is the same as the integral.
Could you please help me figuring out how to get answer for the second integral with additional (1-x) in the exponential?

How to fix Errors with lists Length?

Help me please!
On the 3-rd step I got such errors as
Part 41 of ...... does not exist.
though on the previous steps it worked and returned results.
I've got lists of 40 elements in spkn,spkw,spmn,spmw and 41 in spx,spfn,spfw.
Code:
spx = {-2, -1.90577, -1.81153, -1.59327, -1.375, -1.35785, -1.3407, -1.24655, -1.22941, -1.11811, -0.934054, -0.80167, -0.75, -0.625,-0.5, -0.25, -0.0981238, 0.303752, 0.651876, 0.94833, 1, 1.5, 1.75,2.11731, 2.5, 2.5625, 2.625, 3.3125, 3.75, 4, 4.00964, 4.01928,4.25964, 4.36731, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 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}
spfn[[i]] = spkn[[i]]*spx[[i]] + spmn[[i]];
spfw[[i]] = spkw[[i]]*spx[[i]] + spmw[[i]];
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-st step:
For[i = 1, i < Length#spfn, i++,
If[spfn[[i]]*spfn[[i + 1]] < 0 && spfw[[i]]*spfw[[i + 1]] < 0,
Print["1) exist roots: ", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
xwz[i] = -spmw[[i]]/spkw[[i]]] ;
Break[]
]
]
2-nd step:
For[i = 1, i < Length#spfn, i++,
If[(0 < spfn[[i]]) && (spfn[[i + 1]] < 0) && (0 < spfw[[i]]) && (0 <
spfw[[i + 1]]),
Print["2) exist roots:", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
spx[[i + 1]]] ;
Break[]
]
]
3-rd step(DOESN'T WORK):
For[i = 1, i < Length#spfn, i++,
If[(spfn[[i]] < 0) && (0 < spfn[[i + 1]]) && (0 < spfw[[i]]) && (0 <
spfw[[i + 1]]),
Print["3) exist roots:", xnz[i] = -spmn[[i]]/spkn[[i]], ", ",
spx[[i]]];
Break[]
]
]
THE RESULTS are:
1) exist roots: 5.58272, 5.511
2) exist roots:2.35475, 2.5
and errors:
Part::partw: Part 41 of {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,<<8>>,-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} does not exist. >>
Part::partw: Part 41 of {-2,-1.90577,-1.81153,-1.59327,-1.375,-1.35785,-1.3407,-1.24655,-1.22941,-1.11811,-0.934054,-0.80167,-0.75,-0.625,-0.5,-0.25,-0.0981238,0.303752,0.651876,0.94833,1,1.5,1.75,2.11731,2.5,2.5625,2.625,3.3125,3.75,4,4.00964,4.01928,4.25964,4.36731,4.5,4.75,5,5.25,5.5,5.75,6} does not exist. >>
and some more similar..
If you take the following out of your 'Code' section the rest executes without error messages.
spfn[[i]] = spkn[[i]]*spx[[i]] + spmn[[i]];
spfw[[i]] = spkw[[i]]*spx[[i]] + spmw[[i]];

How to unite intervals?

Help me please to unite resulted intervals!
The line "For..." outputs the intervals where the roots exist (roots: 2.94 and 5,52).
I have to consider a remark:
If in the intervals {x*[i],x[i+1]} and {x[i+1],x**[i+1]} can be the roots of the equation, the range {x*[i],x**[i+1]} must have at least one of its root.
X = {-2, 6}
spx = {-2, -1.90577, -1.81153, -1.59327, -1.375, -1.35785, -1.3407, -1.24655, -1.22941, -1.11811, -0.934054, -0.80167, -0.75, -0.625,-0.5, -0.25, -0.0981238, 0.303752, 0.651876, 0.94833, 1, 1.5, 1.75,2.11731, 2.5, 2.5625, 2.625, 3.3125, 3.75, 4, 4.00964, 4.01928,4.25964, 4.36731, 4.5, 4.75, 5, 5.25, 5.5, 5.75, 6}
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}
For[i = 1, i < Length#spfn, i++,
If[! (((0 < spfn[[i]]) && (0 < spfn[[i + 1]])) ||
((spfw[[i]] < 0) && (spfw[[i + 1]] < 0))),
Print["1) exists root on: {", spx[[i]], ";", spx[[i + 1]], "}"]]]
So the result include 5 intervals:
1) exists root on: {2.11731;2.5}
1) exists root on: {2.5;2.5625}
1) exists root on: {2.5625;2.625}
1) exists root on: {2.625;3.3125}
1) exists root on: {5.5;5.75}
And as the first root is 2.94 it has to enter into 4 first intervals, and 5.52 in the last. So after considering the remark, that line should outputs two intervals.
I tried to use IntervalUnion but it doesn't work:(
Please help me to code this remark in this line.
Revised answer. See this link for explanation of ## &[].
spxpairs = Interval /# Partition[spx, 2, 1];
spfwpairs = Interval /# Partition[spfw, 2, 1];
spfnpairs = Interval /# Partition[spfn, 2, 1];
ans = Apply[IntervalUnion,
If[Not[0 < Min##1 || Max##2 < 0], #3, ## &[]] &
### Transpose[{spfnpairs, spfwpairs, spxpairs}]];
Column[Prepend[List ## ans, "Roots exist on :"], Spacings -> 1]
Roots exist on :
{2.11731, 3.3125}
{5.5, 5.75}
yes there is a more succinct way.. once you have your list of intervals (as in Chris' answer ) you just do:
IntervalUnion ## intervals
(* Interval[{2.11731, 3.3125}, {5.5, 5.75}] *)

Problems with Mathematica Plotting using Piecewise

I am trying to plot using piecewise in one of my problems and I have two variables: x and psi. However, the respective functions are only valid for a defined range of "x" and the psi range is the same. I am trying to make a 3D plot of these -- and I basically just have Plot3D[p,{x,0,1},{psi,0.01,1}] ---> These ranges are for the entire plot range and my x range for the respective functions is already defined in the Piecewise function.
I get the following error: saying that Plot::exclul: ...... must be a list of equalities or \ real-valued functions.
Can anyone please help me with this. I am trying to follow the same procedure as: How can I use Piecewise[] with a variable number of graphs/intervals
But, I don't know what to do about the plotting part.
Thanks.
The following is my code:
j = 10;
s = 0; r = 0;
K[x_, psi_] :=
Sum[Sin[n*Pi*x]*
Sin[n*Pi*
psi]*(2*Exp[-(n*Pi)^2*
Abs[s + r]] - (Exp[-(n*Pi)^2*Abs[s - r]] -
Exp[-(n*Pi)^2*(s + r)])/(n*Pi)^2 ), {n, 1, j}];
TL[x_, psi_] = Integrate[K[x - y, psi]*y, {y, -10, 10}];
TU[x_, psi_] = Integrate[K[x - y, psi]*(1 - y), {y, -10, 10}];
eq = {TL[x, psi], TU[x, psi]};
cond = {{0 <= x <= 0.5, 0.01 <= psi <= 1}, {0.5 < x <= 1,
0.01 <= psi <= 1}};
p = Piecewise[{eq, cond}];
Plot3D[p, {x, 0, 1}, {psi, 0.01, 1}]
Here is a working version:
time = AbsoluteTime[];
j = 10; s = 0; r = 0;
K[x_, psi_] :=
Sum[Sin[n*Pi*x]*Sin[n*Pi*psi]*
(2*Exp[-(n*Pi)^2*Abs[s + r]] -
(Exp[-(n*Pi)^2*Abs[s - r]] -
Exp[-(n*Pi)^2*(s + r)])/(n*Pi)^2), {n, 1, j}];
TL[x_, psi_] := Integrate[K[x - y, psi]*y, {y, -10, 10}];
TU[x_, psi_] := Integrate[K[x - y, psi]*(1 - y), {y, -10, 10}];
Plot3D[Piecewise[
{{TL[x, psi], 0 <= x <= 0.5}, {TU[x, psi], 0.5 < x <= 1}}],
{x, 0, 1}, {psi, 0.01, 1}]
ToString[Round[AbsoluteTime[] - time]] <> " seconds"

Mathematica: Thread::tdlen: Objects of unequal length in {Null} {} cannot be combined. >>

I have aproblem:
Thread::tdlen: Objects of unequal length in {Null} {} cannot be combined. >>
It seems to occur in the while test which makes no sense at all since I am onlu comparing numbers...?
The program is a program to solve the 0-1 knapsack dynamic programming problem though I use loops, not recursion.
I have put some printouts and i can only think that the problem is in the while loop and it doesnt make sense.
(* 0-1 Knapsack problem
item = {value, weight}
Constraint is maxweight. Objective is to max value.
Input on the form:
Matrix[{value,weight},
{value,weight},
...
]
*)
lookup[x_, y_, m_] := m[[x, y]];
generateTable[items_, maxweight_] := {
nbrofitems = Dimensions[items][[1]];
keep = values = Table[0, {j, 0, nbrofitems}, {i, 1, maxweight}];
For[j = 2, j <= nbrofitems + 1, j++,
itemweight = items[[j - 1, 2]];
itemvalue = items[[j - 1, 1]];
For[i = 1, i <= maxweight, i++,
{
x = lookup[j - 1, i, values];
diff = i - itemweight;
If[diff > 0, y = lookup[j - 1, diff, values], y = 0];
If[itemweight <= i ,
{If[x < itemvalue + y,
{values[[j, i]] = itemvalue + y; keep[[j, i]] = 1;},
{values[[j, i]] = x; keep[[j, i]] = 0;}]
},
y(*y eller x?*)]
}
]
];
{values, keep}
}
pickItems[keep_, items_, maxweight_] :=
{
(*w=remaining weight in knapsack*)
(*i=current item*)
w = maxweight;
knapsack = {};
nbrofitems = Dimensions[items][[1]];
i = nbrofitems + 1;
x = 0;
While[i > 0 && x < 10,
{
Print["lopp round starting"];
x++;
Print["i"];
Print[i];
Print["w"];
Print[w];
Print["keep[i,w]"];
Print[keep[[i, w]]];
If[keep[[i, w]] == 1,
{Append[knapsack, i];
Print["tjolahej"];
w -= items[[i - 1, 2]];
i -= 1;
Print["tjolahopp"];
},
i -= 1;
];
Print[i];
Print["loop round done"];
}
knapsack;
]
}
Clear[keep, v, a, b, c]
maxweight = 5;
nbrofitems = 3;
a = {5, 3};
b = {3, 2};
c = {4, 1};
items = {a, b, c};
MatrixForm[items]
results = generateTable[items, 5];
keep = results[[1]][[2]];
Print["keep:"];
MatrixForm[keep]
Print["------"];
results2 = pickItems[keep, items, 5];
MatrixForm[results2]
This is not really an answer to the specific question being asked, but some hints on general situations when this error occurs. The short answer is that this is a sign of passing lists of unequal lengths to some Listable function, user-defined or built-in.
Many of Mathematica's built-in functions are Listable(have Listable attribute). This basically means that, given lists in place of some or all arguments, Mathematica automatically threads the function over them. What really happens is that Thread is called internally (or, at least, so it appears). This can be illustrated by
In[15]:=
ClearAll[f];
SetAttributes[f,Listable];
f[{1,2},{3,4,5}]
During evaluation of In[15]:= Thread::tdlen: Objects of unequal length in
f[{1,2},{3,4,5}] cannot be combined. >>
Out[17]= f[{1,2},{3,4,5}]
You can get the same behavior by using Thread explicitly:
In[19]:=
ClearAll[ff];
Thread[ff[{1,2},{3,4,5}]]
During evaluation of In[19]:= Thread::tdlen: Objects of unequal length in
ff[{1,2},{3,4,5}] cannot be combined. >>
Out[20]= ff[{1,2},{3,4,5}]
In case of Listable functions, this is a bit more hidden though. Some typical examples would include things like {1, 2} + {3, 4, 5} or {1, 2}^{3, 4, 5} etc. I discussed this issue in a bit more detail here.
Try this version:
pickItems[keep_, items_, maxweight_] := Module[{},
{(*w=remaining weight in knapsack*)(*i=current item*)w = maxweight;
knapsack = {};
nbrofitems = Dimensions[items][[1]];
i = nbrofitems + 1;
x = 0;
While[i > 0 && x < 10,
{
Print["lopp round starting"];
x++;
Print["i"];
Print[i];
Print["w"];
Print[w];
Print["keep[i,w]"];
Print[keep[[i, w]]];
If[keep[[i, w]] == 1,
{
Append[knapsack, i];
Print["tjolahej"];
w -= items[[i - 1, 2]];
i -= 1;
Print["tjolahopp"];
},
i -= 1;
];
Print[i];
Print["loop round done"]
};
knapsack
]
}
]
no errors now, but I do not know what it does really :)

Resources