%Aufgabe 6.1, Beispiel fuer mehrdimensionale Variation der Konstanten
%Copyright (C) 2019  Sascha Eichmann
%
%    This program is free software: you can redistribute it and/or modify
%    it under the terms of the GNU General Public License as published by
%    the Free Software Foundation, either version 3 of the License, or
%    (at your option) any later version.
%
%    This program is distributed in the hope that it will be useful,
%    but WITHOUT ANY WARRANTY; without even the implied warranty of
%    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%    GNU General Public License for more details.
%
%    You should have received a copy of the GNU General Public License
%    along with this program.  If not, see <https://www.gnu.org/licenses/>
clear;

% Koeffizienten der Differentialgleichung
A=[1 0 0 2 0.1;0 1 0 0.5 -1; 0 -1 1 0 0.3; 0 3 0 1 0; -2 0 1 0 1];
 
b=@(t) [3*cos(t); exp(t); t.^2; -t; sqrt(abs(t))];

%Anfangsdaten:
x0=[0.5;1;0.5;-1;5];
t0=-1;

%Maximalzeit
tMax=0;

%Integrand in Variation der Konstanten, e^(-At) = (e^(At))^-1
integrand=@(t) (expm(-t.*A))*(b(t));

%Loesung mithilfe der Variation der Konstanten
x=@(t) expm(t.*A)*(expm(-t0.*A))*x0 + expm(t.*A)*quadv(integrand, t0, t);

%Plotten der ersten Komponente:
%Zeithorizont
tZeit=[t0:0.05:tMax];
%Initialisieren der Hilfsvariabale für die erste Komponente
x1=[];
for t=tZeit
  help=x(t); %Berechnen der Loesung zum Zeitpunkt t
  x1=[x1;help(1)]; %Speichern des Wertes
end
%Plotten der ersten Komponente
plot(tZeit,x1);