MATLAB Answers

0

ode23 and ode45 problem

Asked by baran erbas on 26 May 2019
Latest activity Commented on by baran erbas on 27 May 2019
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?

  2 Comments

Next time, why not make an effort to do your homework yourself? Then show what you tried and ask for someone to help fix it, if you do not succeed.
Because i have no idea about this question if you can give me an example solution i will try to solve my question.

Sign in to comment.

1 Answer

Answer by Stephan
on 26 May 2019
 Accepted 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

  4 Comments

Show 1 older comment
function xl=H(x,y)
xl=zeros(2,1);
xl(1)=x(2);
xl(2)=-2*x+4*y^2;
[y,x]=ode45['H',[0,1],[0,1]];
I tried this but i think it is totally wrong. I should write this code like this. how can i do like that??
Jan
on 27 May 2019
Almost nice.
function main
[y,x] = ode45(@H, [0,1], [0,1]);
% ^ ^ round parentheses
end
function xl = H(x,y)
xl = zeros(2,1);
xl(1) = x(2);
xl(2) = -2 * x + 4 * y^2;
end
Use @H instead of defining the function to be integrated as char 'H'. The latter is still working, but outdate for 15 years now.
I tried this but it gives error
Index exceeds the number of array elements (1).
Error in batu>H (line 7)
xl(1) = x(2);
Error in odearguments (line 90)
f0 = feval(ode,t0,y0,args{:}); % ODE15I sets args{1} to yp0.
Error in ode45 (line 115)
odearguments(FcnHandlesUsed, solver_name, ode, tspan, y0, options, varargin);
Error in batu (line 2)
[y,x] = ode45(@H, [0,1], [0,1]);
what is the problem???

Sign in to comment.