I am trying to solve a coupled system of ODE's using MATLAB's bvp4c
function. I want to impose the condition that
$$\int_{0}^{\pi} y_{1}(t) y_{1}(t) dt = 1,$$
where $y_{1}$ is the first state variable.
Another post suggested adding a additional equation to the system of ODE's. Namely, $$I(t) = \int_{0}^{t} y_{1}(s) y_{1}(s) ds \, .$$
Then the additional boundary condition $I(\pi)-1=0$ would enforce the integral condition I want. However, I am not sure how to code this in MATLAB. Can anyone help me with this?
As per nicoguaro's comment, the integral equation can be written as $$\frac{d}{dt}I(t) = y_{1}(t) y_{1}(t) \, .$$
Updating the code accordingly, I have
This is the whole code I am using with bvp4c
(as kirill said, it would be useful to include the whole code):
s = 100;
solinit = bvpinit(linspace(0,pi,1001),@mat4init,s);
sol = bvp4c(@odeproblem,@eightbc,solinit);
x = linspace(0,pi,1001);
y = deval(sol,x);
trapz(y(1,:).*y(1,:))
y_bar = y(1,:)./norm(y(1,:),2);
plot(x,y(1,:))
fprintf('The intial guess for s was %d. The value solved for was %d.\n',s,sol.parameters)
function dydt = odeproblem(t,y,s)
b = pi;
dydt(1) = y(2);
dydt(2) = y(3);
dydt(3) = y(4);
dydt(4) = y(5);
dydt(5) = y(6);
dydt(6) = y(7);
dydt(7) = y(8);
dydt(8) = (-b^4*y(1) + 2*b^2*y(3) - (b^4-2)*y(5) - 2*s^2*b^2*y(7))/s^2;
dydt(9) = y(1)*y(1);
end
function res = eightbc(ya,yb,s)
res = [ya(1); yb(1); ya(3); yb(3); ya(5); yb(5); ya(7); yb(7); ya(9); yb(9)-1];
end
function yinit = mat4init(x)
yinit = [ cos(2*x), -2*sin(2*x), -4*cos(2*x), 8*sin(2*x),...
16*cos(2*x), -32*sin(2*x), -64*cos(2*x), 128*sin(2*x), 1];
end
Here is a plot of the solution:
All other boundary conditions appear to be met (I didn't include the plots showing that). But the integral condition is still not.
eightbc
) for a TPBVP of size 9? $\endgroup$ – Kirill Apr 19 '17 at 22:50eightbc
was a poor choice of name. It could be that MATLAB is converging to the solution you suggest, but I thought the last equation and BC would prevent that from happening. $\endgroup$ – BoiseID Apr 20 '17 at 1:26