Numerical Differential Equation Matrix Form with Forcing Function

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?

2 Comments

It's a bit difficult to see what's going on when your original ode has been reduced to 4 systems of equations as seen in your code, especially in the form as a matrix. Could you post the original equations?
The original equation is y''''[t]+3*y''[t]-4*y'[t]-12*y[t]=t*exp(2*t) with y=y'=y''=0 at t=0

Sign in to comment.

 Accepted 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

2 Comments

Thanks! I tried b = @(t) etc. but forgot to change
F = @(t, Y, A, b) A*Y+b
to
F = @(t, Y, A, b) A*Y+b(t)

Sign in to comment.

More Answers (0)

Products

Release

R2018a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!