Math & ScienceArrays, Loop Structures and Representations

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, Loop Structures and Representations

Post by LaTeX Beginner »

Hi again! :)

I have some doubts that I hope you can solve. I've defined two arrays (AiModulo & AiFase), using the arrayjob package. I've used this arrays in order to define a set of functions (AiReal1,AiImag1,...,AiReal10,AiImag10), that take specific values of these arrays.

At the same time, I use this functions in order to obtain the values of FARe & FAIm within a foreach structure. With the values of FARe & FAIm I define the final function, FAMODTOTAL

Finally, I want to represent, using another foreach structure, the function FAMODTOTAL, from (\x-1,FAMODTOTAL(\x-1)) to (\x,FAMODTOTAL(\x)). It is because I want that the representation joins a point with the previous point, in the specified range.

Here is my code:

Code: Select all

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

\def\LO{1.0}
\def\KO{2*pi/\LO}
\def\SE{0.45}		
    
\newarray\AiModulo
\readarray{AiModulo}{0&0&0&0&0&1&1&1&1&1}

\newarray\AiFase
\readarray{AiFase}{0&10&20&30&40&50&60&70&80&90}

\newarray\FAMODTOTAL
\readarray{FAMODTOTAL}{0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0&0}

%I do that in order to ensure that the \FAMODTOTAL function has a total of 360 elements

\def\AiReal1{\AiModulo(1)*cos(\AiFase(1)*(180/pi))}
\def\AiImag1{\AiModulo(1)*sin(\AiFase(1)*(180/pi))}

\def\AiReal2{\AiModulo(2)*cos(\AiFase(2)*(180/pi))}
\def\AiImag2{\AiModulo(2)*sin(\AiFase(2)*(180/pi))}

\def\AiReal3{\AiModulo(3)*cos(\AiFase(3)*(180/pi))}
\def\AiImag3{\AiModulo(3)*sin(\AiFase(3)*(180/pi))}

\def\AiReal4{\AiModulo(4)*cos(\AiFase(4)*(180/pi))}
\def\AiImag4{\AiModulo(4)*sin(\AiFase(4)*(180/pi))}

\def\AiReal5{\AiModulo(5)*cos(\AiFase(5)*(180/pi))}
\def\AiImag5{\AiModulo(5)*sin(\AiFase(5)*(180/pi))}

\def\AiReal6{\AiModulo(6)*cos(\AiFase(6)*(180/pi))}
\def\AiImag6{\AiModulo(6)*sin(\AiFase(6)*(180/pi))}

\def\AiReal7{\AiModulo(7)*cos(\AiFase(7)*(180/pi))}
\def\AiImag7{\AiModulo(7)*sin(\AiFase(7)*(180/pi))}

\def\AiReal8{\AiModulo(8)*cos(\AiFase(8)*(180/pi))}
\def\AiImag8{\AiModulo(8)*sin(\AiFase(8)*(180/pi))}

\def\AiReal9{\AiModulo(9)*cos(\AiFase(9)*(180/pi))}
\def\AiImag9{\AiModulo(9)*sin(\AiFase(9)*(180/pi))}

\def\AiReal10{\AiModulo(10)*cos(\AiFase(10)*(180/pi))}
\def\AiImag10{\AiModulo(10)*sin(\AiFase(10)*(180/pi))}

\foreach \x in {1,...,360}
{
\def\aa{\KO*1*\SE*cos(x*180/pi)}
\def\FARe{\AiReal1*cos(\aa)-\AiImag1*sin(\aa)}
\def\FAIm{\AiReal1*sin(\aa)+\AiImag1*cos(\aa)}

\def\aa{\KO*2*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal2*cos(\aa)-\AiImag2*sin(\aa)}
\def\FAIm{\FaIm+\AiReal2*sin(\aa)+\AiImag2*cos(\aa)}

\def\aa{\KO*3*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal3*cos(\aa)-\AiImag3*sin(\aa)}
\def\FAIm{\FAIm+\AiReal3*sin(\aa)+\AiImag3*cos(\aa)}

\def\aa{\KO*4*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal4*cos(\aa)-\AiImag4*sin(\aa)}
\def\FAIm{\FAIm+\AiReal4*sin(\aa)+\AiImag4*cos(\aa)}

\def\aa{\KO*5*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal5*cos(\aa)-\AiImag5*sin(\aa)}
\def\FAIm{\FAIm+\AiReal5*sin(\aa)+\AiImag5*cos(\aa)}

\def\aa{\KO*6*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal6*cos(\aa)-\AiImag6*sin(\aa)}
\def\FAIm{\FAIm+\AiReal6*sin(\aa)+\AiImag6*cos(\aa)}

\def\aa{\KO*7*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal7*cos(\aa)-\AiImag7*sin(\aa)}
\def\FAIm{\FAIm+\AiReal7*sin(\aa)+\AiImag7*cos(\aa)}

\def\aa{\KO*8*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal8*cos(\aa)-\AiImag8*sin(\aa)}
\def\FAIm{\FAIm+\AiReal8*sin(\aa)+\AiImag8*cos(\aa)}

\def\aa{\KO*9*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal9*cos(\aa)-\AiImag9*sin(\aa)}
\def\FAIm{\FAIm+\AiReal9*sin(\aa)+\AiImag9*cos(\aa)}

\def\aa{\KO*10*\SE*cos(x*180/pi)}
\def\FARe{\FARe+\AiReal10*cos(\aa)-\AiImag10*sin(\aa)}
\def\FAIm{\FAIm+\AiReal10*sin(\aa)+\AiImag10*cos(\aa)}

\def\FAMod{(\FARe^2+\FAIm^2)^(1/2)}

\def\FAMODTOTAL(\x){\FAMod}

};
	
	\begin{document}


	\begin{tikzpicture}
	  
    \begin{axis}
    \foreach \x in {2,...,360}
    {
    \addplot[domain=0:360,red] { (\x-1,\FAMODTOTAL(\x-1)) (\x,\FAMODTOTAL(\x)) } ;
    };
    \end{axis}  
       
    \end{tikzpicture}

	\end{document}
With that code, I get some errors that I don't know how to solve...

Please, do you know how to do that in a right way?

Thank you so much :)
Regards.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org
LaTeX books
percusse
Posts: 3
Joined: Mon Nov 12, 2012 11:24 pm

Arrays, Loop Structures and Representations

Post by percusse »

I can feel a little bit of what you are trying to do but the immediate problems make it very difficult to understand the goal. For example, you are using arrayjob while having the excellent pgfplotstable naturally supporting pgfplots. You have a few macro names with numbers, overwriting definitions and redefining them 360 times which begs for a proper mathematical operation iteration instead.

Can you describe how you would like to compute and plot the complex numbers (which are easy to do if you treat them as 2D vectors)?
Post Reply