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

作者: sangy79692 (瞇瞇眼看世界)   2017-09-30 16:16:41
請教各位大神
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;
作者: sppmg (sppmg)   2017-10-03 01:03:00
乍看之下感覺錯很多.... 你要不要細寫想法、算式、變數代號if t ==tv 這也是明顯有問題。切記用 >= 比較,尤其是浮點數很難有絕對的 == 。然後看結構,感覺你應該還要在if 內加 break 中斷 for loop
作者: sunev (Veritas)   2017-10-03 01:57:00
你算出dvdt結果沒用到不覺得奇怪嗎?
作者: sangy79692 (瞇瞇眼看世界)   2017-10-03 08:43:00
有一小部分是人家教的... 問他也問不出個所以然QQ

Links booklink

Contact Us: admin [ a t ] ucptt.com