%Lab 26 Solutions %Lab26ode45.m %Patrick D. Schmid clear; fprintf('Solve the equation:\n'); fprintf(' dy/dt = y + 2t, y = 1 @ t = 0\n'); fprintf(' To find the value at t = 1.\n'); %initial value yinit = 1; tspan = [0:0.5:1]; %let's not output this [t, sol] = ode45('lab26ode45func', tspan, yinit); fprintf(' The solution of this is %7.3f at t=%3.1f\n\n', sol(3), t(3)); % Note: we have to give at least 3 values for tspan, or else Matlab will % return every value for t and sol that it used in its computations. % With >=3 values, only the ones requested will be returned. % Look at Matlab's help docs (on "ode45") for more information. yinit = 1; tspan = 0:0.2:4; [t, sol] = ode45('lab26ode45func', tspan, yinit); fprintf(' Here are some other results: [t, y]\n'); for i=1:1:21 fprintf(' [%3.1f, %7.3f]\n', t(i), sol(i)); end fprintf(' End of program.\n');