用R畫圖來檢驗(yàn)y和某變量之間的關(guān)系(增函數(shù)/減函數(shù))

# 加載 ggplot2 庫

library(ggplot2)

# 固定參數(shù)賦值

# 定義表達(dá)式 y 的函數(shù)

y_function <- function(t,n) {

? Ka <- 0.1

? F <- 0.8

? Dose <- 3

? ktr <- 0.5

? Input_Rate<- Ka*F*Dose*((ktr * t)^n / factorial(n)) * exp(-ktr*t)

}

# 定義時(shí)間范圍

t <- seq(0, 24, by = 0.5)? # 時(shí)間范圍從 0 到 5,步長為 0.01

# 定義 n 的取值范圍

n_values <- c(0, 1, 2, 3, 4, 5,6,7,8)? # n 的取值

# 創(chuàng)建數(shù)據(jù)框以便 ggplot 使用

data <- data.frame()

for (n in n_values) {

? y <- sapply(t, y_function, n = n)? # 計(jì)算不同 t 下的 y 值

? temp_data <- data.frame(t = t, y = y, n = factor(n))? # 將 n 轉(zhuǎn)換為因子類型

? data <- rbind(data, temp_data)? # 合并數(shù)據(jù)

}

# 使用 ggplot 繪制圖像

ggplot(data, aes(x = t, y = y, color = n)) +

? geom_line(size = 1) +? # 繪制曲線

? labs(title = "y vs t for different n", x = "time", y = "Input Rate", color = "n") +? # 添加標(biāo)題和標(biāo)簽

? theme_minimal() +? # 使用簡潔主題

? scale_color_brewer(palette = "Set1")? # 設(shè)置顏色樣式

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容