Group Abstract Group Abstract

Message Boards Message Boards

Assistance with solving coupled equations using module with external file

Dear All,
I am seeking your assistance in solving a system of two coupled equations using a Module that involves writing to and reading from an external file. To clarify my requirement, I have included an example program below that demonstrates my approach. However, the current implementation does not work as intended.
Could you please help me identify and correct the issues in my program? Your guidance would be greatly appreciated.

FF[xx1_, xx2_] := Module[
   {xxx1 = xx1, xxx2 = xx2, FF1, FF2},

   ClearAll[FileC];
   FileC = "C:\\Mohsen-Files\\MLSF\\W-1\\MLSF.3\\MLSF\\C.txt";

   OpenWrite[FileC];
   WriteString[
    FileC,
    Sin[xxx1], "\n",
    Cos[xxx2]
    ];

   Close[FileC];

   ClearAll[DataC];
   DataC = Import[FileC, "Table"];

   ClearAll[FF1, FF2];
   FF1 = xxx1*DataC[[1, 1]];
   FF2 = xxx2*DataC[[2, 1]];

   {FF1, FF2}

   ];

FindRoot[
 FF[x1, x2] == {0.0, 0.0},
 {{x1, \[Pi]/4}, {x2, \[Pi]/4}}
 ]
4 Replies

The problem is DataC[[1, 1]] comes out as a string, not as an expression. Try

FF = ToExpression[DataC[[1, 1]]] - xx;

By the way, the local variables xx and FF are superfluous. I would rather write

F[x_] := 
  Module[{FileC, DataC}, 
   FileC = "C:\Mohsen-Files\MLSF\W-1\MLSF.3\MLSF\C.txt";
   OpenWrite[FileC];
   WriteString[FileC, Tan[x]];
   Close[FileC];
   DataC = Import[FileC, "Table"];
   ToExpression[DataC[[1, 1]]] - x];
POSTED BY: Gianluca Gorni

Dear Gianluca, Thank you so much for your help it works perfectly now! I truly appreciate your support.

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