MATLAB Answers

# ode 45 doesnt work

1 view (last 30 days)
Naor t on 2 May 2021
Commented: William Rose on 3 May 2021
Hey' so im tring to solve this eq
this is the code' what im missing? why it dosent work?
y0 = [-1 ;8];
tspan = [0 ;3];
[t,y] = ode45(@(t,y) ((2*y^2*t + 4)/((3-t^2*y)*2)),tspan,y0);
plot(t,y)
grid on
xlabel('t')
ylabel('y')
title('Solutions of (2*y^2*t + 4)/((3-t^2*y)*2','interpreter','latex')
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

William Rose on 3 May 2021
Edited: William Rose on 3 May 2021
It doesn;t work because you define y0=[-1 8], a vector with 2 elements. Therefore ode45() gets into trouble when it tries to square this vector as it evaluates dydt.
If you define y0=-1 it works. See result below.
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### More Answers (2)

Cris LaPierre on 3 May 2021
There is an issue with your initial conditions. See this post on how to set up an initial value problem when t ~= 0.
However, you may want to revisit either your equation or your initial values, as the result seems incorrect. Y values are extremely big.
y0 = 8;
tspan = [-1 3];
[t,y] = ode45(@(t,y) ((2*y^2*t + 4)/((3-t^2*y)*2)),tspan,y0);
plot(t,y)
grid on
xlabel('t')
ylabel('y')
title('Solutions of (2*y^2*t + 4)/((3-t^2*y)*2')
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

VBBV on 3 May 2021
Edited: VBBV on 3 May 2021
y0 = [-1 ;8];
tspan = [0 ;3];
[t,y] = ode45(@(t,y) ((2*y.^2.*t + 4)./((3-t.^2.*y)*2)),tspan,y0); % use element wise operator
plot(t,y); axis([0 3 -1.5 20])
grid on
xlabel('t')
ylabel('y'); legend('y0= -1','y0 = 8')
use element wise operator for matlab function ode45
##### 2 CommentsShowHide 1 older comment
William Rose on 3 May 2021
The answer from @VBBV is interesting. He assumes that the equation you gave was for the derivative of the vector [y1(t),y2(t)]. By using element-wise squaring and division, @VBBV converted your one equation into two simultaneous differential equations, one for dy1/dt and one for dy2/dt. But they are identical:
There is no cross-talk between the equations - in other words, y1(t) and y2(t) do not influence eachother. @VBBV solved the pair of differential equations with ode45(), for the inital conditions y1(0)=-1, y2(0)=+8. His solution for y1(t) exactly matches mine, because the differential equation is the same, and the inital condition is the same. The solution for y2(t) is different, only because the initial condtion is different.
I assumed that you intended to write a differential equation for a single variable. You may then solve it for as many initial conditions as you like.

Sign in to comment.

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by