# How to give a piecewise function as input

9 views (last 30 days)
Mehul kumar on 18 Nov 2021
Answered: VBBV on 28 Mar 2022
clear all
clc
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F = input('Input the coefficients [a,b,c]: ');
a=F(1);b=F(2);c=F(3);
nh = input('Enter the non-homogenous part f(x): ');
eqn=a*d2y(t)+b*dy(t)+c*y(t)-nh;
LTY=laplace(eqn,t,s);
IC = input('Enter the initial conditions in the form [y0,Dy(0)]: ');
y0=IC(1);
dy0=IC(2);
LTY=subs(LTY,{laplace(y(t), t, s),y(0),dy(0)},{Y,y0,dy0});
eq=collect(LTY,Y);
Y=simplify(solve(eq,Y));
yt=simplify(ilaplace(Y,s,t));
disp('The solution of the differential equation y(t)=')
disp(yt);
lhs input:
[1,0,16]
rhs input:
F(t)=cos4t 0<t<pi
o t>pi

Robert U on 18 Nov 2021
Hi Mehul kumar,
Kind regards,
Robert

VBBV on 28 Mar 2022
clear all
clc
syms t s y(t) Y
dy(t)=diff(y(t));
d2y(t)=diff(y(t),2);
F = [-1 1 16]; %input('Input the coefficients [a,b,c]: ');
a=F(1);b=F(2);c=F(3);
nh = piecewise(t>0 & t< pi, cos(4*t), 0) %input('Enter the non-homogenous part f(x): ');
nh = eqn1=a*d2y(t)+b*dy(t)+c*y(t)-subs(nh,t,0)
eqn1 = eqn2=a*d2y(t)+b*dy(t)+c*y(t)-subs(nh,t,pi)
eqn2 = % LTY=laplace([eqn1 eqn2],t,s)
IC = [y(0) == 1; dy(0) == 0];% input('Enter the initial conditions in the form [y0,Dy(0)]: ');
y0=IC(1);
dy0=IC(2);
% LTY=subs(LTY,[laplace(y(t), t, s),y(0),dy(0)],[Y,y0,dy0])
% eq=collect(LTY,Y)
sol =simplify(dsolve([eqn1 eqn2],IC));
% yt=simplify(ilaplace(y,s,t))
disp('The solution of the differential equation y(t)=')
The solution of the differential equation y(t)=
disp(sol) fplot(sol,[-1 1]) ### Categories

Find more on Mathematics in Help Center and File Exchange

R2019a

### Community Treasure Hunt

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

Start Hunting!