You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

求R Studio绘制多温度黑体辐射曲线及λmax的基础脚本

R 脚本:Planck黑体辐射函数计算与光谱绘图

以下是直接可用的基础脚本,包含Planck函数计算、多温度辐射光谱绘制,以及Wien位移定律的峰值波长标注:

# 安装并加载ggplot2(如果未安装)
if (!require("ggplot2")) {
  install.packages("ggplot2")
  library(ggplot2)
}

# 定义Planck函数:输入波长(μm)和温度(K),返回光谱辐射出射度(W/(m²·μm))
planck_function <- function(lambda, T) {
  # 物理常数
  h <- 6.626e-34  # 普朗克常数,J·s
  c <- 2.998e8    # 光速,m/s
  k <- 1.381e-23  # 玻尔兹曼常数,J/K
  
  # 转换波长单位:μm转m
  lambda_m <- lambda * 1e-6
  
  # Planck公式计算(光谱辐射出射度,单位W/(m²·m))
  numerator <- 2 * h * c^2
  denominator <- lambda_m^5 * (exp(h * c / (lambda_m * k * T)) - 1)
  spectral_radiance <- numerator / denominator
  
  # 转换单位为W/(m²·μm)
  spectral_radiance_um <- spectral_radiance * 1e-6
  
  return(spectral_radiance_um)
}

# 定义Wien位移定律:计算λmax(μm)
wien_lambda_max <- function(T) {
  b <- 2898  # Wien位移常数,μm·K
  return(b / T)
}

# 设置需要计算的温度列表
temperatures <- c(290, 1250, 1800, 2600, 5000, 6000)

# 设置波长范围:0.1到20μm(覆盖所有温度的辐射峰值)
lambda_range <- seq(0.1, 20, by = 0.01)

# 计算各温度下的辐射值,整理为数据框
radiation_data <- data.frame()
for (T in temperatures) {
  radiance <- planck_function(lambda_range, T)
  temp_df <- data.frame(
    Wavelength = lambda_range,
    Radiance = radiance,
    Temperature = as.factor(T)
  )
  radiation_data <- rbind(radiation_data, temp_df)
}

# 计算各温度的λmax
lambda_max_data <- data.frame(
  Temperature = as.factor(temperatures),
  Lambda_max = sapply(temperatures, wien_lambda_max)
)

# 绘制光谱曲线+λmax垂直线
ggplot(radiation_data, aes(x = Wavelength, y = Radiance, color = Temperature)) +
  geom_line(linewidth = 1) +
  # 添加Wien位移的λmax垂直线
  geom_vline(data = lambda_max_data, aes(xintercept = Lambda_max, color = Temperature), 
             linetype = "dashed", linewidth = 0.8) +
  # 添加λmax标注
  geom_text(data = lambda_max_data, 
            aes(x = Lambda_max, y = planck_function(Lambda_max, as.numeric(as.character(Temperature))),
                label = paste0("λmax = ", round(Lambda_max, 2), "μm")),
            hjust = -0.1, vjust = 1, color = "black") +
  # 设置坐标轴标签和标题
  labs(
    x = "波长 (μm)",
    y = "光谱辐射出射度 (W/(m²·μm))",
    title = "不同温度下的黑体辐射光谱",
    color = "温度 (K)"
  ) +
  # 设置主题,让图形更清晰
  theme_minimal() +
  theme(
    plot.title = element_text(hjust = 0.5, size = 14, face = "bold"),
    axis.title = element_text(size = 12),
    legend.position = "right"
  ) +
  # 设置y轴为对数刻度(可选,方便观察低辐射区域)
  scale_y_log10()

脚本说明

  1. 依赖包处理:自动检查并安装ggplot2,无需手动操作
  2. Planck函数:严格遵循物理公式,内置单位转换,输出常用的辐射强度单位
  3. Wien位移计算:一行代码实现峰值波长求解,常数取值符合标准
  4. 数据准备:循环计算多温度辐射数据,整理为ggplot2兼容的数据格式
  5. 绘图细节:
    • 实线绘制辐射光谱,虚线标记峰值波长
    • 自动标注每个温度的λmax数值
    • 对数y轴可清晰展示低辐射区域,不需要可删除scale_y_log10()行

内容的提问来源于stack exchange,提问作者nancy sawaged

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 08:23:55