【发布时间】:2017-03-27 00:06:33
【问题描述】:
我有一个问题,经过多次头疼后,我认为与长双打中非常小的数字有关。
我正在尝试实现普朗克定律方程,以在给定波长范围和给定温度之间以 1nm 的间隔生成归一化黑体曲线。最终这将是一个接受输入的函数,现在它是 main(),变量固定并由 printf() 输出。
我在matlab 和python 中看到了示例,它们在类似的循环中执行与我相同的等式,完全没有问题。
这是equation:
我已经独立测试了代码的关键部分。在尝试通过在 excel 中将方程分解成块来测试方程后,我注意到它确实会产生非常小的数字,我想知道我对大数字的实现是否会导致问题?有没有人对使用 C 来实现方程有任何见解?这对我来说是一个新领域,我发现数学比普通代码更难实现和调试。
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
//global variables
const double H = 6.626070040e-34; //Planck's constant (Joule-seconds)
const double C = 299800000; //Speed of light in vacume (meters per second)
const double K = 1.3806488e-23; //Boltzmann's constant (Joules per Kelvin)
const double nm_to_m = 1e-6; //conversion between nm and m
const int interval = 1; //wavelength interval to caculate at (nm)
//typedef structure to hold results
typedef struct {
int *wavelength;
long double *radiance;
long double *normalised;
} results;
int main() {
int min = 100 , max = 3000; //wavelength bounds to caculate between, later to be swaped to function inputs
double temprature = 200; //temprature in kelvin, later to be swaped to function input
double new_valu, old_valu = 0;
static results SPD_data, *SPD; //setup a static results structure and a pointer to point to it
SPD = &SPD_data;
SPD->wavelength = malloc(sizeof(int) * (max - min)); //allocate memory based on wavelength bounds
SPD->radiance = malloc(sizeof(long double) * (max - min));
SPD->normalised = malloc(sizeof(long double) * (max - min));
for (int i = 0; i <= (max - min); i++) {
//Fill wavelength vector
SPD->wavelength[i] = min + (interval * i);
//Computes radiance for every wavelength of blackbody of given temprature
SPD->radiance[i] = ((2 * H * pow(C, 2)) / (pow((SPD->wavelength[i] / nm_to_m), 5))) * (1 / (exp((H * C) / ((SPD->wavelength[i] / nm_to_m) * K * temprature))-1));
//Copy SPD->radiance to SPD->normalised
SPD->normalised[i] = SPD->radiance[i];
//Find largest value
if (i <= 0) {
old_valu = SPD->normalised[0];
} else if (i > 0){
new_valu = SPD->normalised[i];
if (new_valu > old_valu) {
old_valu = new_valu;
}
}
}
//for debug perposes
printf("wavelength(nm) radiance(Watts per steradian per meter squared) normalised radiance\n");
for (int i = 0; i <= (max - min); i++) {
//Normalise SPD
SPD->normalised[i] = SPD->normalised[i] / old_valu;
//for debug perposes
printf("%d %Le %Lf\n", SPD->wavelength[i], SPD->radiance[i], SPD->normalised[i]);
}
return 0; //later to be swaped to 'return SPD';
}
/*********************更新 2017 年 3 月 24 日星期五 23:42**************** ********/
到目前为止,感谢您的建议,许多有用的指针特别是了解数字在 C 中的存储方式 (IEEE 754) 但我不认为这是这里的问题,因为它只适用于有效数字。我实施了大部分建议,但在这个问题上仍然没有进展。我怀疑 cmets 中的 Alexander 可能是对的,改变单位和操作顺序可能是我需要做的才能使方程像 matlab 或 python 示例一样工作,但我的数学知识不足以做到这一点。我把方程分解成几块,仔细看看它在做什么。
//global variables
const double H = 6.6260700e-34; //Planck's constant (Joule-seconds) 6.626070040e-34
const double C = 299792458; //Speed of light in vacume (meters per second)
const double K = 1.3806488e-23; //Boltzmann's constant (Joules per Kelvin) 1.3806488e-23
const double nm_to_m = 1e-9; //conversion between nm and m
const int interval = 1; //wavelength interval to caculate at (nm)
const int min = 100, max = 3000; //max and min wavelengths to caculate between (nm)
const double temprature = 200; //temprature (K)
//typedef structure to hold results
typedef struct {
int *wavelength;
long double *radiance;
long double *normalised;
} results;
//main program
int main()
{
//setup a static results structure and a pointer to point to it
static results SPD_data, *SPD;
SPD = &SPD_data;
//allocate memory based on wavelength bounds
SPD->wavelength = malloc(sizeof(int) * (max - min));
SPD->radiance = malloc(sizeof(long double) * (max - min));
SPD->normalised = malloc(sizeof(long double) * (max - min));
//break equasion into visible parts for debuging
long double aa, bb, cc, dd, ee, ff, gg, hh, ii, jj, kk, ll, mm, nn, oo;
for (int i = 0; i < (max - min); i++) {
//Computes radiance at every wavelength interval for blackbody of given temprature
SPD->wavelength[i] = min + (interval * i);
aa = 2 * H;
bb = pow(C, 2);
cc = aa * bb;
dd = pow((SPD->wavelength[i] / nm_to_m), 5);
ee = cc / dd;
ff = 1;
gg = H * C;
hh = SPD->wavelength[i] / nm_to_m;
ii = K * temprature;
jj = hh * ii;
kk = gg / jj;
ll = exp(kk);
mm = ll - 1;
nn = ff / mm;
oo = ee * nn;
SPD->radiance[i] = oo;
}
//for debug perposes
printf("wavelength(nm) | radiance(Watts per steradian per meter squared)\n");
for (int i = 0; i < (max - min); i++) {
printf("%d %Le\n", SPD->wavelength[i], SPD->radiance[i]);
}
return 0;
}
xcode 中运行时的方程变量值:
【问题讨论】:
-
顺便说一句,为什么
nm_to_m等于 1e-6 ?如果是纳米到米,那么不应该是 1e-9 吗? -
你完全正确,一个错字。这显然会影响数字的大小,但不幸的是方向错误,我现在在长双精度数组中有很多“inf”和“nan”
-
与您的问题无关,但我发现您使用 10 个有效数字表示普朗克常数,而仅用 4 个有效数字表示光速。
-
IEEE 浮点数只有 18 位精度。你知道的,对吧?
-
你说你的图表是错误的,但我不清楚为什么它是错误的。将其与the one at Wikipedia 进行比较并非易事,即使单位不同。你能解释一下为什么你说它是错的吗?是不是太高了?太低?曲线太陡?您正在计算 T = 200 K,但 Wiki 上的图表从 T = 3000 K 开始。您是否期望那里的红线(一种倾斜的高斯)而不是负指数?
标签: c math long-double