LaTeX forum ⇒ Math & ScienceArrays & Functions (II)

Information and discussion about LaTeX's math and science related features (e.g. formulas, graphs).
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Arrays & Functions (II)

Postby LaTeX Beginner » Tue Nov 20, 2012 4:37 pm

Hi again!

(I've created a new post, because the previous post is solved)

I'm sorry for disturb you again, but I have a doubt that I think could be easy to solve.

In the same line I've working, I want to represent the phase of a complex number (a+j*b), where "a" is the real part and "b" is the imaginary part of the number.

The phase is given by the following equation:

Ph=arctan(b/a)

I don't know how to obtain the arctan of two variables. In this case, this two variables are:

  1. \def\FAR(#1){(\ReAi*cos(#1)*(180/pi)+\ImAi*cos(#1)*(180/pi))}
  2. \def\FAI(#1){(\ReAi*sin(#1)*(180/pi)+\ImAi*sin(#1)*(180/pi))}


And I want to do something like that:

  1. \def\Ph(#1){arctan(\FAI(#1)/\FAR(#1))}


But I don't find the way to do that. I've installed the packages pst-math and pst-ploth, and I've used the function ATAN, but it still give me some errors... And, of course, I want to do something like that:

  1. \usepackage{longtable,array,siunitx,expl3}
  2. \ExplSyntaxOn
  3. \tl_new:N \tablecontents
  4. \int_do_while:nn { \l_tmpa_int <= 360 }
  5. {
  6. \fp_set:Nn \l_tmpa_fp { round( \FAR( \l_tmpa_int * deg ) , 2 ) }
  7. \fp_set:Nn \l_tmpb_fp { round( \Ph( \l_tmpa_int * deg ) , 2 ) }
  8. \tl_put_right:Nx \tablecontents
  9. {
  10. \int_use:N \l_tmpa_int & % x
  11. \fp_to_tl:N \l_tmpa_fp & % FAR(x)
  12. \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % Ph(x)
  13. }
  14. \int_incr:N \l_tmpa_int
  15. }
  16. \ExplSyntaxOff
  17. \begin{document}
  18.  
  19.  
  20. \begin{tikzpicture}
  21. \begin{axis}
  22. \addplot[domain=0:360,red] { \FAR(x) } ;
  23. \addplot[domain=0:360,blue] { \Ph(x) } ;
  24. \end{axis}
  25. \end{tikzpicture}
  26.  
  27.  
  28. \begin{longtable}{l*2{S[table-format=-2.2]}}
  29. $x$ & {$FAR(x)$} & {$Ph(x)$} \\ \hline
  30. \endhead
  31. \tablecontents
  32. \end{longtable}
  33.  
  34. \end{document}


I mean, I want to proceed in the same way I've done until this moment.

Any idea?? Thank you so much :)

Tags:

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Tue Nov 20, 2012 9:25 pm

As I've mentioned a few times in the other thread: these calculations use the expl3 module »fp«. All of its features are explained in interface3. To be more specific: available operations are explained in section 9.3. You'll find that atan and its siblings asin and acos are missing. As a matter of fact they are mentioned in section 10 (Disclaimer and Roadmap) as part of the todo list. So I'm afraid you'd need either to find another way to calculate the arctangent or use another method to generate the table.

Thinking about it: probably the best way would be to generate a csv file with some other program and use datatool or pgfplotstable to access the numbers and display them.

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Postby LaTeX Beginner » Wed Nov 21, 2012 12:26 pm

Thanks for the reply.

I've been looking at the pgfmanual, and I've found that it's possible to get the arctan of two numbers using this function:

  1. \pgfmathatantwo{x}{y}


That, in my case, could be like that:

  1. \def\Ph(#1){\pgfmathparse(\FAR(#1),\FAI(#1))\pgfmathresult}


But it give me so many errors, always the same error: the function ... already exists. It refers to math functions, because I'm using the pgfplotspackage, that is necessary for my work.

Other way that I think that could be fine, it's to get the phase of a complex number by some LaTeX function, but I don't find it yet... I mean, if I had two numbers (\FAR and \FAI), which are the real and the imaginary part of a complex number, I suppose that should exists some function in LaTeX that could enable me to get the phase of the complex number (which could be get by the equation Ph=arctan(b/a), where "b" is the imaginary part and "a" is the real part of the complex number).

I think that, if I don't find any other solution, I could try that you say: define the equations in a CSV, and use some function that reads this values... But it looks a bit difficult.. :)

User avatar
cgnieder
Site Moderator
Posts: 1974
Joined: Sat Apr 16, 2011 7:27 pm
Location: Germany
Contact:

Postby cgnieder » Wed Nov 21, 2012 12:38 pm

No, pgf's math functions do not work together with expl3's. As I tried to emphasize a few times now: the code for the table I gave you uses expl3 for the calculations, specifically in the argument of the \fp_set:Nn macro:

  1. \fp_set:Nn \l_tmpa_fp { <here is where the calculations take place> }


You whole task at hand is something TeX is not designed for. TeX was designed for typesetting books but not for doing complicated calculations. Amazingly enough that there are packages like »l3fp«, »pgfmath«, or »fp«. All of them go to some trouble doing these calculations and all of them have limitations.

Your task probably is easy in LuaLaTeX using Lua for the calculations. I have no experience with Lua at all, though, so I can't give you jump start there...

Regards
Clemens
------------------------------
chemmacros · chemformula · leadsheets · xsim

LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Postby LaTeX Beginner » Tue Nov 27, 2012 12:41 pm

Hi again! :)

Thanks for the reply. For the moment, I must to do other things, so I will think about how to do the arctangent later...

I have two doubts. At first, I attach here my code:

  1. \documentclass{article}
  2. \usepackage{pgfplots}
  3. \usepackage{xargs}
  4. \pgfplotsset{compat=1.6}
  5.  
  6. \def\El{5}
  7. \def\WL{1.0}
  8. \def\RA{1.0}
  9. \def\IA{0.5}
  10. \def\SE{0.45}
  11. \def\In(#1){(2*pi/\WL)*\SE*cos(#1)*(180/pi)}
  12.  
  13. \def\FAR(#1){(\RA*cos(\In(#1))*(180/pi)+\IA*cos(\In(#1))*(180/pi))}
  14. \def\FAI(#1){(\RA*sin(\In(#1))*(180/pi)+\IA*sin(\In(#1))*(180/pi))}
  15. \def\FAM(#1){(\FAR(#1)^2+\FAI(#1)^2)^(1/2)}
  16.  
  17. % create the table contents:
  18. \usepackage{longtable,array,siunitx,expl3}
  19. \ExplSyntaxOn
  20. \tl_new:N \tablecontents
  21. \int_do_while:nn { \l_tmpa_int <= 360 }
  22. {
  23. \fp_set:Nn \l_tmpa_fp { round( \FAM( \l_tmpa_int * deg ) , 2 ) }
  24. \fp_set:Nn \l_tmpb_fp { round( \FAI( \l_tmpa_int * deg ) , 2 ) }
  25. \tl_put_right:Nx \tablecontents
  26. {
  27. \int_use:N \l_tmpa_int & % x
  28. \fp_to_tl:N \l_tmpa_fp & % FAM(x)
  29. \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % FAI(x)
  30. }
  31. \int_incr:N \l_tmpa_int
  32. }
  33. \ExplSyntaxOff
  34. \begin{document}
  35.  
  36. \begin{tikzpicture}
  37. \begin{axis}
  38. \addplot[domain=0:360,red] { \FAM(x) } ;
  39. \addplot[domain=0:360,blue] { \FAI(x) } ;
  40. \end{axis}
  41. \end{tikzpicture}
  42.  
  43.  
  44. \begin{longtable}{l*2{S[table-format=-2.2]}}
  45. $x$ & {$FAM(x)$} & {$FAI(x)$} \\ \hline
  46. \endhead
  47. \tablecontents
  48. \end{longtable}
  49.  
  50. \end{document}


The first doubt that I have is how to define an array like a variable. I think that it's easy, but this doubt is related with the following doubt.
Now, I want to use an instruction in order to call some variables previously defined in my code, and in this way obtain some results. I know how to do that in MatLab, but in LaTeX it is different... I've installed the xargs package in order to make this instruction.

So, imagine that I want to create this instruction with five arguments (\El, \WL, \RA, \IA and \SE), and \RA and \IA are the two variables that I want to define like two arrays (I mean, I want that this variables could take multiple values). So, I think the instruction could be something like that (I don't know if it's right):

  1. \newcommandx{\Instruction1}[5][1=,2=,3=,4=,5=]{\El \WL \RA \IA \SE}


With [5] I say the number of arguments that the instruction have; with [1=, 2=, ... 5=] I say the values that I want the arguments take; and with {\El \WL \RA \IA \SE} I specify the arguments to take into account.
But, like I said before, I want to define \RA and \IA like two arrays, in the way \RA could be, for example, [0,1,1,1], and \IA could be, for example, [5,10,15,20], and I want to specify it in the instruction (I suppose that I've to define previously empty arguments). Do you know what I'm saying?

And finally, I want to make a call to this instruction, in the way:

  1. \Instruction1{}[][][][]


And get like results the table of contents and the tikzpicture defined in the code:

  1. % create the table contents:
  2. \usepackage{longtable,array,siunitx,expl3}
  3. \ExplSyntaxOn
  4. \tl_new:N \tablecontents
  5. \int_do_while:nn { \l_tmpa_int <= 360 }
  6. {
  7. \fp_set:Nn \l_tmpa_fp { round( \FAM( \l_tmpa_int * deg ) , 2 ) }
  8. \fp_set:Nn \l_tmpb_fp { round( \FAI( \l_tmpa_int * deg ) , 2 ) }
  9. \tl_put_right:Nx \tablecontents
  10. {
  11. \int_use:N \l_tmpa_int & % x
  12. \fp_to_tl:N \l_tmpa_fp & % FAM(x)
  13. \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % FAI(x)
  14. }
  15. \int_incr:N \l_tmpa_int
  16. }
  17. \ExplSyntaxOff
  18. \begin{document}
  19.  
  20. \begin{tikzpicture}
  21. \begin{axis}
  22. \addplot[domain=0:360,red] { \FAM(x) } ;
  23. \addplot[domain=0:360,blue] { \FAI(x) } ;
  24. \end{axis}
  25. \end{tikzpicture}
  26.  
  27.  
  28. \begin{longtable}{l*2{S[table-format=-2.2]}}
  29. $x$ & {$FAM(x)$} & {$FAI(x)$} \\ \hline
  30. \endhead
  31. \tablecontents
  32. \end{longtable}
  33.  
  34. \end{document}


But I don't know how to do...

Any idea??

Thank you so much :)

LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Postby LaTeX Beginner » Thu Nov 29, 2012 11:41 am

Please, anyone can help me with this?? I really need it...

Thanks :)


Return to “Math & Science”

Who is online

Users browsing this forum: No registered users and 2 guests