Error when using symsum in function declaration
6 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
Jacob Goodman
el 15 de En. de 2018
Comentada: Jacob Goodman
el 15 de En. de 2018
I apologize for any obvious errors, this is my first time ever using Matlab, and I'm struggling a bit to get used to the syntax. I have defined 3 functions:
function fact = fact2(n)
if n == 0
fact = 1;
elseif n == -1
fact = 1;
elseif mod(n,2) == 0
fact = prod(2:2:n);
elseif mod(int8(n),2) == 1
fact = prod(1:2:n);
end
Note: this function is apparently a built-in function, but when I try to use it, it says it's not defined. I assume that I am missing some package, but have no idea how to go about fixing that, so I wrote it myself. The function is a double factorial.
function sumArgument = asymArgErf(n)
sumArgument = (-1)^n * fact2(2*n-3) / (8^n-1);
end
function Asymptotic = erf2Asym(n)
syms k;
Asymptotic = 1 + (exp(-4)/(2*sqrt(pi))) * symsum(asymArgErf(k), k, 1, n);
end
The first two functions run just fine in my command window, however the third gives me an error
Error using : (line 38)
Unable to compute number of steps from 1 to 2*k - 3 by 2.
Error in fact2 (line 9)
fact = prod(1:2:n);
Error in asymArgErf (line 2)
argument2 = (-1)^n * fact2(2*n-3) / (8^n-1);
Error in erf2Asym (line 3)
Asymptotic = 1 + (exp(-4)/(2*sqrt(pi))) * symsum(asymArgErf(k), k, 1, n);
This seemed like the function was having problems with me inputting what I'm guessing is a symbol type variable into asymArgErf(k). However when I tried converting the type, I got even longer error messages. Could somebody help me resolve this?
_____________________________________________________________________________
I also tried a simpler case directly into my command window and got an error:
>> syms k
>> symsym(fact2(k), k, 1, 3)
Output argument "fact" (and maybe others) not assigned during call to "fact2".
0 comentarios
Respuesta aceptada
Walter Roberson
el 15 de En. de 2018
In the line
Asymptotic = 1 + (exp(-4)/(2*sqrt(pi))) * symsum(asymArgErf(k), k, 1, n);
then MATLAB has to evaluate asymArgErf(k) first and the result of that is passed to symsum(), so at the time of the asymArgErf(k) call, asymArgErf(k) is evaluated with a purely symbolic k, causing your fact2 to be evaluated with the symbolic expression 2*n-3 . When you apply mod() to a symbolic expression, it means something pretty different than you would expect: mod(2*n-3,2) is evaluated as mod(2,2)*n + mod(-3,2) which is 0*n + 1 which is 1. That leads you to your branch
fact = prod(1:2:n)
which then tries to prod(1:2:2*n-3) but 1:2:2*n-3 cannot be evaluated as a colon expression because n is symbolic.
You effectively cannot use mod with a symbolic variable in a way such as you might hope.
You are unlikely to be able to compute the expression symbolically for general numeric endpoints. You just might be able to compute symbolically within any one numeric value.
A lot of the time, symsum() is the wrong thing to use in MATLAB. symsum() is for calculating formulae, such as symsum(1/x,x,1,n) = eulergamma + psi(n + 1) . When you have a definite list of terms to add, such as for your
symsym(fact2(k), k, 1, 3)
where you need fact2(1), fact2(2), fact2(3) evaluated, then you should be calculating each using arrayfun or similar and then sum() them together.
1 comentario
Más respuestas (0)
Ver también
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!