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

  • Thread starter Leo Liu
  • Start date
  • #1
Leo Liu
345
145
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 2022-11-25 at 8.28.13 PM.png

^ Defining the parameters and the equation using a nested function; only real solutions are recorded.
Screenshot 2022-11-25 at 8.32.11 PM.png

^ Data structure
Screenshot 2022-11-25 at 8.29.04 PM.png

^ 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:
Screenshot 2022-11-25 at 8.36.37 PM.png

The compilation of rho also seems fine:
Screenshot 2022-11-25 at 8.43.50 PM.png

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:
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

  • earth_density.xlsx
    5.9 KB · Views: 9
  • Screenshot 2022-11-25 at 8.28.13 PM.png
    Screenshot 2022-11-25 at 8.28.13 PM.png
    9.2 KB · Views: 9
  • Screenshot 2022-11-25 at 8.29.04 PM.png
    Screenshot 2022-11-25 at 8.29.04 PM.png
    6.8 KB · Views: 10
  • Screenshot 2022-11-25 at 8.46.24 PM.png
    Screenshot 2022-11-25 at 8.46.24 PM.png
    22.4 KB · Views: 12
Last edited:

Answers and Replies

  • #2
Leo Liu
345
145
Screenshots in better quality:
Please click on share to get in link because somehow the link gets automatically turned into a widget.
 
  • #3
33,661
11,229
Please don’t screenshot code. Use the standard code delimiters like so:

Example code block title:
//some code here

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

Example code block title:
//some code here

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.
 
  • #5
33,661
11,229
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.”
 
  • #6
33,661
11,229
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.
 
  • #7
Leo Liu
345
145
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!
 

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

Replies
5
Views
844
  • Last Post
Replies
2
Views
614
  • Last Post
Replies
5
Views
490
Replies
6
Views
779
Replies
4
Views
411
Replies
1
Views
413
Replies
0
Views
401
Replies
6
Views
3K
  • Last Post
Replies
1
Views
644
Replies
1
Views
410
Top