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

• Mathematica
• Leo Liu
In summary, the conversation involves a person seeking help with their Mathematica program. The program is designed to read an xsl file, solve an equation, and turn the data into a list for exporting. However, there are several error messages that arise, most likely due to issues with the for loop and the solve nested function. After receiving some suggestions, the person is able to fix the issue by quitting their kernel and re-evaluating their code.

#### Leo Liu

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:
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: 79
• Screenshot 2022-11-25 at 8.28.13 PM.png
9.2 KB · Views: 90
• Screenshot 2022-11-25 at 8.29.04 PM.png
6.8 KB · Views: 86
• Screenshot 2022-11-25 at 8.46.24 PM.png
22.4 KB · Views: 88
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:

Example code block title:
//some code here

Your first error is caused by not having semicolons after Clear[rho] and Clear[CL]

Mark44 and Leo Liu
Dale said:
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.

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.”

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.

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!

Dale

## What is a for loop in Mathematica?

A for loop in Mathematica is a programming construct that allows you to repeatedly execute a set of instructions for a specified number of times. It is commonly used for data processing and manipulation.

## How do I use a for loop to process a list of data in Mathematica?

To use a for loop to process a list of data in Mathematica, you need to first define the list and then use the "For" keyword followed by a set of curly braces. Inside the curly braces, you can specify the instructions to be executed for each element in the list.

## Can I use a for loop to modify the elements in a list in Mathematica?

Yes, you can use a for loop to modify the elements in a list in Mathematica. You can access and modify individual elements by using the "Part" function and specifying the index of the element you want to modify.

## What is the syntax for a for loop in Mathematica?

The basic syntax for a for loop in Mathematica is:

For [iterator, start, end, increment, body]

where "iterator" is a variable used to iterate through the loop, "start" and "end" are the starting and ending values for the loop, "increment" is the amount by which the "iterator" variable is incremented at each iteration, and "body" is the set of instructions to be executed for each iteration.

## Can I use a for loop to iterate through multiple lists in Mathematica?

Yes, you can use a for loop to iterate through multiple lists in Mathematica by using the "Table" function. This function allows you to create a list of values by evaluating a set of instructions for each element in the loop.