???

# How to solve a system of second order nonlinear differential equations with boundary conditions

48 views (last 30 days)

Show older comments

Hello, I am having troubles solving a system of second order nonlinear equations with boundary conditions using MATALB Here is the equations:

f''(t)=3*f(t)*g(t) ; g''(t)=4f(t)*g(t);

the boundary conditions are: f(0)=1.5 et g'(o)=0; g(2)=3 et f'(2)=f(2)

##### 14 Comments

Alex Sha
on 18 Jun 2021

Refer to the result below please:

g(0) = -1.44989398376437

f'(0) = -2.78301182421201

### Accepted Answer

J. Alex Lee
on 12 Jun 2021

This is a boundary value problem, so probably the most straightforward way is to use bvp4c or bvp5c if you want to solve numerically, and if so don't bother with syms at all. It looks like you already figured how to break your 2nd order equations into first order ones so in addition to f and g you must have j = f' and k = g' or something. So your original question about the BCs you can pose as

0=k(0) and j(2)=f(2)

##### 4 Comments

J. Alex Lee
on 14 Jun 2021

believe me, i had some trouble understanding the syntax for bvp4c/5c as well!

It is not at all intuitive to me either what the initial guess should be. bvp4c failed with the trivial initial guess (zeros), so i just picked something random.

I did at least check the BCs are satisfied, but no I didn't really check the ODE...I figured that can be tested by running your IVP integration.

### More Answers (2)

Paul
on 13 Jun 2021

I was not able to solve the problem for the boundary conditions specified at t=2. But I was able to get a resonable looking solution for the same problem with boundry conditions specified at t = 0.5 by posing it as an optimization problem to solve for the unknown initial conditions that result in the boundary conditions being satisfied.

I'm assuming the equations to be solved are:

f''(t) = 3*f(t)*g(t) + 5

g''(t) = 4*g(t)*f(t) + 7

with initial conditions:

f(0) = 1.5, g'(0) = 0

and boundary constraints defined at tf = 0.5:

g(tf) = 3, f(tf) = f'(tf)

Here is the code:

odeFun = @(t,y) ([y(2); 3*y(1)*y(3) + 5; y(4); 4*y(1)*y(3) + 7]);

tspan = [0 .5];

% solve for the unknown initial conditions with an initial guess

x0 = fminsearch(@myfun,[0;0.01]);

% compute the solution

y0 = [1.5 x0(1) x0(2) 0];

[t,y] = ode45(odeFun,tspan,y0,odeset('MaxStep',0.001));

figure;

plot(t,y),legend('f','fprime','g','gprime');

% verify the solution satisfies the boundary conditions

[1.5-y(1,1) 0-y(1,4) y(end,1)-y(end,2)]

% (approximately) verify the solution satisfies the differential equation

for ii = 1:4

ydot(:,ii) = gradient(y(:,ii),t);

yddot(:,ii) = gradient(ydot(:,ii),t);

end

figure

subplot(211);

plot(t,yddot(:,1) - (3*y(:,1).*y(:,3) + 5)),grid

subplot(212);

plot(t,yddot(:,3) - (4*y(:,1).*y(:,3) + 7)),grid

function f = myfun(x)

% y1(t) = f(t)

% y2(t) = f'(t)

% y3(t) = g(t)

% y4(t) = g'(t)

% y1p(t) = f'(t) = y2(t);

% y2p(t) = f''(t) = 3*f(t)*g(t) + 5 = 3*y1(t)*y3(t) + 5

% y3p(t) = g'(t) = y4(t)

% y4p(t) = g''(t) = 4*g(t)*f(t) + 7 = 4*y1(t)*y3(t) + 7

% y0 = [f(0) f'(0) g(0) g'(0)] = [1.5 x(1) x(2) 0]

odeFun = @(t,y) ([y(2); 3*y(1)*y(3) + 5; y(4); 4*y(1)*y(3) + 7]);

tspan = [0 .5];

y0 = [1.5 x(1) x(2) 0];

[t,y] = ode45(odeFun,tspan,y0,odeset('MaxStep',0.001));

f = (y(end,3) - 3)^2 + (y(end,2) - y(end,1))^2;

end

My quick experiments showed the solution blows up pretty quickly for tf > 1, which is probably why ode45 had trouble. I didn't experiment with any of the ode45 parameters or with the initial guess for the initial conditions to see if a solution could be obtained for tf = 2. Maybe that's doable.

Alex Sha
on 20 Jun 2021

### See Also

### Community Treasure Hunt

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

Start Hunting!