[Mathematica] How to use for loop to process a list of data

6 replies · 5K views · last reply
Leo Liu
Messages
353
Reaction score
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):
Screenshot of a Mathematica notebook cell defining constants CD0, k1, k2, S, m, g, W, Eg, t and Pa, then an expression for CL and rho, with an error message about the Set tag Times being protected.

^ Defining the parameters and the equation using a nested function; only real solutions are recorded.
Mathematica session showing an NSolve error message, followed by a long list of decimal values (1.22523, 1.2135, 1.20187...) output from importing the first column of earth_density.xlsx.

^ Data structure
Mathematica notebook screenshot of a For loop that appends CL values to a list, followed by repeated $RecursionLimit warning messages reporting recursion depth 1024 exceeded during evaluation of CL → 0.271305.

^ 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:
Mathematica session: input solve[1.4] and output {{CL -> 0.259723}, {CL -> 13.5505}}.

The compilation of rho also seems fine:
Mathematica notebook screenshot showing a For loop code cell with rho extracted from data, followed by a column of numeric output values from 1.22523 down to 1.03735.

Then the errors must have been caused by these lines in the for loop:
CL = solve[rho] /. sols[[1]];
Print[CL];
lst = Append[lst, CL]
Could someone please tell me how to fix this? Thanks in advance.


Code:
[CODE title="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]= {}[/CODE]
 
Attachments
  • earth_density.xlsx
    earth_density.xlsx
    5.9 KB · Views: 222
  • Screenshot of a Mathematica notebook showing Clear and parameter definitions (CD0, k1, k2, S, m, g, Eg, t, Pa), a defined function expression1 for CL and rho, and an error message about a protected Set tag, followed by an NSolve error.
    Screenshot 2022-11-25 at 8.28.13 PM.png
    7.6 KB · Views: 204
  • Mathematica notebook screenshot: a For loop that sets rho from data, solves for CL with sols, and appends CL to lst, followed by repeated RecursionLimit error messages.
    Screenshot 2022-11-25 at 8.29.04 PM.png
    6.8 KB · Views: 253
  • Screenshot 2022-11-25 at 8.46.24 PM.png
    Screenshot 2022-11-25 at 8.46.24 PM.png
    20.7 KB · Views: 215
Last edited:
Screenshots in better quality:
Please click on share to get in link because somehow the link gets automatically turned into a widget.
 
Please don’t screenshot code. Use the standard code delimiters like so:

[CODE lang="cpp" title="Example code block title"]//some code here [/CODE]

Your first error is caused by not having semicolons after Clear[rho] and Clear[CL]
 
  • Love
  • Like
Likes   Reactions: Mark44 and Leo Liu
Dale said:
Please don’t screenshot code. Use the standard code delimiters like so:

[CODE lang="cpp" title="Example code block title"]//some code here [/CODE]

Your first error is caused by not having semicolons after Clear[rho] and Clear[CL]
I have put my code into a proper code bloc; thanks. Putting semicolons after Clear[] does not solve the issue, unfortunately.
 
Leo Liu said:
Putting semicolons after Clear[] does not solve the issue, unfortunately.
Not even the first one?: “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.”
 
It looks like you have some old stuff in memory. Your code here doesn't even produce the first error. Try quitting your kernel and evaluating again.
 
Dale said:
It looks like you have some old stuff in memory. Your code here doesn't even produce the first error. Try quitting your kernel and evaluating again.
Thanks a lot! It is working now!