Message Boards Message Boards

Execute for loop for importing and solving equations?

Posted 5 years ago

Hi, I have an excel file with complex numbers and I want to solve some simultaneous equations with them. I tried creating a for loop that imports row i from columns 11,12,13,14 and assigns them to a variable and then solves the needed simultaneous equations though it seems to not give any output. For reference, this works when i do it with a single case i.e. get rid of the loop and replace all i with a specific row e.g. 1, but how do I iterate it to complete it several times?

For[i = 1; i < 3, i++, 
 x = Import[
   "C:\\Users\\Laptop\\simultaneous.xlsx", 
{"Data", 1, i, 11}]; 
 y = Import[
   "C:\\Users\\ Laptop\\simultaneous.xlsx", 
{"Data", 1, i, 12}]; 
 z = Import[
   "C:\\Users\\Laptop\\simultaneous.xlsx", 
{"Data", 1, i, 13}];
 m = Import[
   "C:\\Users\\Laptop\\simultaneous.xlsx", 
{"Data", 1, i, 14}];
 eq1 = x == a + (y*z*b)/(1 - b^2);
    eq2 = m == b + (y*z*a)/(1 - a^2);
 NSolve[eq1 && eq2, {a, b}]]
12 Replies
Posted 5 years ago

Apart from being rater inefficient since the file is imported four times, check the documentation For.

POSTED BY: Rohit Namjoshi

I saw the documentation for it and thought the code I wrote met all the requirements, though it seems to not work nonetheless

Posted 5 years ago

By itself For does not give any output, as is mentioned in the documentation under Details:

enter image description here

You would have to add code to save the results from each row. But it might be easier to use Table instead of For. That would produce an output with the row results in a list.

POSTED BY: Hans Milton
Posted 5 years ago

Here is the For loop with the body replaced by Echo. Now do you see the difference between it and the documentation for For?

For[i = 1; i < 3, i++, Echo[i]]

Avoid the loop using

Import[file, {"Data", 1, 1 ;; 3, 11 ;; 14}]
POSTED BY: Rohit Namjoshi

But wouldn't a loop still be required? (since I want to repeat the same calculation on every row of the spreadsheet)

This seems to work, thank you

Posted 5 years ago

The functional way to do it is to use Map.

POSTED BY: Rohit Namjoshi

Thanks

I'm having some difficulty with the map function. Right now my code looks like this:

ClearAll["Global`*"]
e = Import[
   "C:\\Users\\simultaneous_2.xlsx", \
{"Data", 1, {1, 2}, 18}];

x = ToExpression@e;
f = Import[
   "C:\\Users\\simultaneous_2.xlsx", \
{"Data", 1, {1, 2}, 19}];
y = ToExpression@f;
g = Import[
   "C:\\Users\\simultaneous_2.xlsx", \
{"Data", 1, {1, 2}, 20}];
z = ToExpression@g;
h = Import[
   "C:\\Users\\simultaneous_2.xlsx", \
{"Data", 1, {1, 2}, 21}];
m = ToExpression@h;
eq1 = x2 == a + (y2*z2*b)/(1 - b^2);
  eq2 = m2 == b + (y2*z2*a)/(1 - a^2);
fn = NSolve[eq1 && eq2, {a, b}];

So currently x,y,z and m are arrays of length 2. How would I use map on this though? I can't directly map Nsolve because there is no input to that function. I am thinking I would have to use map on x,y,z,m e.g.

x[[i]]=x[[i]]
Map[x, {1, 2}]

but this does not seem to work. Any input would be appreciated

Posted 5 years ago

If you have the imported data in arrays x, y, z, m, you can then use Table to do the final calculation:

Table[
    eq1 = x[[i]] == a + (y[[i]]*z[[i]]*b)/(1 - b^2);
    eq2 = m[[i]] == b + (y[[i]]*z[[i]]*a)/(1 - a^2);
    NSolve[eq1 && eq2, {a, b}],
    {i, 2}
 ]
POSTED BY: Hans Milton

This worked perfectly. Thank you!

.

Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract