tikz luatex add tag
Raaja
Not sure why the 2nd switching in `\ifnum` is not happening. That is in the first part, based on my input definition, the ODE grows, and second, based on the conditional, the ODE decays. However, what I want is that it needs to grow back - decay again in a cycle. Currently, this is what I have, for some reasons, my script is stuck in some sort of loop and I am not able to get it out.

Any help is appreciated.


```
%&lualatex
% !TeX TXS-program:compile = txs:///lualatex/[--shell-escape]
\documentclass[border=5mm,tikz,convert={density=1200,outext=.png}]{standalone}
\usepackage{ifthen}
\usetikzlibrary{calc}
%https://tex.stackexchange.com/questions/471741/draw-solution-curves-of-a-differential-equation-with-tikz?noredirect=1&lq=1
%https://tex.stackexchange.com/questions/294686/ifthenelse-inside-tikz-not-working
\begin{document}
	\begin{tikzpicture}[declare function={f(\x,\y)=(-1*\x) + \y;}]
	\def\xmax{10} \def\xmin{0}
	\def\ymax{5} \def\ymin{0}
	\def\xini{0} \def\uhot{5} \def\ucold{0}
	\def\tmax{10} \def\tmin{0}
	%	\def\nx{15} % \def\ny{15}
	
	%	\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
	%	\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
	%	\foreach \i in {0,...,\nx}
	%	\foreach \j in {0,...,\ny}{
	%		\draw[teal,-stealth] 
	%		({\xmin+\i*\hx},{\ymin+\j*\hy}) -- ++ ({atan2(f({\xmin+\i*\hx},{\ymin+\j*\hy}),1)}:0.1);
	%	}
	\pgfmathsetmacro{\stept}{0.01}
	\pgfmathsetmacro{\nextt}{\tmin+\stept}
	\pgfmathsetmacro{\nextnextt}{\tmin+2*\stept}
	\pgfmathsetmacro{\tfin}{\tmax+0.1}
	\pgfmathsetmacro{\tref}{\tmin}
	\pgfmathsetmacro{\u}{\uhot}
	\pgfmathsetmacro{\tcold}{0.2}
	\pgfmathsetmacro{\ccount}{1}
	\xdef\lstX{(\tmin,\xini)}
	\pgfmathsetmacro{\myy}{\xini}
	\pgfmathsetmacro{\rflag}{1}
	\foreach \x in {\nextt,\nextnextt,...,\tfin}
	{\pgfmathparse{(\x - (0.4*\ccount)) < 0.00001 ? int(1) : int(0)}
		\pgfmathsetmacro{\rtest}{\pgfmathresult}
		\pgfmathparse{and(\rtest, \rflag)}
		\pgfmathsetmacro{\rrtest}{\pgfmathresult}
		\ifnum\rrtest=0
			\pgfmathsetmacro{\u}{\ucold}
			\pgfmathsetmacro{\tref}{\x}
			\pgfmathsetmacro{\rflag}{0}
		\else
			\pgfmathparse{(\x - \tref - \tcold) < 0.00001 ? int(1) : int(0)}
			\pgfmathsetmacro{\crtest}{\pgfmathresult}
			\ifnum\crtest=0
				\pgfmathsetmacro{\u}{\uhot}
				\pgfmathsetmacro{\ccount}{\ccount+1}
				\pgfmathsetmacro{\rflag}{1}
			\fi
		\fi
	
		{
			
			\pgfmathsetmacro{\myy}{\myy+f(\myy,\u)*\stept}
			\xdef\myy{\myy}
			\xdef\lstX{\lstX (\x,\myy)}
		}
	}
	\draw[red,ultra thick] plot[smooth] coordinates {\lstX};
	% grid
	\draw (\xmin,\ymin) rectangle ($(\xmax,\ymax)+(1mm,1mm)$);
	%	\draw (current bounding box.north) node[above=5mm]{$y'=x^2+y^2-1$.};
	% ticks
	\foreach \i in {0,1,...,\xmax} 
	\draw (\i,\ymin) node[below]{$\i$}--++(90:.5mm); 
	\foreach \i in {0,\ymax}
	\draw (\xmin,\i) node[left]{$\i$}--++(0:.5mm);
	\end{tikzpicture}
\end{document}
```

![Capture.PNG](/image?hash=3bcb87f7a0c4f8249edeb21d9787ad00d6de52d5f6b5a606d8a21ba8ec64b548)

What would be more nice is that, a `mesh` like coloring for the figure.

Ideally what I am trying to do is something like this:

![Capture.PNG](/image?hash=d49a219670a324c383c74f2739f3fabba699cd996e47e73a214632573aa3031d)
Top Answer
user 3.14159
I want to start with an apology. I wanted to improve your code by avoiding global variables. However, now for the same input parameters the plot looks different. I think I could fix this if you explain the ODE you solve a bit more.

The main point, though, is to advertize `const plot`. 

```
\documentclass[border=5mm,tikz,convert={density=1200,outext=.png}]{standalone}
\usetikzlibrary{calc}
%https://tex.stackexchange.com/questions/471741/draw-solution-curves-of-a-differential-equation-with-tikz?noredirect=1&lq=1
%https://tex.stackexchange.com/questions/294686/ifthenelse-inside-tikz-not-working
\begin{document}
	\begin{tikzpicture}[declare function={f(\x,\y)=-.5*\x +12* \y;}]
	\def\xmax{8.2} 
	\def\xmin{0}
	\def\ymax{5} 
	\def\ymin{0}
	\def\xini{0} 
	\def\uhot{3.5} 
	\def\ucold{0}
	\def\tmax{8.2}
	\def\tmin{0}
	\pgfmathsetmacro{\stept}{0.01}
	\pgfmathsetmacro{\tfin}{\tmax+0.1}
	\pgfmathtruncatemacro{\nsteps}{ceil((\tfin-\tmin)/\stept)}
	\pgfmathsetmacro{\nextt}{\tmin+\stept}
	\pgfmathsetmacro{\nextnextt}{\tmin+2*\stept}
	\pgfmathsetmacro{\tref}{\tmin}
	\pgfmathsetmacro{\u}{\uhot}
	\pgfmathsetmacro{\tcold}{0.2}
	\edef\lstX{(\tmin,\xini)}
	\edef\arrX{"(\tmin,\xini)"}
	\edef\lstY{(\tmin,\xini)}
	\pgfmathsetmacro{\myy}{\xini}
	\edef\lastmainflag{1}
	\pgfmathsetmacro{\tref}{0.4}
	\edef\iloop{1}
	\loop
	  \pgfmathsetmacro{\x}{\xmin+\stept*\iloop}
	  \pgfmathtruncatemacro{\mainflag}{((\x - \tref) < 0.00001 ? 1 : 0)}
	  \unless\ifnum\mainflag=\lastmainflag
	    \ifnum\mainflag=1
		  \pgfmathsetmacro{\tmpx}{\x-\stept}
	      \edef\lstY{\lstY (\tmpx,\myy)}
		\fi  
	  \fi
	  \ifnum\mainflag=1
		\pgfmathsetmacro{\tref}{\x}
		\pgfmathsetmacro{\myy}{\myy+f(\myy,\uhot)*\stept}
		\edef\lstX{\lstX (\x,\myy)}
		\edef\arrX{\arrX,"(\x,\myy)"}
	  \else
		\pgfmathtruncatemacro{\rflag}{((\x - \tref - \tcold) < 0.00001 ? 1 : 0)}
		\ifnum\rflag=0
		   \pgfmathsetmacro{\tref}{\x + 0.4}
		\else   
		\fi
		\pgfmathsetmacro{\myy}{\myy+f(\myy,\ucold)*\stept}
		\edef\lstX{\lstX (\x,\myy)}
		\edef\arrX{\arrX,"(\x,\myy)"}
	  \fi
	  \edef\lastmainflag{\mainflag}
	  \edef\iloop{\the\numexpr\iloop+1}
	  \ifnum\iloop<\nsteps
	\repeat
	\pgfmathsetmacro{\myres}{{\arrX}[7]}
	\typeout{7th entry: \myres}
	\draw[red,ultra thick] plot[smooth] coordinates {\lstX};
	\draw[blue,const plot,thick] plot coordinates {\lstY};
	% grid
	\draw (\xmin,\ymin) rectangle ($(\xmax,\ymax)+(1mm,1mm)$);
	% ticks
	\foreach \i in {0,1,...,\xmax} 
	\draw (\i,\ymin) node[below]{$\i$}--++(90:.5mm); 
	\foreach \i in {0,\ymax}
	\draw (\xmin,\i) node[left]{$\i$}--++(0:.5mm);
	\end{tikzpicture}
\end{document}
```



![Screen Shot 2020-03-13 at 9.01.28 PM.png](/image?hash=7d9a8c59aa0ef702335a5b9ddb1137d6cd53e22e085bf41ede45bbc469faa2a9)

As you can see, this now depicts a staircase.

With `pgfplots`.

```
\documentclass[border=5mm,tikz,convert={density=1200,outext=.png}]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\usetikzlibrary{calc}
%https://tex.stackexchange.com/questions/471741/draw-solution-curves-of-a-differential-equation-with-tikz?noredirect=1&lq=1
%https://tex.stackexchange.com/questions/294686/ifthenelse-inside-tikz-not-working
\begin{document}
  \begin{tikzpicture}[declare function={f(\x,\y)=-.5*\x +12* \y;}]
	\def\xmax{8.2} 
	\def\xmin{0}
	\def\ymax{5} 
	\def\ymin{0}
	\def\xini{0} 
	\def\uhot{3.5} 
	\def\ucold{0}
	\def\tmax{8.2}
	\def\tmin{0}
	\pgfmathsetmacro{\stept}{0.01}
	\pgfmathsetmacro{\tfin}{\tmax+0.1}
	\pgfmathtruncatemacro{\nsteps}{ceil((\tfin-\tmin)/\stept)}
	\pgfmathsetmacro{\nextt}{\tmin+\stept}
	\pgfmathsetmacro{\nextnextt}{\tmin+2*\stept}
	\pgfmathsetmacro{\tref}{\tmin}
	\pgfmathsetmacro{\u}{\uhot}
	\pgfmathsetmacro{\tcold}{0.2}
	\edef\lstX{(\tmin,\xini)}
	\edef\arrX{"(\tmin,\xini)"}
	\edef\lstY{(\tmin,\xini)}
	\pgfmathsetmacro{\myy}{\xini}
	\edef\lastmainflag{1}
	\pgfmathsetmacro{\tref}{0.4}
	\edef\iloop{1}
	\loop
	  \pgfmathsetmacro{\x}{\xmin+\stept*\iloop}
	  \pgfmathtruncatemacro{\mainflag}{((\x - \tref) < 0.00001 ? 1 : 0)}
	  \unless\ifnum\mainflag=\lastmainflag
	    \ifnum\mainflag=1
		  \pgfmathsetmacro{\tmpx}{\x-\stept}
	      \edef\lstY{\lstY (\tmpx,\myy)}
		\fi  
	  \fi
	  \ifnum\mainflag=1
		\pgfmathsetmacro{\tref}{\x}
		\pgfmathsetmacro{\myy}{\myy+f(\myy,\uhot)*\stept}
		\edef\lstX{\lstX (\x,\myy)}
		\edef\arrX{\arrX,"(\x,\myy)"}
	  \else
		\pgfmathtruncatemacro{\rflag}{((\x - \tref - \tcold) < 0.00001 ? 1 : 0)}
		\ifnum\rflag=0
		   \pgfmathsetmacro{\tref}{\x + 0.4}
		\else   
		\fi
		\pgfmathsetmacro{\myy}{\myy+f(\myy,\ucold)*\stept}
		\edef\lstX{\lstX (\x,\myy)}
		\edef\arrX{\arrX,"(\x,\myy)"}
	  \fi
	  \edef\lastmainflag{\mainflag}
	  \edef\iloop{\the\numexpr\iloop+1}
	  \ifnum\iloop<\nsteps
	\repeat
	\begin{axis}
	  \addplot[smooth,mesh,ultra thick] coordinates {\lstX};
	  \addplot[blue,const plot,thick]  coordinates {\lstY};
	\end{axis}
  \end{tikzpicture}
\end{document}
```

![Screen Shot 2020-03-14 at 9.15.55 AM.png](/image?hash=426887e108d13b4c29987a0359c9d9ecf6364eae3c9619786abc56416a8d1208)
Answer #2
Raaja
The working solution:

```
%&lualatex
% !TeX TXS-program:compile = txs:///lualatex/[--shell-escape]
\documentclass[border=5mm,tikz,convert={density=1200,outext=.png}]{standalone}
\usepackage{ifthen}
\usetikzlibrary{calc}
%https://tex.stackexchange.com/questions/471741/draw-solution-curves-of-a-differential-equation-with-tikz?noredirect=1&lq=1
%https://tex.stackexchange.com/questions/294686/ifthenelse-inside-tikz-not-working
\begin{document}
	\begin{tikzpicture}[declare function={f(\x,\y)=(-.5*\x) + \y;}]
	\def\xmax{8.2} \def\xmin{0}
	\def\ymax{5} \def\ymin{0}
	\def\xini{0} \def\uhot{3.5} \def\ucold{0}
	\def\tmax{8.2}\def\tmin{0}
	%	\def\nx{15} % \def\ny{15}
	
	%	\pgfmathsetmacro{\hx}{(\xmax-\xmin)/\nx}
	%	\pgfmathsetmacro{\hy}{(\ymax-\ymin)/\ny}
	%	\foreach \i in {0,...,\nx}
	%	\foreach \j in {0,...,\ny}{
	%		\draw[teal,-stealth] 
	%		({\xmin+\i*\hx},{\ymin+\j*\hy}) -- ++ ({atan2(f({\xmin+\i*\hx},{\ymin+\j*\hy}),1)}:0.1);
	%	}
	\pgfmathsetmacro{\stept}{0.01}
	\pgfmathsetmacro{\nextt}{\tmin+\stept}
	\pgfmathsetmacro{\nextnextt}{\tmin+2*\stept}
	\pgfmathsetmacro{\tfin}{\tmax+0.1}
	\pgfmathsetmacro{\tref}{\tmin}
	\pgfmathsetmacro{\u}{\uhot}
	\pgfmathsetmacro{\tcold}{0.2}
	\xdef\lstX{(\tmin,\xini)}
	\pgfmathsetmacro{\myy}{\xini}
	\pgfmathsetmacro{\rflag}{1}
	\xdef\rflag{\rflag}
	\pgfmathsetmacro{\tref}{0.4}
	\foreach \x in {\nextt,\nextnextt,...,\tfin}
		{
			\pgfmathparse{(\x - \tref) < 0.00001 ? int(1) : int(0)}
			\pgfmathsetmacro{\rflag}{\pgfmathresult}
			\xdef\rflag{\rflag}
			\ifnum \rflag = 1
			{	
				\pgfmathsetmacro{\tref}{\x}
				\pgfmathsetmacro{\myy}{\myy+f(\myy,\uhot)*\stept}
				\xdef\myy{\myy}
				\xdef\lstX{\lstX (\x,\myy)}
			}
			\else
			{	
				\pgfmathparse{(\x - \tref - \tcold) < 0.00001 ? int(1) : int(0)}
				\pgfmathsetmacro{\rflag}{\pgfmathresult}
				\ifnum \rflag = 0
					{
						\pgfmathsetmacro{\rflag}{1}
						\xdef\rflag{\rflag}
						\pgfmathparse{\x + 0.4}
						\pgfmathsetmacro{\tref}{\pgfmathresult}
						\xdef\tref{\tref}
					}
			    \fi
				\pgfmathsetmacro{\myy}{\myy+f(\myy,\ucold)*\stept}
				\xdef\myy{\myy}
				\xdef\lstX{\lstX (\x,\myy)}
			}
			\fi
%			\draw[red,thick] plot[smooth] coordinates {(\tref, 2) (\tref, 3)};
		}
	\draw[red,ultra thick] plot[smooth] coordinates {\lstX};
	% grid
	\draw (\xmin,\ymin) rectangle ($(\xmax,\ymax)+(1mm,1mm)$);
	%	\draw (current bounding box.north) node[above=5mm]{$y'=x^2+y^2-1$.};
	% ticks
	\foreach \i in {0,1,...,\xmax} 
	\draw (\i,\ymin) node[below]{$\i$}--++(90:.5mm); 
	\foreach \i in {0,\ymax}
	\draw (\xmin,\i) node[left]{$\i$}--++(0:.5mm);
	\end{tikzpicture}
\end{document}
```

Enter question or answer id or url (and optionally further answer ids/urls from the same question) from

Separate each id/url with a space. No need to list your own answers; they will be imported automatically.