Filter löschen
Filter löschen

Implementation of infinite series in MATLAB.

31 Ansichten (letzte 30 Tage)
Sudhir Sahoo
Sudhir Sahoo am 11 Feb. 2021
Kommentiert: Walter Roberson am 18 Sep. 2022
How I can impliment this type of infinite series in MATLAB given a function as where , here l,\gamma are constant. when I use
syms k;
exp = symsum(H(k + 1,gamma_av),k,1,Inf);
I used to get error like this "The following error occurred converting from sym to double:
Unable to convert expression into double array."
Please help me out.
  5 Kommentare
Walter Roberson
Walter Roberson am 11 Feb. 2021
The error is not in the part of the code that you posted. We need to see the rest of the code.
Sudhir Sahoo
Sudhir Sahoo am 12 Feb. 2021
@Walter Roberson Ok sir I am posting the code as follows
syms k;
snr = 2;
l = 2;
expression2 = symsum(H(k + 1,snr,l),k,1,Inf);
and my function i defined as
function [out] = H(y,z,l)
%gam, y,z,l are user inputs
m = -1/log2(cos(pi/6)); % in paper its used as small gamma.(\gamma)
out1 = (2/z)^y;
out2 = gamma(y) - igamma(y,(l^(-2*(m + 2))/2)*z);
out = out1 * out2;
end
And I want to calculate the sum

Melden Sie sich an, um zu kommentieren.

Akzeptierte Antwort

Walter Roberson
Walter Roberson am 13 Feb. 2021
The error message you get is because the symbolic engine was not able to find a convergent value for the infinite series when you use double(). And depending on the number of digits you use for vpa() it might or might not find a solution.
Interestingly, if you use a finite series such as 75 terms, then double() does well at converting the value, whereas you need on the order of 150+ digits in order for vpa to be able to resolve it: if you use a high finite Limit and too few digits then vpa() will tend to return exactly 0.
format long g
syms k;
snr = 2;
l = 2;
Limit = Inf;
expression2 = symsum(H(k + 1,snr,l),k,1,Limit);
for D = 120:129
try
D
ev = vpa(expression2, D)
ed = double(ev)
catch ME
fprintf('failed working at %d digits\n', D);
end
end
D =
120
ev = 
failed working at 120 digits
D =
121
ev = 
0.000000003077984867743638419940134683770443072429756599385419397056352496391763650277668849759282913432606221931130564797576520443
ed =
3.07798486774364e-09
D =
122
ev = 
0.0000000030779848677436384199401346837704430724297565993854193970563524963917636502776688497592829134326062219311305647975746247394
ed =
3.07798486774364e-09
D =
123
ev = 
0.00000000307798486774363841994013468377044307242975659938541939705635249639176365027766884975928291343260622193113056479757456758764
ed =
3.07798486774364e-09
D =
124
ev = 
0.000000003077984867743638419940134683770443072429756599385419397056352496391763650277668849759282913432606221931130564797574605713361
ed =
3.07798486774364e-09
D =
125
ev = 
failed working at 125 digits
D =
126
ev = 
0.00000000307798486774363841994013468377044307242975659938541939705635249639176365027766884975928291343260622193113056479757460092717886
ed =
3.07798486774364e-09
D =
127
ev = 
0.000000003077984867743638419940134683770443072429756599385419397056352496391763650277668849759282913432606221931130564797574600914952293
ed =
3.07798486774364e-09
D =
128
ev = 
failed working at 128 digits
D =
129
ev = 
0.00000000307798486774363841994013468377044307242975659938541939705635249639176365027766884975928291343260622193113056479757460091191697691
ed =
3.07798486774364e-09
double(expression2)
Error using symengine
Unable to convert expression containing remaining symbolic function calls into double array. Argument must be expression that evaluates to number.

Error in sym/double (line 702)
Xstr = mupadmex('symobj::double', S.s, 0);
function [out] = H(y,z,l)
Pi = sym(pi);
%gam, y,z,l are user inputs
m = -1/log2(cos(Pi/6)); % in paper its used as small gamma.(\gamma)
out1 = (2/z)^y;
out2 = gamma(y) - igamma(y,(l^(-2*(m + 2))/2)*z);
out = out1 * out2;
end

Weitere Antworten (1)

Sahil
Sahil am 18 Sep. 2022
Bearbeitet: Walter Roberson am 18 Sep. 2022
function [out] = H(y,z,l)
Pi = sym(pi);
%gam, y,z,l are user inputs
m = -1/log2(cos(Pi/6));
% in paper its used as small gamma.(\gamma)
out1 = (2/z)^y;
out2 = gamma(y) - igamma(y,(l^(-2*(m + 2))/2)*z);
out = out1 * out2;
end

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by