MATLAB: Numerical Differential Equation Matrix Form with Forcing Function

differential equationsMATLABmatricesmatrixode45

I'm trying to solve a fourth order differential equation with a forcing function t*e^(2t) with ODE45 in matrix form. The method below works fine:
F = @(t,y) [
y(2);
y(3);
y(4);
t*exp(2*t)+4*y(2)-3*y(3)+12*y(1)]
y0 = [0 0 0 -1/4]';
ode45(F, [0 4], y0);
But when I try a matrix form of the system, the forcing function returns an error for an undefined t.
A = [0 1 0 0
0 0 1 0
0 0 0 1
12 4 -3 0]
b = [0 0 0 -t*exp(2*t)]'
y0 = [0 0 0 -1/4]'
F = @(t, Y, A, b) A*Y+b;
ode45(@(t,Y) F, [0,4], y0)
I've used this method for undriven systems of first-order equations. Does anyone know how to modify the code to fix the error?

Best Answer

You need to make ‘b’ a function of ‘t’, and call it appropriately in ‘F’. Then it works:
A = [0 1 0 0
0 0 1 0
0 0 0 1
12 4 -3 0]
b = @(t) [0 0 0 -t*exp(2*t)]'
y0 = [0 0 0 -1/4]'
F = @(t, Y, A, b) A*Y+b(t);
[T,Y] = ode45(@(t,Y) F(t, Y, A, b), [0,4], y0);
figure(1)
plot(T,Y)
grid