[討論]一個function,不知道bug怎麼解(已解)
請求板上高手
幫同學debug,就這兩個式子,matlab不給過
請板上高手解答,很少發文,如有冒犯,請多多指教, 謝謝。
function [ssum,b,x] = cosser(x,tol,nmax)
% cosser Evaluate the series representation of the cosine function
% x = argumant of the cosine function, i.e. to compute cos(x)
% tol = (optional) tolerance on accumulated sum, default: 5. e-9
% series is terminated when abs(t_k/s_k) < delta. t_k is the kth term and
% s_k is the sum after the kth term is added
% nmax = (optional) maximum number of terms added in the expansion,
% default: nmax = 15
% output: b = the number of terms added in the series
% output: ssum = value of series sum after the tolerance is met
if nargin < 2, tol = 5.e-9;end
if nargin < 3, nmax = 15; end
term = 1; ssum = 1; b = 1;
for k = 2: 2: (2*nmax) ;
* term = -term *(x^2)/(k*(k-1));
* ssum = sum + term;
if (term/ssum) < tol, break
end
end
b= k/2 + 1;
end
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 203.77.65.115
推
03/28 21:12, , 1F
03/28 21:12, 1F
→
03/28 21:13, , 2F
03/28 21:13, 2F
→
03/28 21:23, , 3F
03/28 21:23, 3F
→
03/28 21:24, , 4F
03/28 21:24, 4F
→
03/28 21:28, , 5F
03/28 21:28, 5F
→
03/28 21:29, , 6F
03/28 21:29, 6F
→
03/28 21:33, , 7F
03/28 21:33, 7F