看板 MATLAB 關於我們 聯絡資訊
%---------------------- #1 (a)---------------- function [x, iter] = sqrtn(a , tol) n = 1; x = []; x(1) = a; while 1 x(n+1) = 0.5 * ( x(n) + a / x(n) ); rel = (abs( x(n+1) - x(n) )) / x(n+1); while rel < tol fprintf('x = '); disp(x(n)) ; iter = n break; end n = n + 1; end %=============================================== 這是我的code. 牛頓iteration法 我想輸入 >>[x , iter] = sqrtn(2 , 0.01) 應當跑出 x = 1.4142 iter = 3 (蝶代三次中止) 可是那個break似乎不管用 執行一直跑出 x = 1.4142 x = 1.4142 x = 1.4142 x = 1.4142 x = 1.4142........跑不完 想請問要怎麼讓它停下來? 感恩QAQ ! -- █████████◤█████ █ ██ █ ███ █ █ █████麥面█◤█████ █ █◢███◣◥◣ ███████◤██ ██ ███◢████ ██ ███禾斗█◤     吃了會科科    ▂ ▕   ◤ █████◤     快去買來吃!  -⊙-⊙—      禾斗█◤             █▏ /\        -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.112.7.214 ※ 編輯: timche2 來自: 140.112.7.214 (03/27 21:53)
Raymond0710:第二層while要改成if 03/27 22:21
shomingchang:break 只會跳出一層迴圈 03/28 00:55
math99:ctrl+c 03/28 01:00
maxxV3:if和while其實有很微妙的不同點 03/29 03:01