- #1
Leo Liu
- 353
- 156
Hi. I am writing a program in Mathematica that reads a xsl file from excel, then processes it by solving an equation, and lastly turns the processed data into a list for exporting.
Context: Finding the maximum speed of a model plane at different altitudes (density).
Screenshots of the code and outputs (oh gosh the compression):
^ Defining the parameters and the equation using a nested function; only real solutions are recorded.
^ Data structure
^ For loop used to call the function and add the result into a list.
As you can see above, the for loop gives rise to several error messages. The solve nested function takes the density and outputs the CL (lift coefficient) value, which works fine in the code:
The compilation of rho also seems fine:
Then the errors must have been caused by these lines in the for loop:
Code:
Context: Finding the maximum speed of a model plane at different altitudes (density).
Screenshots of the code and outputs (oh gosh the compression):
^ Defining the parameters and the equation using a nested function; only real solutions are recorded.
^ Data structure
^ For loop used to call the function and add the result into a list.
As you can see above, the for loop gives rise to several error messages. The solve nested function takes the density and outputs the CL (lift coefficient) value, which works fine in the code:
The compilation of rho also seems fine:
Then the errors must have been caused by these lines in the for loop:
Could someone please tell me how to fix this? Thanks in advance.CL = solve[rho] /. sols[[1]];
Print[CL];
lst = Append[lst, CL]
Code:
MMA Code:
In[2097]:=
(*max speed*)
ClearAll["Global'*"];
CD0 = 0.01884;
k1 = -0.01985;
k2 = 0.03418;
S = 0.45;
m = 12;
g = 9.81;
W = m g;
Eg = 2.88*10^6;
t = 2*3600;
Pa = 0.79 0.87 Eg/t;
Clear[rho]
Clear[CL]
expression1[CL_,
rho_] = (2 W^3)/(rho S) (CD0 + k1 CL + k2 CL^2)^2/(CL^3)
During evaluation of In[2097]:= Set::write: Tag Times in ((7.2505*10^6 (0.01884 -0.01985 CL+0.03418 CL^2)^2)/(CL^3 rho))[CL_,rho_] is Protected.
Out[2110]= (
7.2505*10^6 (0.01884 - 0.01985 CL + 0.03418 CL^2)^2)/(CL^3 rho)
In[2111]:=
solve[rho_] = NSolve[expression1 == Pa^2, CL, Reals];
During evaluation of In[2111]:= NSolve::ratnz: NSolve was unable to solve the system with inexact coefficients. The answer was obtained by solving a corresponding exact system and numericizing the result.
data = Part[Import["/Users/xxxx/Desktop/earth_density.xlsx"], 1]
Out[2115]= {{1.22523}, {1.2135}, {1.20187}, {1.19032}, {1.17885}, \
{1.16747}, {1.15617}, {1.14496}, {1.13383}, {1.12279}, {1.11182}, \
{1.10094}, {1.09014}, {1.07942}, {1.06878}, {1.05823}, {1.04775}, \
{1.03735}, {1.02703}, {1.01679}, {1.00663}, {0.996547}, {0.98654}, \
{0.976611}, {0.966758}, {0.956981}, {0.947281}, {0.937655}, \
{0.928105}, {0.918629}, {0.909228}, {0.8999}, {0.890646}, {0.881464}, \
{0.872355}, {0.863319}, {0.854354}, {0.845461}, {0.836638}, \
{0.827887}, {0.819205}, {0.810593}, {0.802051}, {0.793578}, \
{0.785173}, {0.776837}, {0.768568}, {0.760367}, {0.752234}, \
{0.744167}, {0.736166}, {0.728231}, {0.720362}, {0.712558}, \
{0.704819}, {0.697144}, {0.689534}, {0.681987}, {0.674504}, \
{0.667083}, {0.659725}, {0.65243}, {0.645196}, {0.638024}, \
{0.630913}, {0.623863}, {0.616873}, {0.609943}, {0.603073}, \
{0.596263}, {0.589511}, {0.582818}, {0.576184}, {0.569607}, \
{0.563088}, {0.556626}, {0.550221}, {0.543872}, {0.53758}, \
{0.531344}, {0.525163}, {0.519037}, {0.512966}, {0.506949}, \
{0.500987}, {0.495079}, {0.489224}, {0.483422}, {0.477672}, \
{0.471976}, {0.466331}, {0.460738}, {0.455197}, {0.449707}, \
{0.444268}, {0.438879}, {0.43354}, {0.428251}, {0.423012}, \
{0.417821}, {0.41268}, {0.407587}, {0.402543}, {0.397546}, \
{0.392597}, {0.387695}, {0.38284}, {0.378032}, {0.37327}, {0.368554}, \
{0.363884}}
In[2093]:=
lst = {}
Clear[rho]
For[i = 1, i <= Length[data], i++,
rho = Part[Part[data, i], 1];
CL = solve[rho] /. sols[[1]];
Print[CL];
lst = Append[lst, CL]
]
lst
Out[2093]= {}
During evaluation of In[2093]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of {CL->0.271305}.
During evaluation of In[2093]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of {CL->0.271305}.
During evaluation of In[2093]:= $RecursionLimit::reclim2: Recursion depth of 1024 exceeded during evaluation of {CL->0.271305}.
During evaluation of In[2093]:= General::stop: Further output of $RecursionLimit::reclim2 will be suppressed during this calculation.
Out[2096]= {}
Attachments
Last edited: