Django
Django

Reputation: 103

How to plot a curve line with X and Y as a parameter in R

My equation for the curve line is

equation = function(x+y+sin(x)+cos(y))

How to plot the graph for this equation?

Upvotes: 1

Views: 95

Answers (2)

Qaswed
Qaswed

Reputation: 3879

Asuming that your arguments going into your function are x and y and what the function returns is x + y + sin(x) + cos(y), my solution is the following:

f <- function(x, y) x + y + sin(x) + cos(y)

x <- seq(from = -5, to = 5, length = 100)
y <- seq(from = -5, to = 5, length = 100)
z <- f(x, y)

library(plotly)
plot_ly(x = x, y = y, z = z)

Using persp (what @李哲源 Zheyuan Li suggested) would mean running (the parameters of course must be adjusted according your preferences):

z <- outer(x, y, f)
persp(x, y, z, theta = 30, phi = 30, expand = 0.5, col = "lightblue")

Upvotes: 0

Zheyuan Li
Zheyuan Li

Reputation: 73375

Just a start:

x <- seq(-1, 1, length = 20)
y <- seq(-1, 1, length = 20)
z <- outer(x, y, function (x, y) x + y + sin(x) + cos(y))
persp(x, y, z)

Upvotes: 2

Related Questions