p6_17.m
来自「Advanced Engineering Mathematics using M」· M 代码 · 共 40 行
M
40 行
% P6_17.M Solve the equation% y''+y = 1-t^2/pi^2 for 0 <= t ,=pi% 0 elsewhere% Calls ode23 and p617f.Mcleart0=0; % Initial timetf=3*pi; % Final time x0t=[0 0]';[t,x]=ode23('p617f',[t0,tf],x0t) % This displays resultsy=x(:,1);%length_t=length(t);for I=1:length_t; if t(I) <=pi input(I)=1-t(I)^2/(pi^2); else input(I)=0; endend%% Plot the analytic solution and compare%for I=1:length_t; if t(I) <= pi y2(I)=(1+2/(pi^2))*(1-cos(t(I)))-t(I)^2/(pi^2); else y2(I)=-(1+4/(pi^2))*cos(t(I))+(2/pi)*sin(t(I)); endendclfplot(t,y,'-',t,input,'+',t,y2,'o')title('ODE23 solution --- Input ++++ Analytical ooo')xlabel('t')ylabel('y(t)')grid%% Observe the t and x output showing how% ode23 steps forward in time%% Version 5 Changed call to ode23; changed p617f.m
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?