Clear Filters
Clear Filters

Laplace with Heaviside step function solving with ode45

3 views (last 30 days)
I dont understand why I keep getting an error, can you please explain to me why and how I can fix this?
syms s t Y
f = heaviside(t-1)- heaviside(t-2);
X = laplace(f);
Sol = X ./(s^2+3*s+2);
sol = symfun(ilaplace(Sol),t);
pretty(sol)
/ exp(2 - 2 t) 1 \ / exp(4 - 2 t) 1 \ heaviside(t - 1) | ------------ - exp(1 - t) + - | - heaviside(t - 2) | ------------ - exp(2 - t) + - | \ 2 2 / \ 2 2 /
tspan = [0 5];
ic = [0 0];
fn = @(t,Y) [Y(2); (heaviside(t-1)- heaviside(t-2) - 3*Y(2) - 2* Y(1))];
[t,Y] = ode45(fn,tspan,ic);
Index exceeds the number of array elements. Index must not exceed 1.

Error in indexing (line 968)
R_tilde = builtin('subsref',L_tilde,Idx);

Error in solution>@(t,y)[Y(2);(u-Y(3)-3*Y(2)-2*Y(1))] (line 12)
fn = @(t,y) [Y(2); (u - Y(3) - 3*Y(2) - 2* Y(1))];

Error in odearguments (line 92)
f0 = ode(t0,y0,args{:}); % ODE15I sets args{1} to yp0.

Error in ode45 (line 104)
odearguments(odeIsFuncHandle,odeTreatAsMFile, solver_name, ode, tspan, y0, options, varargin);
hold on
plot(t,y(:,1))
fplot(sol, [0 5])
grid on
xlabel('x')
ylabel('y')
legend('Ode45' ,'Solved' )
hold off

Accepted Answer

Torsten
Torsten on 28 Nov 2023
Edited: Torsten on 28 Nov 2023
You try to access Y(3), but you have only two differential equations for Y(1) and Y(2), I guess.
And the "Y" must be "y" because your list of inputs is t and y, not t and Y.
And ode45 is a numerical solver - it does not accept symbolic inputs like your "u".
  3 Comments

Sign in to comment.

More Answers (0)

Categories

Find more on Symbolic Math Toolbox in Help Center and File Exchange

Products

Community Treasure Hunt

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

Start Hunting!