MathGroup Archive 2013

[Date Index] [Thread Index] [Author Index]

Search the Archive

Using Wolfram Alpha in a nested loop

  • To: mathgroup at smc.vnet.net
  • Subject: [mg129604] Using Wolfram Alpha in a nested loop
  • From: Nicholas Martin <en51nm at gmail.com>
  • Date: Tue, 29 Jan 2013 02:41:58 -0500 (EST)
  • Delivered-to: l-mathgroup@mail-archive0.wolfram.com
  • Delivered-to: l-mathgroup@wolfram.com
  • Delivered-to: mathgroup-newout@smc.vnet.net
  • Delivered-to: mathgroup-newsend@smc.vnet.net

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]



  • Prev by Date: Re: sorting?
  • Next by Date: Re: Setting parameters via Manipulate?
  • Previous by thread: Re: Matrix elimination
  • Next by thread: text table