Math & Sciencepgfplots | Conditions on functions

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

pgfplots | Conditions on functions

Post by LaTeX Beginner »

Hi guys!

I have a function defined in LaTeX, and I want to use this function inside other one, in this way:

Code: Select all

\def\A{...}
\ded\B{A<0}
Mi doubt is the next: How can I express in LaTeX the condition A<... or A>...? I mean, I want to use the "negative side" (A<0) of this function.

Thank you so much! :)
Last edited by cgnieder on Thu Oct 11, 2012 7:55 pm, edited 2 times in total.

Recommended reading 2024:

LaTeXguide.org • LaTeX-Cookbook.net • TikZ.org

Learn LaTeX easily with newest books:

The LaTeX Beginner's Guide: 2nd edition and perfect for students writing a thesis

The LaTeX Cookbook: 2nd edition full of practical examples for mathematics, physics, chemistry, and more

LaTeX Graphics with TikZ: the first book about TikZ for perfect drawings in your LaTeX thesis

User avatar
Stefan Kottwitz
Site Admin
Posts: 10359
Joined: Mon Mar 10, 2008 9:44 pm

pgfplots | Conditions on functions

Post by Stefan Kottwitz »

The etoolbox package provides such tests, have a look at section 3.6.4 Arithmetic Tests in the etoolbox documentation.

What do you plan to do? You said negative side of a function, would you like to plot functions, or what is the plan?

Stefan
LaTeX.org admin
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

pgfplots | Conditions on functions

Post by LaTeX Beginner »

Stefan_K wrote:The etoolbox package provides such tests, have a look at section 3.6.4 Arithmetic Tests in the etoolbox documentation.

What do you plan to do? You said negative side of a function, would you like to plot functions, or what is the plan?

Stefan
Hi Stefan, thanks for the reply.

I've plotted a function, and this function has a negative part and a positive part. So I want to define a new function (B) that only takes the negative values of A, in order to operate with it. Do you understand me?
User avatar
Stefan Kottwitz
Site Admin
Posts: 10359
Joined: Mon Mar 10, 2008 9:44 pm

pgfplots | Conditions on functions

Post by Stefan Kottwitz »

Sure, but it's quite theoretical. Please post a Infominimal working example. So we have something to test and to implement, and we can see if you work with gnuplot, PSTricks, TikZ, pgfplots, or something else.

Stefan
LaTeX.org admin
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

pgfplots | Conditions on functions

Post by LaTeX Beginner »

Yeah, sure, I post here an example:

Code: Select all

\documentclass[11pt,a4paper,spanish]{article}
\usepackage[T1]{fontenc}
\usepackage{selinput}
\usepackage{babel}
\usepackage{geometry}
\usepackage{tikz}
\usepackage{textcomp}
\usepackage{pgfplots}

\usepgfplotslibrary{polar}
\usepgfplotslibrary[polar]
\usetikzlibrary{pgfplots.polar}
\usetikzlibrary[pgfplots.polar]

\begin{document}
\begin{center}
\pgfplotsset{width=15cm,compat=1.5.1}
\begin{tikzpicture}

%%%%%%%%%%% CARTESIAN COORDINATES

%\begin{axis}

%\addplot+[red,domain=0:360,samples=360] {sin(\x)};

%\end{axis}

%%%%%%%%%%%

\begin{polaraxis}[title=\large\bfseries{Polar coordinates}]
				
\addplot+[red,domain=0:360,samples=360] {sin(\x)}; 						

\end{polaraxis}
\end{tikzpicture}
\end{center}
\end{document} 

This is an example, a sin function representation. If you run it, you'll get a sin(x) function in polar coordinates (the same example in cartesian coordinates is commented). The sin function oscillates between -1 and 1. So, I want to define a new function (B, for example), that only takes the negative values of sin(x), these values for which sin(x)<0

I hope I explained it well :D
User avatar
Stefan Kottwitz
Site Admin
Posts: 10359
Joined: Mon Mar 10, 2008 9:44 pm

Re: pgfplots | Conditions on functions

Post by Stefan Kottwitz »

Ok, that's something clearer. So I changed the topic title from "Use of "<" or ">" in LaTeX" to "pgfplots | Conditions on functions", to make clear it's not about printing those < and > symbols or the like, and in the hope that this is noticed by a pgfplots expert.

Stefan
LaTeX.org admin
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Re: pgfplots | Conditions on functions

Post by LaTeX Beginner »

Thanks Stefan. I hope someone can help me :)
User avatar
cgnieder
Site Moderator
Posts: 2000
Joined: Sat Apr 16, 2011 7:27 pm

pgfplots | Conditions on functions

Post by cgnieder »

You can exploit the fact that the mathematical expression in

Code: Select all

\addplot {<expr>} ;
can be any valid pgfmath expression (pgfplots also refers to the pgfmanual).

One of the expression available is the “ifthenelse” expression

Code: Select all

 <if> ? <then> : <else>
So this is a way to go:

Code: Select all

\documentclass{article}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
 \begin{axis}[mark=none]
   \addplot[red,samples=100]   { x^2 } ;
   \addplot[green,samples=300] { x^2 > 4 ? -x^2+8 : x^2 } ;
 \end{axis}
\end{tikzpicture}

\end{document}
ifthenelse.png
ifthenelse.png (7.47 KiB) Viewed 10157 times
Regards
site moderator & package author
LaTeX Beginner
Posts: 30
Joined: Tue Sep 11, 2012 2:55 pm

Re: pgfplots | Conditions on functions

Post by LaTeX Beginner »

Thank you so much, Clemens :)

Tomorrow I will try it, and I tell you.

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

Re: pgfplots | Conditions on functions

Post by LaTeX Beginner »

I tried it, and it works very well! I don't knew how to use the if?then:else structure, and I think that it could be very useful to me

But I've realised that I didn't explain my doubt well... Taking the sin(x) example, you can see that it has a negative part (-1:0) and a positive part (0:1). If I want to operate in this way:

A=(sin(\x)*(sin(\x)>0))

How can I do that? I want to operate with the sin(\x) function and with his positive part(sin(\x)>0)

Thank you again! :D
Post Reply