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

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

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 Rolf,
Thank you very much for your kind response and for addressing my question so thoughtfully. However, I fear I may not have explained my primary concern clearly enough. To clarify, let’s consider the following scenario:
We have a function F[x], and our objective is to find its root near x = 0. The complication arises because this function is implemented as a module that interacts with an external file for reading and writing data. Unfortunately, we lack insight into the internal structure of this module or its specific operations.
To better illustrate the issue, let me provide a simpler example: solving the equation Tan[x] = x. This example might help demonstrate the type of problem I'm encountering. Thank you again for your support, and I look forward to hearing your insights.

F[x_] := Module[
   {xx= x, FF},

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

   OpenWrite[FileC];
   WriteString[FileC,Tan[xx] ];
   Close[FileC];

   DataC = Import[FileC, "Table"];

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

   FF

   ];

FindRoot[F[x] ==0.0,{x, 0.0}]

FindRoot has attribute HoldAll. Therefore FF does evaluate in a way you do not expect.

I would just use StepMonitor. Herr a quick fix:

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

   ClearAll[FileC];
   FileC = CreateFile[];

   OpenWrite[FileC];
   WriteString[
    FileC,
    ToString[#,InputForm]& @ Sin[xxx1], "\n",
    ToString[#,InputForm]& @ 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[{Sin[x1], Cos[x2]} == {0., 0.}, {{x1, Pi/4}, {x2, Pi/4}}, 
    StepMonitor :> (Print["Step to x1 = ", x1, " x2: ", x2]; FF[x1, x2])
]

FilePrint @ FileC
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

Group Abstract Group Abstract