[問題] 迴圈問題

看板Mathematica作者 ( )時間10年前 (2014/12/07 15:41), 編輯推噓3(301)
留言4則, 3人參與, 最新討論串1/1
我現在在試著用Mathematica寫一個FDTD的演算法 希望可以畫出電磁波的傳遞動畫 但是在跑的時候一直出現錯誤訊息 ke = 100; nsteps = 100; ks = ke/2; c0 = 3 10^8; Ex = Table[0, {i, 0, ke}]; Hy = Table[0, {i, 0, ke}]; dx = 0.01; dt = dx/(2 c0); cc = c0 dt/dx; t0 = 20; sp = 8; plotex = ListPlot[Ex]; Dynamic[plotex] For[t = 1, t <= nsteps, t++, (For[kk = 2, kk <= ke - 1, kk++, Ex[[kk]] = Ex[[kk]] + cc (Hy[[kk - 1]] - Hy[[kk]]); ] Ex[[ks]] = Exp[-0.5*(t - t0)^2/sp^2]; For[kk = 1, kk <= ke - 1, kk++, Hy[[kk]] = Hy[[kk]] + cc (Ex[kk] - Ex[[kk + 1]])]; ListPlot[Ex])] 錯誤訊息是 Set::write : "Tag Times in 0.\ Null is Protected. " "Tag Times in Null\ (0. +0.5\ (0.5\ (0. \ +{0,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,\ 0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,0.,\ 0.,0.,0.,0.,<<51>>}[49])-0.5\ (0. \ +{0,0.,0.,<<45>>,0.,0.,<<51>>}[50]))) is Protected. \ \!\(\*ButtonBox[\">>\", Appearance->{Automatic, None}, BaseStyle->\"Link\", ButtonData:>\"paclet:ref/message/General/write\", ButtonNote->\"Set::write\"]\)" 但是我實在找不到我哪邊有誤用Set(=)了, 有人可以幫我解惑一下嗎? -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 118.166.245.251 ※ 文章網址: http://www.ptt.cc/bbs/Mathematica/M.1417938063.A.B47.html

12/07 15:52, , 1F
你中間有一個 Ex[kk] 不是雙中括號, 改掉試試?(倒數第二行)
12/07 15:52, 1F

12/07 15:57, , 2F
啊, 我找到了.... Ex[[ks]] 前面沒有分號
12/07 15:57, 2F

12/07 16:02, , 3F
喔喔 太感謝了!!!
12/07 16:02, 3F

12/09 16:26, , 4F
推推XD
12/09 16:26, 4F
文章代碼(AID): #1KX0IFj7 (Mathematica)
文章代碼(AID): #1KX0IFj7 (Mathematica)