[討論] matlab 不知道哪個地方寫錯消失

看板MATLAB作者時間8年前 (2017/09/30 16:16), 編輯推噓2(204)
留言6則, 3人參與, 最新討論串1/1
請教各位大神 Q: 問題描述:傘兵問題,(1)求第十秒的速度,(2)依time stop=0.5 和 time stop= 1 ,繪出0到16秒到時間vs速度圖 以下是小弟的程式,不知道那裡錯,拜託幫忙指導 function value = veiocity3(dt,ti,tf,tv,vi,m,cd) to(1)=0;vo(1)=0; g=9.81; t=ti; v=vi; n=(tf-ti)/dt; for i=2:n+1 dvdt=deriv(v,m,cd); t=t+dt; to(i,1)=t; vo(i,1)=v; if t ==tv, value=v; end end vexact=sqrt(g*m/cd)*tanh(sqrt(g*cd/m)*to); plot(to,vo,’b*-‘,to(:,1),vexact,’r-‘) title(‘comparision of exact and numerical solution.’) xlabel(‘values of time’) ylabel(‘values of velocity’) legend(strcat(‘dt=‘,num2str(dt)),’exact solution’) function dv=deriv(v,m,cd) g=9.81; dv=g-(cd/m)*v^2; -- ※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 39.12.230.132 ※ 文章網址: https://www.ptt.cc/bbs/MATLAB/M.1506759403.A.7F6.html

10/03 01:03, , 1F
乍看之下感覺錯很多.... 你要不要細寫想法、算式、變數代號
10/03 01:03, 1F

10/03 01:04, , 2F
if t ==tv 這也是明顯有問題。切記用 >= 比較,尤其是浮點
10/03 01:04, 2F

10/03 01:04, , 3F
數很難有絕對的 == 。
10/03 01:04, 3F

10/03 01:06, , 4F
然後看結構,感覺你應該還要在if 內加 break 中斷 for loop
10/03 01:06, 4F

10/03 01:57, , 5F
你算出dvdt結果沒用到不覺得奇怪嗎?
10/03 01:57, 5F

10/03 08:43, , 6F
有一小部分是人家教的... 問他也問不出個所以然QQ
10/03 08:43, 6F
文章代碼(AID): #1PprBhVs (MATLAB)