ODE45 Singularity at the Initial Point - matlab

For the given differential equation,
with the initial condition y(0) = 1,
I am trying to find a solution using ODE45 method of MATLAB over the interval [0 5]. However, I noticed that the given equation is actually singular at the given initial point. Given the circumstances, how do you suggest I proceed?
What I have tried is that I can start the interval from 0 + δ for some δ < 1e-5. However, this would be a crude approximation. The equation is not differentiable at the point 0.
Thank you so much,

Related

Problem on numerical solution of y'=y^2+1

I would like to use numerical approach to calculate the differential with singularities.
For instance, y'=y^2+1 with y(0)=0. The analytical solution is not hard to find, y=tan(x).
The problem arises when I apply the numeric method, see the code in Matlab
tspan = [0 6];
y0 = [0; 0];
ode = #(t, y) y.^2+1;
[t, y] = ode45(ode, tspan, y0);
plot(t, y(:,1))
axis([0 6 -20 20])
which gives the plot below, i.e., it missed the part of solution after the first singularity.
My question: how to find the full numeric solution of a differential equation with singularities?
Thanks in advance!
This is impossible in general. An ODE solution ends where it diverges to infinity (or leaves the domain of the ODE function in any other way).
What you can do is apply domain knowledge. This equation is a Riccati DE. If one knows one solution, one can transform it into a Bernoulli DE that has a solution formula. There is a second transform without knowledge of a particular solution, it works always but is not always helpful. Set y=p/q and select their relationship so that a system of linear equations results. Here p'q-q'p=p^2+q^2 resolves nicely to p'=q, q'=-p, or as second order DE u''+u=0, the harmonic oscillator, for u=q or u=p. This system now has no singularities and the poles of the original solution y are the roots of the denominator q.

Simple script that computes a solution of linear ODEs giving wrong result

this a question that envolves both programming and mathematics. So, I'm trying to write a code that computes the general solution of a system of linear ODEs described by . The mathematical formula it's shown above:
where the greek symbol \PHI that appers in the equation is the expm(A*t)
clear all
A=[-2]; %system matrix
t0=1; %initial time of simulation
tf=2; %final time of simulation
syms t x_0
x0=x_0;
hom=expm(A*t); %hom means "homogeneous solution"
hom_initialcond=hom*x0;%this is the homogeneous solution multiplied by the initial conditon
invhom=inv(hom); %this is the inverse of the greek letter at which, multiplied by the input of the system, composes the integrand of the integral
g=5*cos(2*t); %system input
integrand=invhom*g; %computation of the integrand
integral=int(integrand,t0,t); %computation of the definite integral from t0 to t, as shown by the math formula
partsol=hom*integral; %this is the particular solution
gen_sol=partsol+hom_initialcond %this is the general solution
x_0=1; %this is the initial condition
t=linspace(t0,tf); %vector of time from t0 to tf
y=double(subs(gen_sol)); %here I am evaluating my symbolic expression
plot(t,y)
The problem is that my plot of the ODE's solution it's not looking well, as you can see:
The solution it's wrong because the curve shown in the graph doesnt start at the initial value equals 1. But the shape it's very similar from the plot gave by the MATLAB ODE solver:
However, if I set t0=0 then the plot gave by my code and by MATLAB solver it's exacly equal to each other. So, my code it's fine for t0=0 but with any other values my code goes wrong.
The general solution in terms of fundamental matrices is
or more often seen as
But since the initial time is often taken to be zero, the inverse of the fundamental matrix is often omitted since it is the identity for linear, constant coefficient problems at zero (i.e., expm(zeros(n)) == eye(n)) and the c vector is equivalent to the initial condition vector.
Swapping some of the lines around near your symbolic declaration to this
syms t x_0 c_0
hom = expm(A*t) ;
invhom = inv(hom) ;
invhom_0 = subs(invhom,t,sym(t0)) ;
c_0 = invhom_0 * x_0 ;
hom_initialcond = hom * c_0 ;
should provide the correct solution for non-zero initial time.

How to solve equations with complex coefficients using ode45 in MATLAB?

I am trying to solve two equations with complex coefficients using ode45.
But iam getting an error message as "Inputs must be floats, namely single or
double."
X = sym(['[',sprintf('X(%d) ',1:2),']']);
Eqns=[-(X(1)*23788605396486326904946699391889*1i)/38685626227668133590597632 + (X(2)*23788605396486326904946699391889*1i)/38685626227668133590597632; (X(2)*23788605396486326904946699391889*1i)/38685626227668133590597632 + X(1)*(- 2500000 + (5223289665997855453060886952725538686654593059791*1i)/324518553658426726783156020576256)] ;
f=#(t,X)[Eqns];
[t,Xabc]=ode45(f,[0 300*10^-6],[0 1])
How can i fix this ? Can somebody can help me ?
Per the MathWorks Support Team, the "ODE solvers in MATLAB 5 (R12) and later releases properly handle complex valued systems." So the complex numbers are the not the issue.
The error "Inputs must be floats, namely single or double." stems from your definition of f using Symbolic Variables that are, unlike complex numbers, not floats. The easiest way to get around this is to not use the Symbolic Toolbox at all; just makes Eqns an anonymous function:
Eqns= #(t,X) [-(X(1)*23788605396486326904946699391889*1i)/38685626227668133590597632 + (X(2)*23788605396486326904946699391889*1i)/38685626227668133590597632; (X(2)*23788605396486326904946699391889*1i)/38685626227668133590597632 + X(1)*(- 2500000 + (5223289665997855453060886952725538686654593059791*1i)/324518553658426726783156020576256)] ;
[t,Xabc]=ode45(Eqns,[0 300*10^-6],[0 1]);
That being said, I'd like to point out that numerically time integrating this system over 300 microseconds (I assume without units given) will take a long time since your coefficient matrix has imaginary eigenvalues on the order of 10E+10. The extremely short wavelength of those oscillations will more than likely be resolved by Matlab's adaptive methods, and that will take a while to solve for a time span just a few orders greater than the wavelength.
I'd, therefore, suggest an analytical approach to this problem; unless it is a stepping stone another problem that is non-analytically solvable.
Systems of ordinary differential equations of the form
,
which is a linear, homogenous system with a constant coefficient matrix, has the general solution
,
where the m-subscripted exponential function is the matrix exponential.
Therefore, the analytical solution to the system can be calculated exactly assuming the matrix exponential can be calculated.
In Matlab, the matrix exponential is calculate via the expm function.
The following code computes the analytical solution and compares it to the numerical one for a short time span:
% Set-up
A = [-23788605396486326904946699391889i/38685626227668133590597632,23788605396486326904946699391889i/38685626227668133590597632;...
-2500000+5223289665997855453060886952725538686654593059791i/324518553658426726783156020576256,23788605396486326904946699391889i/38685626227668133590597632];
Eqns = #(t,X) A*X;
X0 = [0;1];
% Numerical
options = odeset('RelTol',1E-8,'AbsTol',1E-8);
[t,Xabc]=ode45(Eqns,[0 1E-9],X0,options);
% Analytical
Xana = cell2mat(arrayfun(#(tk) expm(A*tk)*X0,t,'UniformOutput',false)')';
k = 1;
% Plots
figure(1);
subplot(3,1,1)
plot(t,abs(Xana(:,k)),t,abs(Xabc(:,k)),'--');
title('Magnitude');
subplot(3,1,2)
plot(t,real(Xana(:,k)),t,real(Xabc(:,k)),'--');
title('Real');
ylabel('Values');
subplot(3,1,3)
plot(t,imag(Xana(:,k)),t,imag(Xabc(:,k)),'--');
title('Imaginary');
xlabel('Time');
The comparison plot is:
The output of ode45 matches the magnitude and real parts of the solution very well, but the imaginary portion is out-of-phase by exactly π.
However, since ode45's error estimator only looks at norms, the phase difference is not noticed which may lead to problems depending on the application.
It will be noted that while the matrix exponential solution is far more costly than ode45 for the same number of time vector elements, the analytical solution will produce the exact solution for any time vector of any density given to it. So for long time solutions, the matrix exponential can be viewed as an improvement in some sense.

ODE45 and time interval

ODE45 function in matlab takes argument:
(function,[tinitial tfinal],yinitial)
But here, I believe, the span of time is predetermined. How can I assign a vector for it? I mean how do I solve the ODE for the domain 1:0.1:5?
Thank You.
If you need the values at specified points in time, simply go:
tspan = 1:0.1:5
[T Y] = ode45(odefun, tspan, y0)
T should be the same as tspan, and Y will be the corresponding values for each point in time.
You can assign the following vector for time span,and this way, you are saying to ode45 that you want the solution at specific time points (here , every 0.001)
tspan = ti:0.001:tf;
Output vectors ( T, X ) will have as many steps as the tspan vector has.
(But if you mean that you want the solver to take predetermined and constant steps for solving the equation , you can't do that.)

error solving two coupled second order differential equations in matlab

I am trying to solve the following differential equations on matlab. (They are the equations obtained from the yang-mills-higgs lagrangian for the hoofy polyakov monopole ansatz). This is my function file. I have two variables h and k and their derivatives w.r.t to a variable t. My x(1)=h, x(2)=k, x(3)=dh\dt, x(4)=dk\dt. All the functions have initial value 0.
function xprime = monopole( t,x )
%UNTITLED Summary of this function goes here
% Detailed explanation goes here
xprime(1)=x(3);
xprime(2)=x(4);
xprime(4)=(1/(t.^2)).*((x(2).^2)-1).*x(2) + 4.*(x(1).^2).*x(2);
xprime(3)=(2/(t.^2)).*(x(2).^2).*x(1)-(1-(x(1)).^2).*x(1)-(2/t).*x(3);
xprime=xprime(:);
end
Now when I run the following code
>
> t0=0;
>> tf=10;
>> x0=[0 0 0 0];
>> [t,s]=ode45(#monopole,[t0,tf],x0);
>> plot(t,s(:,1));
I am not getting anything. The graph window appears but it doesnt contain anything. This equations are supposed to have solutions. The dotted curves is what one should get with the curve starting from 1 is k, and from 0 is h.
What is my mistake?
When this happens the first thing you should do is look at the values in the t and s vectors. In this case s(1,1) contains 0, and s(:,2:end) are all NaN. Hence nothing on the plot.
As to why this is happening, a few thoughts
Are you sure that your definition of monopole is correct?
Why are you showing a plot where k(0) = 1, but passing it an initial condition of k=0?
Why are you using h^prime(0) = 0 initial conditions, but in the plot it looks like h^prime(0) has a non-zero slope?
The term with the 1./t^2 sure looks suspicious; just think about it, at the very first step you are going to divide 0 by 0, hence NaN. Perhaps the ode solver is having a hard time with this, and another solver would work better (note: I have very little experience with ODE solvers, so take this with a big grain of salt).
Finally, to make sure you really understand how to use the ODE solver, why not start with a very simple ODE where you know the exact answer (i.e. harmonic oscillator).