[問題] 建立function求積分

看板MATLAB作者 (比格踢)時間9年前 (2014/12/10 02:12), 編輯推噓1(102)
留言3則, 1人參與, 最新討論串1/1
Create a function that can do the following things: Calculate the following integration : g(k) = ∫f(x)*sin(k*x) dx, where f(x) is any mathematical function, and the integration range is from -X to X k=[0: 2*pi/X : 1/dx] 以上是題目 f(x)和上下線都由使用者輸入 然後下面是我打的程式碼 function onetwothree(f,X) dx = 1e-6; for k=0 : 2*pi/X : 1/dx g = integral(f*sin(k),-X,X); fprintf('g(%d)=%d\n',k,g); end 然後去測試 onetwothree(@(x)x^2,1) 結果跑出來這樣 Undefined function 'mtimes' for input arguments of type 'function_handle'. 想請問大大我的程式碼問題出在哪 -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 27.147.50.151 ※ 文章網址: http://www.ptt.cc/bbs/MATLAB/M.1418148733.A.244.html

12/12 16:34, , 1F
onetwothree(@(x,k) x.^2*sin(k),1)
12/12 16:34, 1F

12/12 16:35, , 2F
(function裡) g=integral(@(x)f(x,k),-X,X)
12/12 16:35, 2F

12/12 16:36, , 3F
答案不知道對不對 有值就是了
12/12 16:36, 3F
文章代碼(AID): #1KXpjz94 (MATLAB)