永发信息网

matlab连加sum问题求助

答案:2  悬赏:40  手机版
解决时间 2021-03-01 17:06
  • 提问者网友:皆是孤独
  • 2021-02-28 21:08
>> syms x
>> k=1;
>> s=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

s =

x/4

>> k=2;
>> s=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

s =

(3*x^2)/64
分别计算时系数为(1/4,3/64)。
连加计算时
>> k=1:2

k =

1 2

>> s=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1));
>> sum(s)

ans =

x^2/192 + x/4

>> 系数成(1/4,1/192)了。哪位高手帮忙看一下什么原因,谢谢啦。
最佳答案
  • 五星知识达人网友:举杯邀酒敬孤独
  • 2021-02-28 21:24
clc
clear
syms x
k=1;
s1=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p1=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

k=2;
s2=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p2=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

k=3;
s3=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p3=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

k=4;
s4=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p4=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))

for k=1:4
s12=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p12=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
end
用上面语句得出的结果是一致的,但下面的就不同了,
k=1:4
s12=x.^k.*prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
p12=prod(1:2:2.*k-1).^2./(4.^k.*(factorial(k)).^2.*(2.*k-1))
原因在于:当k是标量时,乘方运算与一般的相同,
当k是数组或矩阵时,乘方运算用的是矩阵乘方,所以,结果相差很大
全部回答
  • 1楼网友:逃夭
  • 2021-02-28 21:41
matlab中的求和对于常数和一维向量是一样的,对矩阵是对其每一列求和。具体参见函数用法说明 sum sum of elements. s = sum(x) is the sum of the elements of the vector x. if x is a matrix, s is a row vector with the sum over each column. for n-d arrays, sum(x) operates along the first non-singleton dimension. if x is floating point, that is double or single, s is accumulated natively, that is in the same class as x, and s has the same class as x. if x is not floating point, s is accumulated in double and s has class double. s = sum(x,dim) sums along the dimension dim. s = sum(x,'double') and s = sum(x,dim,'double') accumulate s in double and s has class double, even if x is single. s = sum(x,'native') and s = sum(x,dim,'native') accumulate s natively and s has the same class as x. examples: if x = [0 1 2 3 4 5] then sum(x,1) is [3 5 7] and sum(x,2) is [ 3 12]; if x = int8(1:20) then sum(x) accumulates in double and the result is double(210) while sum(x,'native') accumulates in int8, but overflows and saturates to int8(127). see also prod, cumsum, diff, accumarray, isfloat. overloaded methods: codistributed/sum timeseries/sum
我要举报
如以上回答内容为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
点此我要举报以上问答信息
大家都在看
推荐资讯