i am new to mathimathica and i just can do basic things with it. i have a file.txt that contains roots of two polynomials $p(x),q(x)$. both polynomials have the same degre and first n numbers are roots of p and next n numbers are roots of q , now i have thousands of $(p,q)$ roots in file.txt how can i read roots from file.txt and plot $p(x)-q(x)$ for $-1 \le x \le 1$.

for example if degre is 2 and have 2 $(p,q)$ roots, and i have 1 2 3 4 5 6 7 8 in file i want first plot $(x-1)(x-2)-(x-3)(x-4)$ and then $(x-5)(x-6)-(x-7)(x-8)$.

at the moment i only write Plot instruction manually and copy data one by one which is not good. i would really appreciate your help.


1 Answer 1


I'm not 100% clear on the structure of "file.txt", but perhaps this will work as a starting point. I created a test file called "test.txt" which contains a single number on each row with the numbers 1 through 8.

numCurves = 2; (* The total number of curves to plot *)
numPolys = 2; (* The total number of polynomials i.e. just p(x) and q(x) for now *)
degree = 2; (* The degree of each polynomial *)
data = Import["~/Desktop/test.txt", "Data"];
array = ArrayReshape[
   data, {numCurves, numPolys, degree}];
temp = Subtract @@@ Apply[Times, x - array, {2}]
Plot[temp, {x, -1, 1}]

Plot of 2 polynomials.

This assumes that you know the number of p/q datasets in the file. If you don't know that beforehand, you can do a quick calculation using the Dimensions of data.

Basically, I import the text file and then reshape it into a form that has numCurves rows, numPolys columns (always 2 if you're just doing p(x) - q(x)), and degree elements in each of the 2 columns.


$\left( \begin{array}{c} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \\ 7 \\ 8 \\ \end{array} \right)$

$\left( \begin{array}{cc} \{1,2\} & \{3,4\} \\ \{5,6\} & \{7,8\} \\ \end{array} \right)$

We can see what happens when we do x - array:

$\left( \begin{array}{cc} \{x-1,x-2\} & \{x-3,x-4\} \\ \{x-5,x-6\} & \{x-7,x-8\} \\ \end{array} \right)$

I'm using Apply[Times, x - array, {2}] so that it multiplies all the x - _ parts within a single column together, but I don't want anything else getting multiplied. The curly brackets around the 2 tell it I only want it to apply at level 2 of my array.

$\left( \begin{array}{cc} (x-2) (x-1) & (x-4) (x-3) \\ (x-6) (x-5) & (x-8) (x-7) \\ \end{array} \right)$

So now the format of my array is basically:

$\left( \begin{array}{cc} p_1(x) & q_1(x) \\ p_2(x) & q_2(x) \\ \end{array} \right)$

so I can use Subtract @@@ Apply[Times, x - array, {2}] to get my final set of polynomials:

$\{(x-2) (x-1)-(x-4) (x-3),(x-6) (x-5)-(x-8) (x-7)\}$

The @@@ symbol is shorthand for Apply[___, ___, {1}]. Finally, since there's just a list of polynomials, you can call Plot with that list.

If your data is in a slightly different format (like there are multiple numbers on each row), the initial steps with reshaping might be slightly different.

  • $\begingroup$ thank you very much , the format of file.txt is like this , we know number of $(p,q)$ roots , we know their degre (they are equal) , for example if we have 3 $(p,q)$ roots , and if their degre is 4 for example , we will have 24 roots in total , first 4 roots for p1 , next 4 roots for q1 and we draw p1-q1 , then the next 4 roots for p2 and next 4 roots for q2 again draw p2-q2 and so on. also is it possible to draw each graph separately? i do not want draw lots of functions in one image. i hope my explanation clear things more , sorry if it is not good enough. $\endgroup$ Commented Mar 9, 2021 at 21:57
  • $\begingroup$ @wiliammercer Sure, drawing them separately is easy and there are a number of options. Plot[#, {x, -1, 1}]&/@temp, Table[Plot[temp[[i]], {x, -1, 1}], {i, 1, Length@temp}], Map[Plot[#, {x, -1, 1}]&, temp]`, etc. I think my code above follows the format you specified, I just wasn't sure if each row of the text file had a single number on it or if it had all of the p roots or all of the p and all of the q roots. $\endgroup$
    – MassDefect
    Commented Mar 9, 2021 at 22:38
  • $\begingroup$ absolutely perfect , thank you very much. $\endgroup$ Commented Mar 9, 2021 at 23:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.