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

Click For Summary

Discussion Overview

The discussion revolves around troubleshooting a Mathematica program designed to process data from an Excel file to find the maximum speed of a model plane at various altitudes. Participants focus on resolving errors encountered during the execution of a for loop that processes a list of density values.

Discussion Character

  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their program's objective and the errors produced by the for loop, specifically related to the handling of the lift coefficient (CL) calculation.
  • Another participant suggests that the absence of semicolons after the Clear commands may be causing issues.
  • Some participants express that adding semicolons does not resolve the errors, indicating that the problem persists.
  • A later reply proposes that there may be residual data in memory affecting the execution, recommending quitting the kernel and re-evaluating the code.
  • One participant confirms that after following the suggestion to restart the kernel, their code works correctly.

Areas of Agreement / Disagreement

There is no consensus on the initial cause of the errors, as participants offer different troubleshooting suggestions. However, there is agreement that restarting the kernel resolved the issue for one participant.

Contextual Notes

Participants mention specific error messages encountered during evaluation, including issues related to protected tags and recursion limits, but do not resolve the underlying causes of these errors.

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 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:
[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: 184
  • Screenshot 2022-11-25 at 8.28.13 PM.png
    Screenshot 2022-11-25 at 8.28.13 PM.png
    7.6 KB · Views: 169
  • Screenshot 2022-11-25 at 8.29.04 PM.png
    Screenshot 2022-11-25 at 8.29.04 PM.png
    6.8 KB · Views: 222
  • Screenshot 2022-11-25 at 8.46.24 PM.png
    Screenshot 2022-11-25 at 8.46.24 PM.png
    20.7 KB · Views: 183
Last edited:
Physics news on Phys.org
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.”
 
  • Wow
Likes   Reactions: Leo Liu
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.
 
  • Like
Likes   Reactions: Leo Liu
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!
 
  • Like
Likes   Reactions: Dale

Similar threads

  • · Replies 4 ·
Replies
4
Views
2K