or
luatex tikz
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}


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)
marmot
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}
\end{axis}
\end{tikzpicture}
\end{document}


![Screen Shot 2020-03-14 at 9.15.55 AM.png](/image?hash=426887e108d13b4c29987a0359c9d9ecf6364eae3c9619786abc56416a8d1208)
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}


Solving ODE conditionally with and pgfmathparse
My stars are given already :D
Raaja
:)
It has to, see https://tex.meta.stackexchange.com/a/8507/194703.
Raaja
feel-free to edit it however you want ;)
Raaja
@marmot I will send you the final output once I am done. This is one of the most complicated plots that I have drawn so-far
Raaja
that is a cool answer!
Raaja
thanks
Raaja
right away ;)
Raaja
let me check
Raaja
ohh cool
marmot
\addplot[smooth,mesh,ultra thick] coordinates {\lstX};
See my update ;-)
Raaja
Almost there except for accessing the processed data from the \lst
But you are almost there?
because I have not really solved a differential equation in a pgfplots environment yet ;)
huhh ok good ;)
While it should be possible to do the mesh plot without pgfplots I am wondering what prevents you from just using it.
I added a way to get the nth entry of the list. (Seems like the \expandafter\edef\csname ...\endcsname trick does not work in a \loop, so I learned something and had to resort to a more brute force way.)
the one I showed you was from pgfplots ;)
Raaja
https://en.wikipedia.org/wiki/State-space_representation
Raaja
and dy/dt ;)
Raaja
@marmot yes with pgfplots I am aware of the mesh option ;) but I wanted to try it via tikz. And, the equations is f(x, u) = Ax+Bu = dx/dt
But then I do not understand \pgfmathsetmacro{\myy}{\myy+f(\myy,\uhot)*\stept}, which seems to say dy/dx=f(y,u) (and not dy/dx=f(x,u) ).
You can do such mesh pots with pgfplots easily.
Raaja
%&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)=(-0.2*\x) + \y;}, declare function={f1(\x,\y)=(-2*\x) + \y;}, declare function={f2(\x,\y)=(0*\x) + \y;}]
\def\xmax{8.11} \def\xmin{0}
\def\ymax{7} \def\ymin{0}
\def\xini{0} \def\uhot{6} \def\ucold{0}
\def\tmax{8.11}\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.005}
\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)}
\xdef\lstY{(\tmin,\xini)}
\pgfmathsetmacro{\myy}{\xini}
\pgfmathsetmacro{\myyy}{\xini}
\xdef\myyy{\myyy}
\xdef\mainflag{1}
\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}
\pgfmathsetmacro{\myyy}{\myyy+f2(\myyy,0)*\stept}
\xdef\myyy{\myyy}
\xdef\mainflag{1}
\xdef\lstX{\lstX (\x,\myy)}
\xdef\lstY{\lstY (\x,\myyy)}

}
\else
{
\pgfmathsetmacro{\myy}{\myy+f1(\myy,\ucold)*\stept}
\xdef\myy{\myy}
\xdef\lstX{\lstX (\x,\myy)}

% ratime
\pgfmathparse{abs(\x - \tref - \tcold + 0.1) < 0.001 ? int(1) : int(0)}
\pgfmathparse{ and(\pgfmathresult, \mainflag) <0.5 ? int(1) : int(0)}
\ifnum \pgfmathresult = 0
{
\pgfmathsetmacro{\myyy}{\myy+f2(\myyy,\myy)*\stept}
\xdef\myyy{\myy}
\xdef\mainflag{0}
}
\fi
\xdef\lstY{\lstY (\x,\myyy)}
% continue IWD
\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
%		    	\xdef\lstY{\lstY (\x,\myyy)}
}
\fi
%			\draw[red,thick] plot[smooth] coordinates {(\tref, 2) (\tref, 3)};
}
\draw[red,ultra thick] plot[smooth] coordinates {\lstX};
\draw[black, ultra thick] plot coordinates {\lstY};
% 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}
Raaja
may be this is clear for you?
Raaja
@marmot I just made a small update
Raaja
constplot was something new for me today
Raaja
and, thanks for the answer ;)
Raaja
and now apply an input to it via u assuming B = 1/0 to see how the evolution of ODE happens by means of switching this B, the so-called input-vector (ofcourse, I am simplifying a lots of things here). And, yeah, just assume a heater getting warmed-up based on the supply you are giving to it ;)
Raaja
now assume A = -1, then your eigenvalue is -1, thereby the ODE is convergent
Raaja
@marmot I am basically trying to 1e years about the concept of system theory and relating it to switched linear systems. Here what I am trying to recreate is basically of form \dot x = Ax+Bu
Raaja
![Capture.PNG](/image?hash=21288d0542efff48fe4d4d32c8d48aa7a44893bbc37f344a8f9a09a98d221930)
something like this an 1D mesh (I dont exactly know how to call it in TeX language)
Can you explain what "shows a mesh like growth" means? A picture that illustrates it or some rewording, I really do not understand what you want.
as in pgfplots
But thanks for the information. It would be great, if you can tell me how to ensure the plotted data shows a mesh like growth
Had to read almost a huge chapter in the manual to understand this part
after dinner :D
Just realised it
This is the usual problem: foreach starts a group and your macro is not global.