簡體   English   中英

如何通過初始化除一個變量以外的所有變量來繪制R中多個變量的函數

[英]How to plot function of multiple variables in R by initializing all variables but one

通過給某些輸入參數一個值,在2D圖形中繪制多個變量的函數的最簡單方法是什么。 讓我們考慮一個簡單的例子

my.function<-function(a,b,x){a*x^2+b}

現在,我要繪制簡單拋物線,其中a=1b=0 因此,我定義了一個新函數: new.function<-function(x){my.function(1,0,x)}; plot(new.function) new.function<-function(x){my.function(1,0,x)}; plot(new.function)

有什么方法可以在不定義new.function情況下繪制函數?

通常我使用Mathematica,而在Mathematica中,它將是:

Plot[my.function[1,0,x],{x ... }]

您無需定義新功能。 您可以使用原始函數my.function ,並傳入x值1到10來繪制拋物線:

my.function <- function(a,b,x){a*x^2+b}
x <- 1:10
y <- my.function(a=1,b=0,x=x)
plot(y~x)

對於將來的讀者,我在這里分享@ G5W的評論作為答案:

plot(function(x) { my.function(1,0,x) })

我相信這是這里提供的最好方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM