提问人:jbadon 提问时间:11/10/2023 最后编辑:JaMiTjbadon 更新时间:11/11/2023 访问量:51
使用第 n 个根计算 R
calculating r using nth root
问:
我正在尝试计算特定时间的 r 或 f(t_max)。我使用的是从 0 到 255 的刻度。因此,我正在尝试计算 t_max = 255 时指数增长的正确速率值,初始值为 500,最大值为 3500。
指数级增长: f(x) = a(1+r)^x
想要求解 r:
3500 = 500(1+r)^255
3500/500 = (1+r)^255
7 = (1+r)^255
255 n_root(7) = (1+r)
1.00766 - 1 = r
我找到了这篇文章,并且能够通过创建自己的函数来让它工作,但我想知道是否已经有一个函数可以在不重新发明轮子的情况下计算它?
谷歌计算器如何表达这个数学:在这里输入图像描述
这可行,但有更好的方法吗?
/*从 stackoverflow post 构建:在 C++ 中查找数字的第 n 个根 */
Exponential_Growth.h:
/*Built from stackoverflow post:
https://stackoverflow.com/questions/21141447/find-nth-root-of-a-number-in-c?newreg=3a7ef9dc2a444e01b0a25ac3cc4a83d1
*/
#include <iostream>
#include <math.h>
using namespace std;
double exp(double, double);
double n_root_(double, double);
double exp(double a, double b){
double t(1);
for(int i = 0;i<b;++i)
t *= a;
return t;
}
double n_root_(double num, double n_){
double x;
double A(num);
double dx;
double eps(10e-6);
double n(n_);
x = A * 0.5;
dx = (A/exp(x,n-1)-x)/n;
while(dx >= eps || dx <= -eps){
x = x + dx;
dx = (A/exp(x,n-1)-x)/n;
}
return x;
}
double find_rate(double initial_value, double final_value, double t_max){
double root_value_ratio = (final_value / initial_value);
return ((n_root_(root_value_ratio, t_max) - 1));
}
double exponential_growth_value_at_t(double initial_value, double final_value, double t){
double r = find_rate(initial_value, final_value, t);
double egvat = (initial_value * pow((1 + r), t));
return egvat;
}
用途:
//Per my previous stated values
double initial_value = 500;
double final_value = 3500;
double t_max = 255
t = 128;
fr = find_rate(initial_value, final_value, t_max);
fr = 0.00766021063
egvt = exponential_growth_value_at_t(initial_value, final_value, 128);
fr = 0.00766021063 EGVT = 1327.9327
t = 128 时的增长:1327.9327
这有效,但不如@starsplusplus发布的准确
double exponential_growth_value_at_t(double initial_value, double final_value, double t, double t_max){
double r = (log(final_value / initial_value) / t_max);
double egvat = (initial_value * pow((1 + r), t));
return egvat;
}
我确实阅读了有关第 n 个根以解决 r 的所有搜索结果,但没有看到任何回答这个问题的内容。我在代数和计算方面做得很好,但不是数学家。
谢谢你的帮助, /retnel
答: 暂无答案
评论
std::pow
egvat
std::pow(final_value/initial_value, 1.0/t_max) - 1