以R为底的曲线函数 [英] The curve function in base R

查看:72
本文介绍了以R为底的曲线函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,这可能很基本,但是我是新手。我会绘制很多曲线,因此一些建议对我会有用。

Sorry this might be basic but I am a newbie. I will be making lots of curves so some advice will be useful for me.

我有一个要绘制的函数:

I have a function which I want to plot:

f <- function(x) sum(4*sin(x*seq(1,21,2))/(pi*seq(1,21,2)))

使用

curve(f, -pi, pi, n=100)

不幸的是,这对我不起作用。请指教。
谢谢

Unfortunately ,this does not work for me. Please advise. Thanks

推荐答案

您的功能不是矢量化的。目前,它仅需单个标量输入并输出单个返回值。 曲线期望它应该能够输入要绘制的x值的向量,并且应该接收响应值的向量。最简单的解决方案是只使用 Vectorize 自动将您的函数转换为可以接受矢量输入的函数。

You function isn't vectorized. At the moment it will only take a single scalar input and output a single return value. curve expects that it should be able to feed in a vector of the x values it wants to plot for and should receive a vector of response values. The easiest solution is to just use Vectorize to automatically convert your function into one that can take vector inputs.

f2 <- Vectorize(f)
curve(f2, -pi, pi, n = 100)

但是,您可能只想直接编写该函数的矢量化版本。

However, you might just want to write a vectorized version of the function directly.

这篇关于以R为底的曲线函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆