Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.soft-sys.math.mathematica > #13305
| From | Nicholas Martin <en51nm@gmail.com> |
|---|---|
| Newsgroups | comp.soft-sys.math.mathematica |
| Subject | Using Wolfram Alpha in a nested loop |
| Date | 2013-01-29 07:41 +0000 |
| Message-ID | <ke7ug5$4e3$1@smc.vnet.net> (permalink) |
| Organization | Time-Warner Telecom |
Hi, I'm very new to Mathematica, I just started using it a couple of days ago. I'm trying to write a small application to get data from WolframAlpha for different types of fluid and then store this data.
My problem at the moment is that if I want to create a 2D array of (for example) density, Cv and Cp of air for a range of 100 temperatures and pressures. That's 3*n^2 calls to Wolfram alpha, which will take a long time!
Is there any way speed up my code? I've included my code:
(*First find the max and min values of pressure and temperature in the location you are interested in and create the temperature and pressure lists:*)
presData = WeatherData[{"London", "UK"}, "Pressure", {{2000, 1, 1}, {2009, 12, 31}, "Day"}];
presData[[All, 2]] = 100 presData[[All, 2]];
baddata[entry_] := Not[MatchQ[entry, _?NumberQ]]; (*remove bad datapoints*)
pmax = Max[DeleteCases[presData[[All, 2]], _?baddata]];
pmin = Min[DeleteCases[presData[[All, 2]], _?baddata]];
tempData = WeatherData[{"London", "UK"}, "Temperature", {{2000, 1, 1}, {2009, 12, 31}, "Day"}];
absoluteZero = -WolframAlpha["absolute zero in celsius", {{"Result", 1}, "NumberData"}];
tempData[[All, 2]] = absoluteZero + tempData[[All, 2]]; (*convert to K*)
tmax = Max[DeleteCases[tempData[[All, 2]], _?baddata]];
tmin = Min[DeleteCases[tempData[[All, 2]], _?baddata] ];
stepnum = 100;
dt = (tmax - tmin)/stepnum;
dp = (pmax - pmin)/stepnum;
temps = Range[tmin, tmax, dt];
press = Range[pmin, pmax, dp];
createstring[prop_, fluid_, temp_, pres_] := ToString[prop] <> " of " <> ToString[fluid] <> " at " <> ToString[temp] <> "K and " <> ToString[pres] <> "Pa"
(*Now cycle through i temperatures and j pressures to get the Cp, density and Cv data for air:*)
Cp = Array[0, {stepnum, stepnum}];
rho = Array[0, {stepnum, stepnum}];
Cv = Array[0, {stepnum, stepnum}];
For[i = 1, i < stepnum + 1, i++,
For[j = 1, j < stepnum + 1, j++,
Print[{i, j}];
text = createstring["Cp", "air", temps[[i]], press[[j]]];
Print["Getting data for " <> ToString[text ]];
data = WolframAlpha[
ToString[text ], {{"UnitConversion", 3}, "NumberData"}];
Print[data];
Cp[[i, j]] = data;
text = createstring["density", "air", temps[[i]], press[[j]]];
Print["Getting data for " <> ToString[text ]];
data = WolframAlpha[ToString[text ], {{"Result", 1}, "NumberData"}];
Print[data];
rho[[i, j]] = data;
text = createstring["Cv", "air", temps[[i]], press[[j]]];
Print["Getting data for " <> ToString[text ]];
data = WolframAlpha[
ToString[text ], {{"UnitConversion", 3}, "NumberData"}];
Print[data];
Cv[[i, j]] = data;
]
]
ListContourPlot[rho]
ListContourPlot[Cp]
ListContourPlot[Cv]
Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread
Using Wolfram Alpha in a nested loop Nicholas Martin <en51nm@gmail.com> - 2013-01-29 07:41 +0000
csiph-web