% y'=-2xy
% y(-3)=exp(-9)
f = @(x,y) -2*x*y;
opt = odeset('RelTol',1e-3);
[x y] = ode23(f,[-3 3],exp(-9),opt); % prova anche ode45
t = linspace(-3,3,200)';
sol = exp(-t.^2);
plot(x,y,'-o',t,sol,'--')
title('y''=-2xy')
legend('y(x)','soluzione')
pause

% y'=-20y+20sin(x)+cos(x)
% y(0)=1
f = @(x,y) -20*y+20*sin(x)+cos(x);
opt = odeset('RelTol',1e-3);
[x y] = ode45(f,[0 1],1,opt);
t = linspace(0,1,200)';
sol = exp(-20*t)+sin(t);
plot(x,y,'-o',t,sol,'--')
title('y''=-20y+20sin(x)+cos(x)')
legend('y(x)','soluzione')
pause

% y"=-2y-2(x-1)y'
% y(0)=1/e,  y'(0)=2/e
f = @(x,z) [z(2);-2*z(1)-2*(x-1)*z(2)];
opt = odeset('RelTol',1e-3);
[x z] = ode23(f,[0 5],[exp(-1);2*exp(-1)],opt);
t = linspace(0,5,200)';
sol = exp(-(t-1).^2);
plot(x,z,'-o',t,sol,'--')
title('y"=-2y-2(x-1)y''')
legend('y(x)','y''(x)','soluzione')
pause
