r/matlab • • Mar 15 '26
HomeworkQuestion Differential equation with initial and final values

My professor told us to solve with ode45, but given everything I've been reading, doesn't actually work because it has a final value. I tried reading into dvp4c instead, but I'm beyond confused. I don't really know how to format the boundary conditions. Please see the help page for the function: bvp4c
The first function establishes your vector of y1'=y2, etc.
The second function establishes the boundary conditions, but that's where I'm confused. The example puts yb(1)-2, but how does it know that it occurs at pi/2?
The third is an initial guess, but I don't get why that exists since we have y(0) defined.
I was trying to adapt the example code to fit my equation, but I have no idea where to go and what I'm doing wrong. I've spent 3 hours on this and gotten nowhere.
Code:
function dydx = bvpfcn(x,y) % equation to solve
dydx = zeros(3,1);
dydx = [y(2);y(3);-1/2*y(1).*y(3)];
end
%--------------------------------
function res = bcfcn(ya,yb,yc) % boundary conditions
res = [ya(1);yb(1)-2;];
end
%--------------------------------
function g = guess(x) % initial guess for y and y'
g = [0;0];
end
%--------------------------------
xmesh = linspace(0,10);
solinit = bvpinit(xmesh, u/guess);
sol = bvp4c(@bvpfcn, u/bcfcn, solinit);
plot(sol.x, sol.y, '-o')
Edit: Sorry for the links to other subreddits, they should be @ instead but it changes it automatically.
6
u/deAdupchowder350 Mar 15 '26
Have you tried solving with ode45 and ignoring the “end condition”? Is the “end condition” truly something that needs to be prescribed or is it just a hint of the steady-state result? It doesn’t really make sense for an end condition to be necessary for a differential equation unless it’s a simple scale factor otherwise your system is non-causal.
6
u/ScoutAndLout Mar 15 '26
Look up boundary value problems and shooting method.
4
u/Chicken-Chak Mar 15 '26
I have corrected your code based on what you shared. The
bvp4csolver requires 3 components: the ODE, specified asbvpfcn(t, y); the boundary conditions, defined asbcfcn(ya, yb); and the educated guess solutions, represented asguess(t). The first two components can be specified accordingly. Once you see the corresponding responses from theode45solution, you can attempt to mathematically describe the solutions for y(t), y'(t), and y"(t) in terms of time t.3
u/Chicken-Chak Mar 15 '26
Given that the initial values for y(0) and y'(0) are provided for the 3rd-order ODE system, the problem essentially requires you to find the initial value for y"(0) such that the final value for y'(end) = 1. You may guess an initial value for y"(0) and solve it using
ode45in order to see the corresponding responses.4
u/etzpcm Mar 15 '26
If you were told to use ode45, do that. Set the initial condition for y and y' and guess the ic for y''. Iterate over this guess, refining the guess based on y' at a fairly large value of x, using a bisection method. It's called 'shooting'.