function Example2 % clc % clear close all y0=[1;0]; %[x;y] tmax=2*pi; % try different ode solvers - ode45, ode23, ode15s odeSolve=ode45(@f_dydt,[0,tmax],y0); %[uc, i] %%%%%%%%% analytic solution %%%%%%%%% dt=1000; t=[0:tmax/dt:tmax]; x=sin(t); y=cos(t); %%%%%%%%%%%%%%%%%% figure plot(odeSolve.x, odeSolve.y,'o',t,x,t,y) grid on title('Example 2') legend('numerical solution', 'analytical solution') fprintf('Number of integration steps: %g\n',length(odeSolve.y)); x_anal=sin(odeSolve.x); y_anal=cos(odeSolve.x); fprintf('Error: %g\n',norm(odeSolve.y-[y_anal;x_anal])); end function dydt = f_dydt(t,y) dydt = zeros(2,1); % a column vector dydt(1) = -y(2); dydt(2) = y(1); end