function Example3 % clc % clear close all global a; % change constant 'a' and see the problems with numerical solution due to the computer arithmetic representation of number... a=1; y0=[1;1]; %[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=exp(a*sin(t)); y=exp(-a*sin(t)); %%%%%%%%%%%%%%%%%% figure plot(odeSolve.x, odeSolve.y,'o',t,x,t,y) grid on title('Example 3') legend('numerical solution', 'analytical solution') fprintf('Number of integration steps: %g\n',length(odeSolve.y)); x_anal=exp(a*sin(odeSolve.x)); y_anal=exp(-a*sin(odeSolve.x)); fprintf('Error ||v_num-v_anal||: %g\n',norm(odeSolve.y-[x_anal;y_anal])); end function dydt = f_dydt(t,y) global a dydt = zeros(2,1); % a column vector dydt(1) = a*y(1)*cos(t); dydt(2) = -a*y(2)*cos(t); end