 # multiline functions that I can then plot

#1

I've written a multiline function using LaTex, but then I can't seem to work out how to plot it.

f(x) = \begin{cases} \frac{12}{7}(1+x)^2 & \quad \text{for} \; -1< x \leq 0\\ \frac{12}{7}(1-x)^3 & \quad \text{for} \; 0< x < 1\\ 0 & \quad \text{otherwise} \end{cases}

I don't know where to go from here!
Any help is greatly appreciated.
Thank you

0 Likes

#2

Welcome to the community!

Can you please explain what you are trying to do? This is not clear to me from your question.

If you want to write the function in a document, PDF or HTML, then that has nothing to do with plotting the function. On the other hand, if you want to plot the function for different values of the argument x, then you don't need to write it in latex.

Can you please share a little bit more specifics of your problem? It'll be better if you can provide a REPRoducible EXample of your problem?

In case you don't know how to make a reprex, here's a great link:

0 Likes

#3

Thank you so much for the response. I'm just trying to plot the function for the different values of x as you've mentioned. I just don't know how to do it for multiline functions like the one in this case. Especially with the differing domains.

Your help is greatly appreciated.

0 Likes

#4

Are you new to programming? In this situations, the if - else if - else is almost always the most obvious solution. Add the first case under if, and in case there are more, add those conditions one by one under else if. If all the cases are collectively exhaustive, you can use else in the final one, as there's only one possibility left.

See below:

f <- function(x)
{
if ((-1 < x) & (x <= 0))
{
return((12 / 7) * ((1 + x) ^ 2))
} else if ((0 < x) & (x < 1))
{
return((12 / 7) * ((1 + x) ^ 3))
} else
{
return(0)
}
}

x_vals <- seq(from = -2,
to = 2,
by = 0.01)

y_vals <- sapply(X = x_vals,
FUN = f)

plot(x = x_vals,
y = y_vals) Created on 2019-04-01 by the reprex package (v0.2.1)

I didn't join the points, you can do that by specifying type="l". Hope this helps.

PS I'm changing the category of your post, as it doesn't relate to either R Markdown or latex. Hope you don't mind.

PPS You can write LaTeX equations within a pair of  in R markdown. Check my edit in the question.

0 Likes

closed #5

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes