%% Using Initial Guess to Indicate Desired Solution
% Boundary value problems can have multiple solutions. One purpose of the initial guess is to indicate which solution, among several, that you want.
% The second-order differential equation
%  y'' + |y| = 0
% has exactly two solutions that satisfy the boundary conditions
%  y(0) = 0,
%  y(4) = -2.
% Before using bvp4c to solve the problem, you need to rewrite the differential equation as a system of two first-order ODEs,
%  y_1' = y_2,
%  y_2' = -|y_1|,
% where y_1 = y and y_2 = y'. This system has the required form
%  y' = f(x,y),
%  bc(y(a),y(b)) = 0.
% The function, f, and the boundary conditions, bc, are coded in MATLAB as the functions twoode and twobc.
% function dydx = twoode(x,y)
% dydx = [ y(2); -abs(y(1)) ];
% function res = twobc(ya,yb)
% res = [ ya(1); yb(1) + 2 ];
% Form a guess structure consisting of an initial mesh of five equally spaced points in [0,4] and a guess of the constant values
%  y_1(x) = 1,
%  y_2(x) = 0.

solinit = bvpinit(linspace(0,4,5),[1 0]);

%
% Solve the problem using bvp4c.

sol = bvp4c(@twoode,@twobc,solinit);

%
% Evaluate the numerical solution at 100 equally spaced points and plot y(x).

x = linspace(0,4);
y = deval(sol,x);
plot(x,y(1,:))

%
% To obtain the other solution of this problem, use the initial guess
%  y_1(x) = -1,
%  y_2(x) = 0.

solinit = bvpinit(linspace(0,4,5),[-1 0]);
sol = bvp4c(@twoode,@twobc,solinit);
x = linspace(0,4);
y = deval(sol,x);
plot(x,y(1,:))

% Copyright 2015 The MathWorks, Inc.