- 试试。。。
- % o45ex1.m
- % solve y’’=tˆ2*y*cos(t)on [0,5] given y(0)=2, y’(0)=3
- % this can be written as a system of first order DEs
- % y1’=y2 y1(0)=2
- % y2’=tˆ2*y1*cos(t) y2(0)=3
- a=0; b=5; % interval
- y0=[2 3]; % initial condition
- tspan=[a:.1:b]; % interval of integration
- [t,y]=ode23(’o45ex1func’,tspan,y0);% soln in vectors t and y
- plot(t,y(:,1),t,y(:,2),’r--’) % plot y1 and y2 versus t
- legend(’y’,’dy/dt’,2)
- xlabel(’t’); ylabel(’y and dy/dt’)
- print -depsc o45ex1 % output to postscript file
- ———————————————————————————————————————
- function f=o45ex1func(t,y)
- % o45ex1func.m
- % the RHS of the system of DEs
- f(1)=y(2);
- f(2)=tˆ2.*y(1).*cos(t);
- f=f(:); % forces f to be a column vector
- return
复制代码
|