How to give a piecewise function as input
6 views (last 30 days)
Show older comments
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
0 Comments
Answers (2)
Robert U
on 18 Nov 2021
Hi Mehul kumar,
Please, have a look here: https://de.mathworks.com/matlabcentral/answers/359268-making-ramp-and-unit-step-function-in-matlab?s_tid=srchtitle
Kind regards,
Robert
0 Comments
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): ');
eqn1=a*d2y(t)+b*dy(t)+c*y(t)-subs(nh,t,0)
eqn2=a*d2y(t)+b*dy(t)+c*y(t)-subs(nh,t,pi)
% 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)=')
disp(sol)
fplot(sol,[-1 1])
0 Comments
See Also
Categories
Find more on Calculus in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!