I wonder why two solutions are not the same:
The differential equation is:
ds(t)/dt=A*s(t)+B(t)
with initial condition
s(0)=s0
A is a 2*2 constant matrix, s(t) is a 2*1 variable vector and B(t) is a 2*1 vector. I have seen the answer in different references as:
s(t)=exp(A*t)*s0+integral(exp(A*s)*B(s),s,0,t)
but the numerical method and the above answer are not the same.
I wonder if someone could help.
function ForwardForward
clc;clear;close all;
tf=2;
N=99;
s0=[-1;2];
[t,s]=ode45(@sysB,0:tf/N:tf,s0);
figure(1);hold all;plot(t,s(:,1))
figure(2);hold all;plot(t,s(:,2))
t=0:tf/N:tf;
for i=1:length(t)
A=[-1,-2;-3,-4];
syms s
m=expm(A*t(i))*s0+eval(int(expm(A*s)*[-s^2;exp(-s)],s,0,t(i)));
n1(i)=m(1);
n2(i)=m(2);
end
figure(1);plot(t,n1,'--r');legend('Numerical','Closed Form')
figure(2);plot(t,n2,'--r');legend('Numerical','Closed Form')
function sdot=sysB(t,s)
A=[-1,-2;-3,-4];
B=[-t^2;exp(-t)];
sdot=A*[s(1);s(2)]+B;
The solution is $~e^{At}\color{Red}{\left(\color{Black}{s_0+\int_0^te^{-A\tau}b(\tau)d\tau}\right)}~$ not $~e^{At}s_0+\int_0^te^{-A\tau}b(\tau)d\tau$.
The differential equation may be solved as follows:
$$s'=As+b(t) \quad \Rightarrow \quad s'-As=b(t) \quad \Rightarrow \quad e^{-At}(s'-As)=e^{-At}b(t)$$
$$\Rightarrow \quad (e^{-At}s)'=e^{-At}b(t) \quad \Rightarrow \quad e^{-At}s=s_0+\int_0^te^{-A\tau}b(\tau)d\tau$$
$$\implies s=e^{At}\left(s_0+\int_0^te^{-A\tau}b(\tau)\right) $$