看板 MATLAB 關於我們 聯絡資訊
function ballode %BALLODE Run a demo of a bouncing ball. tstart = 0; tfinal = 30; y0 = [0; 20]; refine = 4; options = odeset('Events',@events,'OutputFcn', @odeplot,... 'OutputSel',1,'Refine',refine); set(gca,'xlim',[0 30],'ylim',[-25 25]); box on hold on; tout = tstart; yout = y0.'; teout = []; yeout = []; ieout = []; for i = 1:10 % Solve until the first terminal event. [t,y,te,ye,ie] = ode23(@f,[tstart tfinal],y0,options); if ~ishold hold on end % Accumulate output. nt = length(t); tout = [tout; t(2:nt)]; yout = [yout; y(2:nt,:)]; teout = [teout; te]; % Events at tstart are never reported. yeout = [yeout; ye]; ieout = [ieout; ie]; ud = get(gcf,'UserData'); if ud.stop break; end % Set the new initial conditions, with .9 attenuation. y0(1) = 0; y0(2) = -.9*y(nt,2); % A good guess of a valid first time step is the length of % the last valid time step, so use it for faster computation. options = odeset(options,'InitialStep',t(nt)-t(nt-refine),... 'MaxStep',t(nt)-t(1)); tstart = t(nt); end plot(teout,yeout(:,1),'ro') xlabel('time'); ylabel('height'); title('Ball trajectory and the events'); hold off odeplot([],[],'done'); % -------------------------------------------------------------- function dydt = f(t,y) dydt = [y(2); -9.8]; % -------------------------------------------------------------- function [value,isterminal,direction] = events(t,y) % Locate the time when height passes through zero in a % decreasing direction and stop integration. value = y(1); % Detect height = 0 isterminal = 1; % Stop the integration direction = -1; % Negative direction only ---------------------------------------------------------------------- 這是個物理現象 球的拋體運動 水平速度為0 垂直速度為20 是把y(t)=(-9.8*t^2)/2 拆成一組常微分方程 y_1'(t)=y_2(t) y_2'(t)=-9.8 我知道的部分就這些 想問請程式碼的意思 像options裡面那些函數所表示的意思 -- ※ 發信站: 批踢踢實業坊(ptt.cc) ◆ From: 140.123.61.208 ※ 編輯: anikishawn 來自: 140.123.61.208 (11/06 15:48)
anikishawn:不好意思 我是學純數學的 對程式不是很懂 還請包含 11/06 15:49
ejialan:help odeset 他有對每個設定做解釋 11/07 08:31
anikishawn:謝謝哦 我昨天使用過help去找了 看了很久的時間 應該是 11/07 09:30
anikishawn:這個code在做什麼 表達什麼了 11/07 09:30
anikishawn:^知道 11/07 09:31