[討論]一個function,不知道bug怎麼解(已解)

看板MATLAB作者 (Dante)時間13年前 (2012/03/28 20:59), 編輯推噓1(106)
留言7則, 3人參與, 最新討論串1/1
請求板上高手 幫同學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
多了一個 end?
03/28 21:12, 1F

03/28 21:13, , 2F
sum 沒給值? x.^2?
03/28 21:13, 2F

03/28 21:23, , 3F
剛剛發現,只能在function裡面給值才可以跑,請問要怎麼才
03/28 21:23, 3F

03/28 21:24, , 4F
能在matlab直接給值去跑答案?
03/28 21:24, 4F

03/28 21:28, , 5F
你是不是把ssum打成sum了?
03/28 21:28, 5F

03/28 21:29, , 6F
還有你 x 是一個陣列還是一個數值?
03/28 21:29, 6F

03/28 21:33, , 7F
x = argumant of the cosine function ,它是一個值。
03/28 21:33, 7F
文章代碼(AID): #1FSmj9aR (MATLAB)