Errors while trying to solve a simple differential equation

1 view (last 30 days)
Why am I getting all these errors while solving this simple differential equation?
syms y t a b
eqn = diff(y,t,2) == a^2*y;
Dy = diff(y,t);
cond = [y(0)==b, Dy(0)==1];
ySol(t) = dsolve(eqn,cond)
??? Error using ==> sym.sym>checkindex at 2590
Index must be a positive integer or logical.
Error in ==> sym.sym>privformatscalar at 2540
checkindex(x);
Error in ==> sym.sym>privformat at 2524
s = privformatscalar(x);
Error in ==> sym.sym>sym.subsref at 1364
[inds{k},refs{k}] = privformat(inds{k});

Answers (1)

Walter Roberson
Walter Roberson on 19 Oct 2017
You need
syms y(t)
You are currently just using y so MATLAB do not know that it is a function and thinks it is a constant. Constant differentiated gives 0
  2 Comments
naygarp
naygarp on 19 Oct 2017
Edited: Walter Roberson on 19 Oct 2017
I previously tried going with 'syms y(t)' but it shows that the format is not recognized.
syms y(t) a b
eqn = diff(y,t,2) == a^2*y;
Dy = diff(y,t);
cond = [y(0)==b, Dy(0)==1];
ySol(t) = dsolve(eqn,cond)
??? Error using ==> syms at 61
Not a valid variable name.
Walter Roberson
Walter Roberson on 19 Oct 2017
You must be using a older version of MATLAB. Try
syms a b t
y = sym('y(t)');
y0 = subs(y,t,0);
Dy = diff(y,t);
Dy0 = subs(Dy, t, 0);
D2y = diff(Dy,t);
eqn = D2y == a^2*y;
cond = [y0 == b, Dy0 == 1];
ySol = dsolve(eqn, cond(1), cond(2))

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!