y1 = dsolve('D2y-3*Dy+2*y = 1','y(0) = 1','Dy(0) = 0','t');
tspan = [0 1];
[t,y2] = ode45(@myodefun,tspan,[1 0]);
y3 = double(subs(y1,'t',0.5));
pos = 0;
for i = 1:size(t,1)-1
if t(i+1) >= 0.5
pos = i;
break;
end
end
k = (0.5-t(pos))/(t(pos+1)-t(pos));
y4 = y2(pos,1)+k*(y2(pos+1,1)-y2(pos,1));
disp(['t = 0.5时数值解为: ' num2str(y4) ' 符号解为: ' num2str(y3)]);
hold on;
y5 = double(subs(y1,'t',t));
plot(t,y2(:,1),'r*',t,y5,'k-');
legend('数值解','符号解');