作者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