作者war0tft (Dante)
看板MATLAB
標題[討論]一個function,不知道bug怎麼解
時間Wed Mar 28 20:59:50 2012
請求板上高手
幫同學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
1F:推 summitstudio:多了一個 end? 03/28 21:12
2F:→ shomingchang:sum 沒給值? x.^2? 03/28 21:13
3F:→ war0tft:剛剛發現,只能在function裡面給值才可以跑,請問要怎麼才 03/28 21:23
4F:→ war0tft:能在matlab直接給值去跑答案? 03/28 21:24
5F:→ shomingchang:你是不是把ssum打成sum了? 03/28 21:28
6F:→ shomingchang:還有你 x 是一個陣列還是一個數值? 03/28 21:29
7F:→ war0tft:x = argumant of the cosine function ,它是一個值。 03/28 21:33