MATLAB: Ode23 and ode45 problem

ode23 ode45

Suppose we have a differential equation dy/dx=-2x+4y^2 over the range x=0 to 1 with y(0)=0. I need to solve this question with 'ode23' and ode45 in matlab. Does anybody help me?

Best Answer

% Solve symbolic (blue line in plot)
syms y(x) x
eqn = diff(y,x) == -2*x+4*y^2
sol_symbolic = dsolve(eqn,y(0)==0);
fplot(sol_symbolic,[0 1])
hold on
% solve numeric with ode45 (red dots in plot)
[V, S] = odeToVectorField(eqn);
fun = matlabFunction(V,'vars', {'x','Y'});
[x, sol_numeric] = ode45(fun,[0 1], 0);
plot(x, sol_numeric,'or')
hold off
With this example code it should be possible to use ode23 also