MATLAB Answers

Solving First order ODE with constant coefficient

7 views (last 30 days)
Alexandra Reyes
Alexandra Reyes on 1 Aug 2021
Edited: Bjorn Gustavsson on 1 Aug 2021
How do i fix y= sym('y(t)') as it always show error and could not run the program properly.
%%Solving First Order ODEs with constant coefficients
syms t s Y
y = syms('y(t)'); % writing y as a function of t
y1 = diff(y,t); % Derivative of y(t) in symbolic form
a = input('Enter the coefficient of dy/dt in the equation');
b = input('Enter the coefficient of y in the equation');
f = input('Enter the inhomogenous part of the equation');
y0 = input('The initial condition is y(0)-');
eqnt = a*y1 + b*y - f; % Differential equation in the form eqnt - 0
eqns = laplace (eqnt, s); %Taking Laplace Transform of the DE
eqns = subs(eqns, {'laplace (y(t), t,s)','y(0)'}, {Y,y0});
Y = simplify (solve(eqns, Y)); %Solving algebraic equation eqns - 0 for Y and simplifying
y = ilaplace (Y, s, t); %Taking inverse laplce transform of Y to get y
disp(['The solution of the differential equation is: y(t)', char(y)])

Answers (1)

Bjorn Gustavsson
Bjorn Gustavsson on 1 Aug 2021
Edited: Bjorn Gustavsson on 1 Aug 2021
Should be as simple as:
sym y(t)
Also: isn't direct use of dsolve enough for you?

Community Treasure Hunt

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

Start Hunting!