How to plot a graph using a Function curve

Asked

Viewed 73 times

0

I need to resolve a matter of an R course. But I don’t know if I did it correctly. Could you please help me?

The question is this::

Question 2. Create a vector with the Cosine of Numbers between -10 and 10.

a) Plot the Cosine of x (-10 <= x <= 10) as a Function of x.

b) How do you get a Graph with a Smooth line?

c) Plot the same Graph with a single command using the curve Function.

I resolved the matter as follows:

Q2.

b <- cos (-10:10)

  • a) (that I did not understand very well what he asked)

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 don’t know if I can use dnorm. But it was the only way I could make the graph from an equation.

1 answer

1

Although Stackoverflow is not a place for homework, besides you show what you tried the issue of plotting a curve can be interesting for other future issues.

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

  • a. Trace the cosine of x (-10 <= x <= 10) as a function of x.

Plot x vs cos(x). Code:

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

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

  • b. How to make a graph with a smooth curve? (Smooth=Continuous)

It be a continuous line, not points. Just use the tag type='l'. Code:

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

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

curve(cos(x),from=-10,to=10)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.