Math & ScienceArrays & Functions (II)

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

Arrays & Functions (II)

Post by LaTeX Beginner »

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:

Code: Select all

\def\FAR(#1){(\ReAi*cos(#1)*(180/pi)+\ImAi*cos(#1)*(180/pi))}
        \def\FAI(#1){(\ReAi*sin(#1)*(180/pi)+\ImAi*sin(#1)*(180/pi))}
And I want to do something like that:

Code: Select all

\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:

Code: Select all

    \usepackage{longtable,array,siunitx,expl3}
    \ExplSyntaxOn
    \tl_new:N \tablecontents
    \int_do_while:nn { \l_tmpa_int <= 360 }
      {
        \fp_set:Nn \l_tmpa_fp { round( \FAR( \l_tmpa_int * deg ) , 2 ) }
        \fp_set:Nn \l_tmpb_fp { round( \Ph( \l_tmpa_int * deg ) , 2 ) }
        \tl_put_right:Nx \tablecontents
          {
            \int_use:N \l_tmpa_int & % x
            \fp_to_tl:N \l_tmpa_fp & % FAR(x)
            \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % Ph(x)
          }
        \int_incr:N \l_tmpa_int
      }
    \ExplSyntaxOff
    \begin{document} 
       
    
    \begin{tikzpicture}
     \begin{axis}
      \addplot[domain=0:360,red] { \FAR(x) } ;
      \addplot[domain=0:360,blue] { \Ph(x) } ;
     \end{axis}
    \end{tikzpicture}


    \begin{longtable}{l*2{S[table-format=-2.2]}}
      $x$ & {$FAR(x)$} & {$Ph(x)$} \\ \hline
     \endhead
     \tablecontents
    \end{longtable}

    \end{document}
I mean, I want to proceed in the same way I've done until this moment.

Any idea?? Thank you so much :)

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Arrays & Functions (II)

Post by cgnieder »

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
site moderator & package author
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Arrays & Functions (II)

Post by LaTeX Beginner »

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:

Code: Select all

\pgfmathatantwo{x}{y}
That, in my case, could be like that:

Code: Select all

\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: 2000
Joined: Sat Apr 16, 2011 7:27 pm

Arrays & Functions (II)

Post by cgnieder »

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:

Code: Select all

\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
site moderator & package author
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Arrays & Functions (II)

Post by LaTeX Beginner »

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:

Code: Select all

\documentclass{article}
    \usepackage{pgfplots}
    \usepackage{xargs}
    \pgfplotsset{compat=1.6}

\def\El{5}
\def\WL{1.0}
\def\RA{1.0}			
\def\IA{0.5}
\def\SE{0.45}		
\def\In(#1){(2*pi/\WL)*\SE*cos(#1)*(180/pi)}
    
\def\FAR(#1){(\RA*cos(\In(#1))*(180/pi)+\IA*cos(\In(#1))*(180/pi))}
\def\FAI(#1){(\RA*sin(\In(#1))*(180/pi)+\IA*sin(\In(#1))*(180/pi))}
\def\FAM(#1){(\FAR(#1)^2+\FAI(#1)^2)^(1/2)}  
         	
    % create the table contents:
    \usepackage{longtable,array,siunitx,expl3}
    \ExplSyntaxOn
    \tl_new:N \tablecontents
    \int_do_while:nn { \l_tmpa_int <= 360 }
      {
        \fp_set:Nn \l_tmpa_fp { round( \FAM( \l_tmpa_int * deg ) , 2 ) }
        \fp_set:Nn \l_tmpb_fp { round( \FAI( \l_tmpa_int * deg ) , 2 ) }
        \tl_put_right:Nx \tablecontents
          {
            \int_use:N \l_tmpa_int & % x
            \fp_to_tl:N \l_tmpa_fp & % FAM(x)
            \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % FAI(x)
          }
        \int_incr:N \l_tmpa_int
      }
    \ExplSyntaxOff
    \begin{document} 
     
    \begin{tikzpicture}
     \begin{axis}
      \addplot[domain=0:360,red] { \FAM(x) } ;
      \addplot[domain=0:360,blue] { \FAI(x) } ;
     \end{axis}
    \end{tikzpicture}


    \begin{longtable}{l*2{S[table-format=-2.2]}}
      $x$ & {$FAM(x)$} & {$FAI(x)$} \\ \hline
     \endhead
     \tablecontents
    \end{longtable}

    \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):

Code: Select all

\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:

Code: Select all

\Instruction1{}[][][][]
And get like results the table of contents and the tikzpicture defined in the code:

Code: Select all

  % create the table contents:
    \usepackage{longtable,array,siunitx,expl3}
    \ExplSyntaxOn
    \tl_new:N \tablecontents
    \int_do_while:nn { \l_tmpa_int <= 360 }
      {
        \fp_set:Nn \l_tmpa_fp { round( \FAM( \l_tmpa_int * deg ) , 2 ) }
        \fp_set:Nn \l_tmpb_fp { round( \FAI( \l_tmpa_int * deg ) , 2 ) }
        \tl_put_right:Nx \tablecontents
          {
            \int_use:N \l_tmpa_int & % x
            \fp_to_tl:N \l_tmpa_fp & % FAM(x)
            \fp_to_tl:N \l_tmpb_fp \exp_not:N \\ % FAI(x)
          }
        \int_incr:N \l_tmpa_int
      }
    \ExplSyntaxOff
    \begin{document} 
     
    \begin{tikzpicture}
     \begin{axis}
      \addplot[domain=0:360,red] { \FAM(x) } ;
      \addplot[domain=0:360,blue] { \FAI(x) } ;
     \end{axis}
    \end{tikzpicture}


    \begin{longtable}{l*2{S[table-format=-2.2]}}
      $x$ & {$FAM(x)$} & {$FAI(x)$} \\ \hline
     \endhead
     \tablecontents
    \end{longtable}

    \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

Re: Arrays & Functions (II)

Post by LaTeX Beginner »

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

Thanks :)
Post Reply