【发布时间】:2018-06-27 17:51:30
【问题描述】:
我正在尝试评估一个函数,该函数在某些输入值处是一个无限余弦序列。
我写了以下代码在 MATLAB 中描述它。
function func = cosfun_hat(a,i)
syms m x;
assume(m,'integer');
assumeAlso(m > 0);
sum(x) = sqrt(1-a^2)*symsum(sqrt(2)*a^m*cos(i*sym(pi)*x*2^m+1),m,0,Inf);
func(x) = sum(x);
end
我想评估返回的“函数”func 以获取某些输入范围的数值,例如x_in = 0:0.001:1。
%Trying to evaluate func at x = 2
%In the command window I write
func = cosfun_hat(0.5,2);
func(2)
返回符号表达式:
(2^(1/2)*3^(1/2)*sum((1/2)^m*(exp(- pi*exp(m*log(2))*4*i - i)/2 + exp(pi*exp(m*log(2))*4*i + i)/2), m == 0..Inf))/2
我尝试使用subs 来评估表达式:
%In the command window
syms y;
w(y) = func(y);
y = 2;
subs(w);
但这会返回相同的符号表达式。我对符号 MATLAB 很陌生。
谢谢!
编辑根据@NickyMattsson 的评论我试过了
vpa(func(2))
返回表达式的数值。
然而,
vpa(func(0.1)) 返回一个符号表达式:
ans =
1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)
使用double(func(0.1))、double 的同样问题不返回任何内容并被卡住。
【问题讨论】:
-
vpa(func(2))能解决您的问题吗? -
@NickyMattsson 感谢您的评论。
vpa(func(2))返回数值。但是,如果我使用vpa(func(0.1)),MATLAB 将返回1.2247448713915890490986420373529*numeric::sum((1/2)^m*(exp(- (pi*exp(m*log(2))*i)/5 - i)/2 + exp((pi*exp(m*log(2))*i)/5 + i)/2), m == 0..Inf)...
标签: matlab