How to plot a graph using a curve function

I need to solve a question of an R course. Could you help me please?

The question is as follows:

Question 2. Create a vector with the cosine of numbers between -10 and 10.

A) Plot the cosine of x (-10

B) How do you get a graph with a smooth line?

(C) Plot the same graph with a single command using the curve function.

Solved the question as follows:

Q2.

B

  • a) (this I did not quite understand what he asked for)

Code

q2 <- function (x) {
cos(x) }
Plot (q2)
  • (B) ???

  • (C)

Code:

mean(b)
sd(b) 
curve (dnorm(x, -0.08737598, 0.7360983), col = "red")

In this I do not know if I can use dnorm. But it was the only way I could make the graph from an equation.

Author: Comunidade, 2018-04-25

1 answers

Although Stackoverflow is not a place to be, in addition to showing you what you tried the question of plotting a curve may be interesting for other future questions.

The problem seems to be in your interpretation (Google made a very consistent translation from English to Portuguese) of how to use the function curve or plot .

  • a. plot the cosine of x (-10

Plot x vs cos(x). Code:

a<- seq(from=-10,to=10,by=0.5)
b<- cos(a)
plot(a,b)

This generates a dot graph. In this case I used seq, but a<- -10:10 also works, but with fewer points because the range is 1. If you use a function as data for plot (Be it your function , as you did or the library) it has the same behavior as curve, but if you put data, it looks like you use the plot. In your output, you have only a curve between 0 and 1, in the code above, points between -10 and 10 appear.

  • b. how to make a chart with a smooth curve? (Smooth = Continuous)

Let it be a continuous line, not Points. Just use the type='l' tag. Code:

plot(a,b,type='l')
  • C. plot the same plot in a single command, but using the function curve.

As you have already put, he wants the function to be used curve to generate the chart. It reproduces whatever equation (of x) you put as an argument. In your example, you are using the function dnorm that generates a normal distribution. Just replace dnorm with cos and delimit the values between -10 and 10, using from and to.

curve(cos(x),from=-10,to=10)
 1
Author: Guto, 2018-06-18 17:52:28