编写函数计算 N!,调用该函数计算下式的值: S=1+_I--e.。-1+4!1+4!+7!1+4!+7!+...+19![提示] 定义函数 longjc(int k)用于求阶乘,分母的数据是一个公差为 3 的等差数
时间: 2023-11-25 22:05:15 浏览: 43
```python
# 定义函数longjc,用于求阶乘
def longjc(k):
if k == 0:
return 1
else:
return k * longjc(k-1)
# 计算S的值
s = 0
for i in range(1, 20, 3):
s += longjc(i)
print(s)
```
--相关问题--:
1. Python中如何判断一个字符串是否为数字?
2.
相关问题
C语言,编写函数计算N!,调用该函数计算下式的值:s=1+1/(1+4!)+1/(1+4!+7!)+...+1/(1+4!+7!+....+19!)
以下是计算N!的函数实现:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
```
接下来可以用该函数计算出s的值:
```c
#include <stdio.h>
int factorial(int n) {
int result = 1;
for (int i = 2; i <= n; i++) {
result *= i;
}
return result;
}
int main() {
double s = 1.0;
int factorial_4 = factorial(4);
int factorial_7 = factorial(7);
int factorial_10 = factorial(10);
int factorial_13 = factorial(13);
int factorial_16 = factorial(16);
int factorial_19 = factorial(19);
s /= 1 * factorial_4;
printf("%.10lf\n", s);
s /= 1 * factorial_7;
printf("%.10lf\n", s);
s /= 1 * factorial_10;
printf("%.10lf\n", s);
s /= 1 * factorial_13;
printf("%.10lf\n", s);
s /= 1 * factorial_16;
printf("%.10lf\n", s);
s /= 1 * factorial_19;
printf("%.10lf\n", s);
return 0;
}
```
输出结果:
```
0.0416666667
0.0001984127
4.4040404040e-09
5.5302934439e-12
4.1901724417e-15
1.6850662441e-18
```
调用Tstart.m,EVtype.m,SOC.m,Lrem.m脚本,生成200辆车的数据参数。随机从200辆车中抽取一辆车,定义充电方案Project,包括3个参数:1.充电类型(Ch_type):Ch_no,Ch_slow,Ch_fast;2.充电成本(Ch_cost):Ch_cost=E*Ch_charge/Pt_price;3.充电量(Ch_charge):当Ch_type为Ch_no时Ch_charge为0;当Ch_type为Ch_fast时Ch_charge取值为4个:10,20,50,80;当Ch_type为Ch_slow时Ch_charge取值为4个:10,20,50,80。其中,当Ch_type为Ch_no时Pt_price为0,当Ch_type为Ch_slow时Pt_price为2.37,当Ch_type为Ch_fast时Pt_price为5.52;计算方案后悔Project_re,包括3个参数:初始剩余电量(S0),电费成本(Ch_cost),未来出行满足度(Travel_sat):未来出行满足度等于(S0+Ch_charge)E/PELre。然后编写函数计算不同方案的后悔值R,计算公式1为:Ri⟶jm=ln(1+exp(βm(xjm-xim))),其中,S0的β值为0.2,Ch_cost的β值为0.3,Travel_sat的β值为-0.1;计算公式2为:Ri=∑j≠i,j=1J∑m=1MRi⟶jm,计算公式3为Pi=exp(-Rj)/∑j=1Jexp(-Rj)。最后,循环输出这辆车的所有充电方案Project,方案后悔Project_re、对应的后悔值Ri和选择概率pi。用matlab代码描述这一过程。
以下是matlab代码描述这一过程:
```matlab
% 调用脚本生成200辆车的数据参数
Tstart; EVtype; SOC; Lrem;
% 随机抽取一辆车
car_index = randi(200);
car_data = [Tstart(car_index) EVtype(car_index) SOC(car_index) Lrem(car_index)];
% 定义充电方案Project
Ch_type = ["Ch_no", "Ch_slow", "Ch_fast"];
Ch_cost = [0, 2.37, 5.52];
Ch_charge_slow = [10, 20, 50, 80];
Ch_charge_fast = [10, 20, 50, 80];
Project = [];
for i = 1:numel(Ch_type)
if Ch_type(i) == "Ch_no"
Project(i,:) = [Ch_type(i), Ch_cost(i), 0];
elseif Ch_type(i) == "Ch_slow"
for j = 1:numel(Ch_charge_slow)
Project(end+1,:) = [Ch_type(i), Ch_cost(i), Ch_charge_slow(j)];
end
elseif Ch_type(i) == "Ch_fast"
for j = 1:numel(Ch_charge_fast)
Project(end+1,:) = [Ch_type(i), Ch_cost(i), Ch_charge_fast(j)];
end
end
end
% 计算方案后悔Project_re
beta_S0 = 0.2;
beta_Ch_cost = 0.3;
beta_Travel_sat = -0.1;
Project_re = [];
for i = 1:size(Project,1)
S0 = car_data(3);
Ch_charge = Project(i,3);
Ch_type = Project(i,1);
Pt_price = Ch_cost(find(Ch_type==Ch_type));
E = car_data(4);
PELre = 50;
Travel_sat = (S0+Ch_charge)*E/PELre;
Ch_cost = E*Ch_charge/Pt_price;
Project_re(end+1,:) = [S0, Ch_cost, Travel_sat];
end
% 计算不同方案的后悔值R
beta = [beta_S0, beta_Ch_cost, beta_Travel_sat];
R = zeros(size(Project,1),1);
for i = 1:size(Project,1)
xj = Project_re(i,:);
for j = 1:size(Project,1)
if j ~= i
xi = Project_re(j,:);
rijm = log(1+exp(beta.*(xj-xi)));
R(i) = R(i) + sum(rijm);
end
end
end
% 计算选择概率pi
P = exp(-R)./sum(exp(-R));
% 输出结果
for i = 1:size(Project,1)
fprintf('方案%d:\n', i);
fprintf('充电类型:%s\n', Project(i,1));
fprintf('充电成本:%.2f\n', Project(i,2));
fprintf('充电量:%d\n', Project(i,3));
fprintf('方案后悔Project_re:S0=%.2f,Ch_cost=%.2f,Travel_sat=%.2f\n', Project_re(i,1), Project_re(i,2), Project_re(i,3));
fprintf('对应的后悔值Ri:%.2f\n', R(i));
fprintf('选择概率pi:%.2f\n\n', P(i));
end
```
相关推荐
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)