I have initial value problem that I have to write the right side into a vector function

I have DEs that I need to write into a vector function and use
ode45
to approximate the solution for the initial value problem on the interval 0 < t < 12. This is what I have so far
w = @(t,y) 5 + (z/5) - ((4*y)/(20+3*t))
w1 = @(t,z) ((4*y)/(20+3*t)) - (2*z)/5
[t,y] = ode45(w, [0, 12], 0)
[t,z] = ode45(w1, [0, 12], 20)

Answers (1)

The hints are in the instructions. You are not solving the problem the way you are asked to.
Because your equations are coupled, you have to solve them simultaneously. To do that, create an odefun that solves both equations. The input (initial conditions) and output (solution of both equations) are vectors. The output must be a column vector.
See this example (the odefun with 2 equations).
Adapting it to your case, you could designate y(1) to be y in your equations, and y(2) to be z. As long as the order is consistant and matches your y0 input, it doesn't matter which one is y and which one is z.

3 Comments

@Cris LaPierre I have followed the example you posted but I am not sure where to go now as the example did not show any additional steps
y0 = [0;20];
tspan = [0,12];
[t,y] = ode45(@t,y)odefun(t,y),tspan,y0);
function dydt = odefun(t,y)
dydt = zeros (2,1);
dydt(1) = 5 + y(2)/5 - 4*y(1)/(20+3*t);
dydt(2) = 4*y(1)/(20+3*t) - 2*y(2)/5;
end
Well, let's give the credit to @William Rose, as he is the one who came up with this code.
Just run it. What is it you want to do with the results? Perhaps a plot? See below.
y0 = [0;20];
tspan = [0,12];
[t,y] = ode45(@odefun,tspan,y0);
plot(t,y)
legend('y','z')
function dydt = odefun(t,y)
dydt = zeros (2,1);
dydt(1) = 5 + y(2)/5 - 4*y(1)/(20+3*t);
dydt(2) = 4*y(1)/(20+3*t) - 2*y(2)/5;
end
@Cris LaPierre Yes, I am trying to approximate the solution for the initial value equation on the interval of 0 <= t <= 12 and plot the resulting y(t) and z(t)

Sign in to comment.

Asked:

on 18 Nov 2021

Commented:

on 23 Nov 2021

Community Treasure Hunt

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

Start Hunting!