How do you make an equation representation of a computer code if/then statement, for graphing purposes?

55 Views Asked by At

Consider a graph of an equation like y = sin(x).

Now suppose that I want to have a single equation where y gets the same value that it would get in the following computer code:

if (y - sin(x) <= 45) {
    y = sin(x);
}
else {
    y = 0;
}

How would I mathematically build the above logic into a single math equation with no externally defined functions, for graphing (or really any other purpose)?

Note: this is a question about standard math notation, not a particular calculator.

3

There are 3 best solutions below

3
On

I don't know how calculators behave, but we can use indicator functions for this: $$y := x 1[y-x \leq 45] + y 1[y-x > 45]$$ where $1[\phi]$ is defined to be zero if $\phi$ is false, and $1$ if $\phi$ is true.

1
On

In MATLAB: u =(y-x < 45)*x +(y-x>= 45)*sin(x);

0
On

\begin{equation} y=\begin{cases} \sin(x), & \text{if $(y - \sin(x) \leq 45)$}.\\ 0, & \text{otherwise}. \end{cases} \end{equation}