MATLAB Code for Book’s Solved Examples

This file contains MATLAB code for the majority of the book’s solved examples, covering Chapters 2 through 11. Excepted are examples involving simple algebraic calculations and those designed to generate mathematical models, which can or need to be solved analytically by regular hand calculation. The MATLAB Version 7.6 (Release 2008a) has been used throughout, which was the newest version at the time this project started.

The code of individual examples can be copied, pasted, and directly run into MATLAB’s Command Window. Most codes in this online supplement are formed of an algebraic section (where symbolic calculation is utilized to derive equations), followed by a numerical part (where the symbolic formulae are vectorized and used in conjunction with parameter numerical values in order to obtain plots for instance). In the latter section, parameter numerical values are defined and the symbolic result of the former section (which actually results by running the copied- pasted code) is rewritten to account for vector variables syntax. Depending on the MATLAB version you are currently using, the algebraic equations yielded in the symbolic section of a solved example might slightly differ from the ones proposed herein, so you need to accordingly modify the numerical section. Once the needed alterations have been applied, the basic MATLAB code offered here can be saved as a script (or .m) file for further simulation.

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 1 Chapter 2

Example 2.1

>> syms r1 r2 r h x rho

>> rx=r1+(r2-r1)/h*x;

>> da=2*pi*r;

>> J=simplify(rho*int(int(r^2*da,r,0,rx),x,0,h))

>> Jc=limit(J,r2,r1) %calculates the cylinder mass moment of inertia

>> mc=rho*pi*r1^2*h %calculates the cylinder mass

>> Jc1=Jc+mc*(2*r1)^2 %calculates the mass moment of inertia of displaced cylinder

Example 2.2

>> d=0.001;

>> R=0.006;

>> n=10;

>> G=160e9;

>> k=G*d^4/(64*n*R^3)

>> k1=5*k/2

>> k2=2*k/5

Example 2.3

>> syms v g z mu A

>> vz=z/g*v;

>> tau=mu*diff(vz,'z');

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 2 >> fd=tau*A;

>> c=fd/v

Example 2.4

>> %natural frequency without mass of lever

>> la=0.5;

>> lb=1;

>> lc=1.5;

>> ka=1000;

>> mb=1;

>> mc=(lb/lc)^2*mb;

>> kc=(la/lc)^2*ka;

>> om=sqrt(kc/mc)

>> %natural frequency with mass of lever

>> ml=mb/4;

>> Jo=ml*lc^2/3+mc*lc^2;

>> k=kc*lc^2;

>> omn=sqrt(k/Jo)

>> %plot of time response

>> t0=0.1;

>> theta0=4;

>> t=0:0.001:2;

>> theta=theta0/sin(omn*t0)*sin(omn*t);

>> plot(t,theta)

>> xlabel('Time (sec)')

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 3 >> ylabel('\theta (deg)')

>> grid on

Example 2.5

>> j1=0.001;

>> j2=0.0008;

>> n1=32;

>> n2=26;

>> k1=80;

>> k2=200;

>> ke1=k1+(n1/n2)^2*k2;

>> je1=j1+(n1/n2)^2*j2;

>> omn=sqrt(ke1/je1)

>> [k1,k2]= meshgrid(80:2:120,140:2:200);

>> omn = sqrt((k1+n1^2/n2^2*k2)/(j1+n1^2/n2^2*j2));

>> surf(k1,k2,omn)

>> colormap(gray)

>> xlabel('k_1 (N/m)')

>> ylabel('k_2 (N/m)')

>> zlabel('\omega_n (rad/s)')

Example 2.6

>> delta=log(2)/5

>> xi=delta/sqrt(4*pi^2+delta^2)

Example 2.7

>> n1=48;

>> n2=36;

>> c1=20;

>> c2=40;

>> ce=c1*(n2/n1)^2+c2

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 4 Chapter 3

Example 3.1

>> l=310e-6;

>> a=150e-6;

>> b=320e-6;

>> h=50e-6;

>> d=20e-6;

>> rho=5600;

>> Jpx=rho*a*b*h*(a^2+h^2)/12

>> Jbx=rho*l*pi*d^4/32;

>> Jx=Jpx+2*Jbx/3

>> Jpy=rho*a*b*h*(b^2+h^2)/12

>> Jy=Jpy+2*13/35*rho*l*pi*d^2/4*b^2/4

Example 3.2

>> d=2e-6;

>> l=100e-6;

>> E=160e9;

>> iy=pi*d^4/64;

>> ks=12*E*iy/l^3;

>> kl=12*E*iy/(2*l)^3;

>> kes=ks/2*kl/(ks/2+kl);

>> ke=2*kes

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 5 Example 3.3

>> d1=2e-6;

>> d2=1e-6;

>> l1=100e-6;

>> l2=140e-6;

>> G=110e9;

>> thmax=5*4*atan(1)/180;

>> it1=pi*d1^4/32;

>> it2=pi*d2^4/32;

>> k1=G*it1/l1;

>> k2=G*it2/l2;

>> ks=k1*k2/(k1+k2);

>> ke=2*ks

>> mmax=ke*thmax

Example 3.4

>> %natural frequency with beams inertia

>> a=3e-6;

>> m=8e-11;

>> l1=220e-6;

>> l2=80e-6;

>> E=160e9;

>> rho=5200;

>> I=a^4/12;

>> k1=12*E*I/l1^3;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 6 >> k2=12*E*I/l2^3;

>> ke=2*k1*k2/(k1+k2);

>> mb=rho*a^2*l1;

>> mbe=13/35*mb;

>> 2*mbe

>> me=m+2*mbe;

>> omn=sqrt(ke/me)

>> %natural frequency without beams inertia

>> omnp=sqrt(ke/m)

>> delom=(omnp-omn)/omnp

Example 3.5

>> l1=100e-6;

>> l=200e-6;

>> h=20e-6;

>> d=h;

>> w=100e-6;

>> E=180e9;

>> G=140e9;

>> rho=5000;

>> ombp=4.1139e6;

>> omtp=5.0222e6;

>> I=pi*d^4/64;

>> kb=12*E*I/l1^3;

>> It=pi*d^4/32;

>> kt=G*It/l1;

>> m=rho*l*w*h;

>> J=m*(w^2+h^2)/12;

>> omb=sqrt(2*kb/m)

>> omt=sqrt(2*kt/J)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 7 >> mp=(omb^2-ombp^2)/ombp^2*m

>> b=sqrt((omt^2-omtp^2)/omtp^2*J/mp)

Example 3.8

>> %natural frequencies calculation

>> k1=884;

>> k2=k1/2;

>> J1=0.002;

>> J2=J1/2;

>> syms om

>> solve(det([-om^2*J1+k1,-k1;-k1,-om^2*J2+k1+k2]),om)

>> %the two positive values are om1 and om2 used below

>> %plots of modal motions

>> om1=344;

>> om2=1284;

>> t=0:0.00001:0.06;

>> theta11=180/pi/(sqrt(3)-1)*sin(om1*t);

>> theta21=180/pi*sin(om1*t);

>> subplot(1,2,1)

>> plot(t,theta11,t,theta21,'--')

>> grid on

>> xlabel('Time(sec)')

>> ylabel('Rotation angles (deg)')

>> legend('\theta_1_1','\theta_2_1')

>> t=0:0.00001:0.01;

>> theta12=-180/pi/(sqrt(3)+1)*sin(om2*t);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 8 >> theta22=180/pi*sin(om2*t);

>> subplot(1,2,2)

>> plot(t,theta12,t,theta22,'--')

>> grid on

>> xlabel('Time(sec)')

>> legend('\theta_1_2','\theta_2_2')

Example 3.9

>> j1 = 0.002;

>> j2 = j1/2;

>> k1 = 884;

>> k2 = k1/2;

>> in = [j1,0;0,j2];

>> stiff = [k1,-k1;-k1,k1+k2];

>> d = inv(in)*stiff;

>> [V,D] = eig(d)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 9 Chapter 4

Example 4.1

>> syms rab rbc rca rad rbd rcd

>> eq1=rad+rbd-rab*(rbc+rca)/(rab+rbc+rca);

>> eq2=rbd+rcd-rbc*(rca+rab)/(rab+rbc+rca);

>> eq3=rcd+rad-rca*(rab+rbc)/(rab+rbc+rca);

>> s=solve(eq1,eq2,eq3,rad,rbd,rcd);

>> % algebraic solution:

>> s.rad

>> s.rbd

>> s.rcd

>> %numerical solution

>> rab=250;

>> rbc=220;

>> rca=280;

>> %the equations below are obtained by the s.rad, s.rbd, s.rcd

>> rad=1/(rab+rbc+rca)*rab*rca

>> rbd=1/(rab+rbc+rca)*rab*rbc

>> rcd=1/(rab+rbc+rca)*rbc*rca

Example 4.2

>> rx=0:0.00001:0.9;

>> rf=1./(1-rx).^2;

>> plot(rx,rf)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 10 >> grid on

>> xlabel('x/g_0')

>> ylabel('f/f_0')

Example 4.9

>> %symbolic solution

>> syms r1 r2 r3 r4 r5 v

>> a = [r1+r5,0,r4;0,r2+r3,-r4;1,-1,-1];

>> f = [v;0;0];

>> i = inv(a)*f

>> %numerical solution

>> r1=20;

>> r2=30;

>> r3=10;

>> r4=40;

>> r5=5;

>> v=40;

>> %the following equations are obtained by the i=inv(a)*f command above

>> i1=(r2+r3+r4)/(r1*r2+r1*r3+r1*r4+r5*r2+r5*r3+r5*r4+r4*r2+r4*r3)*v

>> i2=r4/(r1*r2+r1*r3+r1*r4+r5*r2+r5*r3+r5*r4+r4*r2+r4*r3)*v

>> i3=(r2+r3)/(r1*r2+r1*r3+r1*r4+r5*r2+r5*r3+r5*r4+r4*r2+r4*r3)*v

Example 4.10

>> %symbolic solution

>> syms r1 r2 r3 r4 i v

>> a = [1/r1+1/r2,-1/r2;-1/r2,1/r2+1/r3+1/r4];

>> f = [i;v/r4];

>> v = inv(a)*f

>> %numerical solution

>> clear

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 11 >> r1=50;

>> r2=70;

>> r3=60;

>> r4=40;

>> i=0.1;

>> v=80;

>> va1=(r4*r3+r4*r2+r2*r3)*r1/(r4*r3+r4*r2+r2*r3+r1*r4+r1*r3)*i;

>> va2=1/(r4*r3+r4*r2+r2*r3+r1*r4+r1*r3)*r1*r3*v;

>> va=va1+va2

>> vc1=1/(r4*r3+r4*r2+r2*r3+r1*r4+r1*r3)*r1*r3*r4*i;

>> vc2=+(r2+r1)*r3/(r4*r3+r4*r2+r2*r3+r1*r4+r1*r3)*v;

>> vc=vc1+vc2

>> i1=va/r1

>> i2=(va-vc)/r2

>> i3=vc/r3

Example 4.12

>> %symbolic calculation

>> syms l1 l2 c om

>> solve(det([1/c-om^2*l1,-1/c;-1/c,1/c-om^2*l2]),om)

>> %numerical calculation

>> l1=0.5;

>> l2=0.3;

>> c=0.02;

>> om=1/l1/c/l2*(l1*c*l2*(l2+l1))^(1/2)%obtained by symbolic calculation above

>> %modal charge vector

>> Q=[1/(1-om^2*l1*c);1]

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 12 Example 4.13

>> l1=50e-3;

>> l2=l1;

>> c1=800e-6;

>> c2=c1;

>> l=[l1+l2,-l2;-l2,l2];

>> c=[1/c1,0;0,1/c2];

>> d=inv(l)*c;

>> [V,D]=eig(d)

>> om1=sqrt(D(1,1))

>> om2=sqrt(D(2,2))

Example 4.14

>> xi1=0.45;

>> omn1=1200*2*pi;

>> xi2=xi1*0.75;

>> omn2=omn1*1.3;

>> r=250; v=100;

>> l=r/(2*xi2*omn2);

>> c2=2*xi2/(r*omn2);

>> dsolve('D2q+2*0.3375*1560*2*pi*Dq+(1560*2*pi)^2*q=0','q(0)=100*2.75e-7','Dq(0)=0')

>> t=0:0.00000001:0.003;

>> pi=4*atan(1);

>> %the charge below is obtained by the symbolic dsolve command above

>> %it is written as the sum of two terms, q1 and q2

>> q1=297/2268400000*5671^(1/2)*exp(-1053*pi*t).*sin(39*5671^(1/2)*pi*t);

>> q2=11/400000*exp(-1053*pi*t).*cos(39*5671^(1/2)*pi*t);

>> q=q1+q2;

>> plot(t,q)

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 13 >> xlabel('Time (sec)')

>> ylabel('Charge (C)')

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 14 Chapter 5

Example 5.1

>> d=0.02;

>> l=6;

>> mu=1000;

>> mu=0.00001;

>> rho=1000;

>> q=0.0001;

>> g=9.81;

>> v1=4*q/(pi*d^2);

>> hf=32*mu*l*v1/(rho*g*d^2);

>> w=g*(l+hf)

Example 5.2

>> syms w1 w2 h l x rho

>> w = w1+(w2-w1)/l*x;

>> in =limit(int(1/w,x),x,l,'left')-limit(int(1/w,x),x,0,'right');

>> inertance = simplify(rho/h*in) %inertance of trapezoid block

>> limit(inertance,w2,w1) %inertance of parallelipipedic block

Example 5.3

>> syms d1 d2 h rho g x

>> d=d1+(d2-d1)/h*x;

>> a=pi*d^2/4;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 15 >> v=int(a,x,0,h);

>> cp=simplify(diff(v,h)/(rho*g))%capacitance of conical vessel

>> cpcyl=simplify(limit(cp,d2,d1))%capacitance of cylindrical vessel

Example 5.4

>> %symbolic solution

>> syms qv

>> delp=qv+3*qv^2;

>> rlin=simplify(delp/qv)%linear resistance

>> r=diff(delp,qv)%nonlinear resistance

>> cr=simplify(r/rlin)

>> crmax=limit(cr,qv,inf)

>> %plot of cR

>> qv=0:0.001:20;

>> cr=(1+6*qv)./(1+3*qv);%obtained in the symbolic section above

>> plot(qv,cr)

>> grid on

>> xlabel('q_v (m^3/s)')

>> ylabel('c_R')

>> %numerical solution

>> qv1=0.01;

>> qv2=2;

>> cr1=(1+6*qv1)/(1+3*qv1)

>> cr2=(1+6*qv2)/(1+3*qv2)

Example 5.5

>> %symbolic calculation

>> syms d1 d2 l x mu qv

>> d=d2+(d1-d2)*(l-x)/l;

>> ddelp=128*mu*qv/pi/d^4;

>> p=limit(int(ddelp,x),x,l)-limit(int(ddelp,x),x,0);

>> r=simplify(p/qv)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 16 >> %numerical calculation

>> d1=0.5;

>> d2=0.3;

>> l=10;

>> mu=0.001;

>> r=128/3*(d1^2+d2*d1+d2^2)*mu*l/d1^3/d2^3/pi % symbolically obtained above

Example 5.6

>> l=100e-6;

>> mu=0.0005;

>> d=20e-6;

>> delp=1000;

>> r=128*mu*l/(pi*d^4);

>> re=3*r;

>> u=delp^2/(2*re)

Example 5.8

>> %symbolic calculations

>> syms c i om

>> c1=c;

>> c2=c;

>> i1=i;

>> i2=i;

>> solve(det([1/c1-om^2*i1,-1/c1;-1/c1,1/c1+1/c2-om^2*i2]),om)

>> %numerical calculations

>> %from symbolic calculation

>> om1=simple(-1/2*(5^(1/2)-3)*2^(1/2)/(i*c*(3-5^(1/2)))^(1/2))

>> om2=simple(1/2*(3+5^(1/2))*2^(1/2)/(i*c*(3+5^(1/2)))^(1/2))

>> %modes

>> syms v11 v12 v21 v22

>> s1=solve(v11-v12*1/(1-om1^2*i*c),sqrt(v11^2+v12^2)-1,v11,v12);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 17 >> %the following are actual equations for v11 and v12

>> v11=simple(s1.v11)

>> v12=simple(s1.v12)

>> s2=solve(v21-v22*1/(1-om2^2*i*c),sqrt(v21^2+v22^2)-1,v21,v22);

>> %the following are actual equations for v21 and v22

>> v21=simple(s2.v21)

>> v22=simple(s2.v22)

>> %separate copy + paste + enter calculation of v11, v12, v21, v22 is needed

Example 5.9

>> c=3e-8;

>> i=2e6;

>> i1=i;

>> i2=i;

>> c1=c;

>> c2=c;

>> in=[i1,0;0,i2];

>> ca=[1/c1,-1/c1;-1/c1,1/c1+1/c2];

>> d=inv(in)*ca;

>> [v,d]=eig(d)

>> om1=sqrt(d(1,1))

>> om2=sqrt(d(2,2))

Example 5.10

>> syms r c qo

>> dsolve('r*c*Dqo+qo=q*t','qo(0)=0')

>> clear

>> r=5e4;

>> c=2e-6;

>> q=1e-4;

>> t=0:0.0001:10;

>> %qo obtained symbolically by the dsolve command

>> qo=-q*r*c+q*t+exp(-1/r/c*t)*q*r*c;

>> plot(t,qo)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 18 >> xlabel('Time (sec)')

>> ylabel('q_o (m^3/sec)')

>> grid on

Example 5.13

>> l=0.01;

>> a=20;

>> thi=23;

>> tho=5;

>> k1=0.15;

>> k2=4*k1;

>> re=3*l/(k1*a);

>> q=(thi-tho)/re

>> thip=2/3*thi+1/3*tho

Example 5.14

>> h=4;

>> w=6;

>> l1=0.1;

>> l2=0.004;

>> a=w*h;

>> k1=0.1;

>> k2=0.0002;

>> ho=25;

>> hi=0.1;

>> th1=25;

>> th2=-10;

>> r=1/a*(1/ho+l2/k2+l1/k1+1/hi);

>> q=(th1-th2)/r

>> tho=th2+q/(ho*a)

>> thi=th1-q/(hi*a)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 19 Chapter 6

Example 6.1

>> syms t

>> syms s positive% identify s as positive to enable limit calculations in integrals

>> int(exp(-s*t)*t,t,0,inf)

>> int(exp(-s*t)*t^2,t,0,inf)

Example 6.2

>> syms t

>> syms s om positive

>> simplify(int(exp(-s*t)*sin(om*t),t,0,inf))

Example 6.3

>> syms s

>> pretty(simplify(ilaplace(3/(s^2+0.2*s+30))))

Example 6.4

>> syms t

>> syms a om s positive

>> f1t=t*exp(-a*t)*sin(om*t);

>> f2t=t*exp(-a*t)*cos(om*t);

>> F1s=simplify(int(exp(-s*t)*f1t,t,0,inf))

>> F2s=simplify(int(exp(-s*t)*f2t,t,0,inf))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 20 Example 6.5

>> syms tau a positive

>> syms t

>> ps=a/tau*laplace(1-heaviside(t-tau))

>> syms s positive

>> is=limit(p,tau,0)

Example 6.6

>> syms t a

>> f=t*heaviside(t)-a*heaviside(t-a)-a*heaviside(t-2*a)-(t-2*a)*heaviside(t-2*a);

>> laplace(f)

Example 6.8

>> syms s i0 l r c v

>> i = v/(s*(l*s+r))+l*i0/(l*s+r);

>> limit(s*i,s,0,'right')

>> limit(s*i,s,inf,'left')

Example 6.9

>> syms t s positive

>> num=simplify(int(exp(-s*t)*sin(pi/2*t),t,0,2));

>> Fs=num/(1-exp(-4*s))

Example 6.10

>> syms s a b t tau

>> f=1/(s^2+a^2);

>> g=1/(s^2+b^2);

>> f1=subs(ilaplace(f),t,tau);

>> g1=subs(ilaplace(g),t,t-tau);

>> x=simplify(1/(a*b)*int(f1*g1,tau,0,t))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 21 Example 6.12

>> syms s

>> collect((s^2+3*s+2)*(s^2+1))

>> [r,p,k]=residue([1,3],[1,3,3,3,2])%after collecting the coefficients above

>> f=ilaplace((-0.2000)/(s+2)+(1)/(s+1)+(-0.4000 - 0.3000i)/(s-(-0.0000 + 1.0000i)) ...

+(-0.4000 + 0.3000i)/(s-(-0.0000 - 1.0000i)))%after using the residues and poles above

Example 6.13

>> syms s

>> collect(s*(s+1)^2)

>> [r,p,k]=residue(1,[1,2,1,0])%after collecting the coefficients above

>> f=ilaplace(-1/(s+1)^2-1/(s+1)+1/s)

Example 6.14

>> n=[1,-8,15];

>> d=[1,7,18,20,8];

>> [r,p,k]=residue(n,d)

Example 6.15

(a)

>> syms s

>> collect(s*(s^2+3*s+2))

>> [r,p,k]=residue(2,[1,3,2,0])%after collection of coefficients from above

>> x=ilaplace(1/(s+2)-2/(s+1)+1/s)%after collection from residue command

(b)

>> syms s

>> collect(s^2*(s^2+3*s+2))

>> [r,p,k]=residue(2,[1,3,2,0,0]) %after collection of coefficients from above

>> x=ilaplace(-0.5/(s+2)+2/(s+1)-1.5/s+1/s^2) %after collection from residue command

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 22 Example 6.16

>> syms t

>> f=2*sin(t);

>> pretty(laplace(f))

>> syms s

>> X=2/((s^2+1)*(s+1)*(s+2));

>> pretty(ilaplace(X))%obtained symbolically

>> %obtained through partial fraction expansion

>> collect((s^2+1)*(s+1)*(s+2))

>> [r,p,k]=residue(2,[1,3,3,3,2])%from residue command

>> x=ilaplace(-0.4/(s+2)+1/(s+1)-(0.3+0.1*j)/(s-j)+(-0.3+0.1*j)/(s+j))

Example 6.17

>> syms s

>> r=200;

>> l=20;

>> c=10e-6;

>> v=100;

>> simplify(ilaplace(inv([r+l*s,r;-l*s,1/c/s])*[v/s;0]))

>> t=0:0.00001:0.5;

>> %the following current expressions are rewritten from above

>> il=0.5-(0.5*cosh(239.8*t)+0.52*sinh(239.8*t)).*exp(-250*t);

>> ic=(0.5*cosh(239.8*t)-0.52*sinh(239.8*t)).*exp(-250*t);

>> subplot(2,1,1)

>> plot(t,il)

>> ylabel('i_L (A)')

>> grid on

>> subplot(2,1,2)

>> plot(t,ic)

>> ylabel('i_C (A)')

>> xlabel('Time (sec)')

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 23 Example 6.18

>> syms s m k

>> in=[m,0;0,m];

>> st=[2*k,-k;-k,k];

>> inv(s^2*in+st)*[0;1]

>> m=1e-6;

>> k=1e-6;

>> %x1s and x2s below are the ones found by symbolic inversion

>> x1s=collect(1e-12*k/(m^2*s^4+3*m*k*s^2+k^2))

>> x2s=collect(1e-12*(m*s^2+2*k)/(m^2*s^4+3*m*k*s^2+k^2))

>> %partial fraction expansion of x1s is next

>> [r,p,k]=residue(1/1000000000000000000,[1/1000000000000,0, ...

3713820117856141/1237940039285380274899124224,0,1/1000000000000])

>> x1=ilaplace((0.1382*j)/(s-1.6180*j)+(- 0.1382*j)/(s+1.6180*j) ...

+(- 0.3618*j)/(s-0.6180*j)+(0.3618*j)/(s+0.6180*j))

>> %partial fraction expansion of x2s is next

>> [r,p,k]=residue([1/1000000000000000000,0, ...

944473296573929/472236648286964521369600000000000], ...

[1/1000000000000,0,3713820117856141/1237940039285380274899124224,0,1/1000000000000])

>> x2=ilaplace((-0.0854*j)/(s-1.6180*j)+( 0.0854*j)/(s+1.6180*j) ...

+(- 0.5854*j)/(s-0.6180*j)+( 0.5854*j)/(s+0.6180*j))

Example 6.19

>> syms s

>> fs=1/(s+1);

>> gs=1/(s^2+1);

>> x=simplify(ilaplace(gs/(1+fs)))

Example 6.20

>> syms s xs

>> eq=s*xs+xs/s^2-4/s;

>> solve(eq,xs)

>> simplify(ilaplace(4*s/(s^3+1)))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 24 Example 6.21

>> syms s t xs

>> eq=s^2*xs+diff(laplace(sinh(t)),s);

>> solve(eq,xs)

>> %using the result obtained by command above, the following is applied

>> simplify(ilaplace(2/(s^2-1)^2/s))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 25 Chapter 7

Example 7.1

>> syms s

>> ys=1/(s^2+s+1);

>> simplify(ilaplace(ys))

Example 7.2

>> syms s c1 c2 r1 r2 i1 i2 i vi vo

>> eq1=1/c1/s*i1-r1*i+r1*i1;

>> eq2=1/c2/s*i2-r2*i+r2*i2;

>> s1=solve(eq1,eq2,i1,i2);

>> i1=s1.i1

>> i2=s1.i2

>> eq3=vi-r1*(i-i1)-r2*(i-i2);

>> eq4=vo-r2*(i-i2);

>> s2=solve(eq3,eq4,vi,vo);

>> vi=s2.vi;

>> vo=s2.vo;

>> g=simplify(vo/vi)

Example 7.3

>> syms s m1 m2 k1 k2 c

>> simplify(inv([m1*s^2+c*s+k1+k2,-k2;-k2,m2*s^2+k2]))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 26 Example 7.4

>> syms s r1 r2 c1 c2

>> z1=r1/(r1*c1*s+1);

>> z2=r2/(r2*c2*s+1);

>> g=simplify(z2/(z1+z2))

Example 7.7

>> syms s c1 c2 r1 r2

>> zc1=1/c1/s;

>> zc2=1/c2/s;

>> zr1=r1;

>> zr2=r2;

>> simplify(inv([(1+zc1/zr1)*c1*s,-(zc1/zr1)*c1*s; ...

-(zc2/zr1)*c2*s,(1+zc2/zr1+zc2/zr2)*c2*s]))

>> %the first row of the matrix above is multiplied by c1*s ...

%whereas the second row is multiplied by c2*s ...

%in order to obtain Qi1 and Qi2 in the right-hand sides of Eqs. (7.39)

Example 7.9

>> f = zpk([1,2],[-1,-2,-3],5)

>> g = tf(f)

>> zpk(g)

Example 7.10

>> c=1.2e-6;

>> r=30000;

>> syms t

>> pi=1e5*(1+4*exp(-0.2*t));

>> pis=laplace(pi);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 27 >> syms s

>> g=1/(r*c*s+1);

>> pos=g*pis;

>> po=simplify(ilaplace(pos))

>> t=0:0.001:25;

>> po=100000+500000000/1241*exp(-1/5*t)-624100000/1241*exp(-250/9*t);

>> plot(t,po)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('p_o (N/m^2)')

Example 7.11

>> %symbolic calculation

>> syms s r l v i0

>> %the following three equations are based on Eq. (7.68)

>> l1=ilaplace(1/s/(s+r/(2*l))^2)

>> l2=ilaplace(s/(s+r/(2*l))^2)

>> i=v/l*l1+i0*l2

>> %numerical calculation

>> r=200;

>> l=3;

>> i0=0.1;

>> v=100;

>> t=0:0.000001:0.35;

>> %the following equation is rewritten from the one obtained symbolically

>> i=v/l*(4*l^2/r^2-2*l*exp(-1/2*r*t/l).*(2*l+r*t)/r^2)+i0...

*(-1/2*exp(-1/2*r*t/l).*(-2*l+r*t)/l);%i= v/l*l1+i0*l2 from symbolic section

>> plot(t,i)

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 28 >> xlabel('Time (sec)')

>> ylabel('i (A)')

Example 7.12

>> r=300;

>> c=50e-6;

>> l=40e-3;

>> q0=0.1;

>> gp=tf([l*q0,r*q0,0],[l,r,1/c])

>> step(gp)

>> ylabel('Charge (C)')

Example 7.13

>> r=300;

>> c=50e-6;

>> l=40e-3;

>> q0=0.1;

>> d=40;

>> gp=tf([l*q0,r*q0+d],[l,r,1/c])

>> impulse(gp)

>> ylabel('Charge (C)')

Example 7.14

>> syms s

>> m=0.1;

>> c=0.2;

>> k=10;

>> y0=0.1;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 29 >> f=6/s+50/(s^2+1000)

>> g=1/(m*s^2+c*s+k);

>> gs=simplify((m*y0*s+c*y0+f)/(f));

>> gp=simplify(gs*g)

>> %after some conditioning, the transfer function of Eq. (7.87) is obtained

>> sys=tf([1,2,700,5200,60000],[60,620,13000,62000,600000]);

>> t=0:0.0001:6;

>> f=6+5*sin(10*t);

>> lsim(sys,f,t)

Example 7.15

>> c1=20;

>> c2=16;

>> r1=2;

>> r2=1.2;

>> h0=[0.1;0];

>> syms s

>> g=inv([1/r1+c1*s,-1/r1;-1/r1,1/r1+1/r2+c2*s]);

>> a1=[c1,0;0,c2];

>> y=g*a1*h0;

>> h1=ilaplace(y(1,1))

>> h2=ilaplace(y(2,1))

>> t=0:0.01:300;

>> %h1 and h2 are rewritten from the equations obtained above under h1= and h2=

>> h1=0.001*exp(-0.054*t).*(94*cosh(0.04*t)+68*sinh(0.04*t));

>> h2=0.077*sinh(0.04*t).*exp(-0.054*t);

>> plot(t,h1,t,h2)

>> plot(t,h1,t,h2,'--')

>> ylabel('Heads (m)')

>> legend('h_1','h_2')

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 30 Example 7.16

>> %symbolic calculation

>> syms m1 m2 c s y10 v20

>> a2=[m1,0;0,m2];

>> a1=[c,0;0,c];

>> y0=[y10;0];

>> v0=[0;v20];

>> ues=collect(s*a2*y0+a2*v0+a1*y0)

>> %ues = [(s*m1+c)*y10; m2*v20]

>> %the equation above gives the two components of the ues vector

>> %numerical calculation

>> m1=2e-10;

>> m2=3e-10;

>> c=0.01;

>> y10=2e-6;

>> v20=1e-6;

>> %calculation of numerical coefficients in symbolic ues

>> m1*y10

>> c*y10

>> m2*v20

Example 7.17

>> %symbolic calculation

>> syms s va vb r1 r2 c l i

>> zr1=r1;

>> zr2=r2;

>> zl=l*s;

>> zc=1/c/s;

>> eq1=(1/zr1+1/zr2)*va-1/zr2*vb-i;

>> eq2=-1/zr2*va+(1/zr2+1/zl+1/zc)*vb;

>> s1=solve(eq1,eq2,va,vb);

>> va=simplify(s1.va)

>> vb=simplify(s1.vb)

>> %numerical calculation

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 31 >> r1=400;

>> r2=550;

>> l=5;

>> c=250e-6;

>> i=20e-3;

>> %the following equations have been obtained symbolically in this code

>> %they are evaluated numerically next

>> va=r1*i*(l*s+r2+c*s^2*r2*l)/(l*s+r2+r1+c*s^2*r2*l+c*s^2*l*r1);

>> vb=r1*i*l*s/(l*s+r2+r1+c*s^2*r2*l+c*s^2*l*r1);

>> vat=simplify(ilaplace(va/s))

>> %it has been considered above that I(s)=i/s

>> vbt=simplify(ilaplace(vb/s))

>> %is has been considered above that I(s)=i/s

>> t=0:0.0001:3;

>> %the following are the simplified equations of va(t) and vb(t) obtained above

>> vat=4.63+0.5*sin(28.2*t).*exp(-2.1*t);

>> vbt=1.2*sin(28.2*t).*exp(-2.1*t);

>> subplot(2,1,1)

>> plot(t,vat)

>> grid on

>> ylabel('v_a (V)')

>> subplot(2,1,2)

>> plot(t,vbt)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('v_b (V)')

Example 7.18

>> g11 = tf([2],[1,2,100]);

>> g12 = tf([4,20],[1,2,100]);

>> g21 = tf([-1],[1,2,100]);

>> g22 = tf([-2,0],[1,2,100]);

>> t = 0:0.01:10;

>> u1 = 8*sin(5*t);

>> u2 = 10./(t+1);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 32 >> lsim(g11,u1,t);

>> lsim(g12,u1,t);

>> lsim(g21,u2,t);

>> lsim(g22,u2,t);

>> [y11,t] = lsim(g11,u1,t);

>> [y12,t] = lsim(g12,u2,t);

>> [y21,t] = lsim(g21,u1,t);

>> [y22,t] = lsim(g22,u2,t);

>> y1 = y11+y12;

>> y2 = y21+y22;

>> subplot(2,1,1);

>> plot(t,y1), ylabel('y_1'), title('First Output'), grid on

>> subplot(2,1,2);

>> plot(t,y2), xlabel('Time (s)'), ylabel('y_2'),title('Second Output'), grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 33 Chapter 8

Example 8.9

>> s = tf ('s');

>> g = [3/(s^2 + 2*s), 1; s/(3*s^2 + s + 1), 1/(s + 3)];

>> f = ss(g);

>> [a,b,c,d] = ssdata(f)

Example 8.10

>> a = eye (2);

>> b = zeros (2, 1);

>> c = [2, 1];

>> sys1 = ss( a, b, c, 2);

>> sys2 = tf (sys1)

>> %alternatively, the following set of commands can be used

>> a = eye (2);

>> b = zeros (2, 1);

>> c = [2, 1];

>> d=2;

>> [num, den] = ss2tf (a, b, c, d)

Example 8.11

>> sys1 = zpk([1,1,3],[-1,-1,-1,0],2)

>> sys2 = ss(sys1)

Example 8.12

>> a = [0,2,0;-8,-1,8;0,0,1];

>> c = [1,0,1];

>> syms s

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 34 >> c*ilaplace(inv(s*eye(3)-a))*[0;1;0]

>> t=0:0.0001:10;

>> y=4/21*7^(1/2)*exp(-1/2*t).*sin(3/2*7^(1/2)*t);

>> % the equation of y was obtained directly through the c*ilaplace command above

>> plot(t,y)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('Output, y')

Example 8.13

>> r1=20;

>> r2=25;

>> c1=1;

>> c2=2;

>> h10=0.05;

>> x0=[h10;0];

>> a=[-1/r1/c1,1/r1/c1;1/r1/c2,-1/c2*(1/r1+1/r2)];

>> c=eye(2);

>> syms s

>> c*ilaplace(inv(s*eye(2)-a))*x0

>> % the equations below are obtained directly through the c*ilaplace command above

>> t=0:0.001:100;

>> h1=1/4020*exp(-19/400*t).*(-sinh(1/400*t*201^(1/2)) ...

*201^(1/2)+201*cosh(1/400*t*201^(1/2)));

>> h2=1/402*201^(1/2)*exp(-19/400*t).*sinh(1/400*t*201^(1/2));

>> subplot(2,1,1)

>> plot(t,h1)

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 35 >> ylabel('h_1 (m)')

>> subplot(2,1,2)

>> plot(t,h2)

>> xlabel('Time (sec)')

>> ylabel('h_2 (m)')

>> grid on

Example 8.14

>> a=[0,1;-60,-5];

>> b=[0;1];

>> c=[1,0];

>> d=0;

>> syms s

>> simplify(c*ilaplace(inv(s*eye(2)-a)*b*3/s))

>> t=0:0.00001:3;

>> %y was obtained by using the simplify command above

>> y=1/20-1/20*exp(-5/2*t).*cos(1/2*215^(1/2)*t)-1/860*215^(1/2)......

*exp(-5/2*t).*sin(1/2*215^(1/2)*t);

>> plot(t,y)

>> subplot(1,1,1)

>> plot(t,y)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('Output, y')

Example 8.15

>> a = [-1, -1.5; 1.5, 0.1];

>> b=[0;0];

>> c = [4, 10];

>> d=0;

>> x0 = [10; 1];

>> sys = ss (a, b, c, d);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 36 >> initial (sys, x0)

Example 8.16

>> Code with regular state space model

>> t = 0:0.01:10;

>> m = 30e-6;

>> da = 0.1;

>> k = 10;

>> u = 5e-6*sin(2*t);

>> a = [0,1,0,0;-2*k/m,-2*da/m,k/m,da/m;0,0,0,1;k/m,da/m,-2*k/m,-2*da/m];

>> b = [0;0;0;1/m];

>> c = [1,0,0,0;0,0,1,0];

>> d =[0;0];

>> sys = ss(a,b,c,d);

>> % individual response 'yf' to forcing

>> [yf,t,x] = lsim(sys,u,t);

>> % individual response 'yic' to initial conditions

>> y01 = -1.5e-11;

>> x0 = [y01;0;0;0];

>> [yic,t,x] = initial(sys,x0,t);

>> % total response 'y' as superposition of individual responses ‘yf’ and ‘yic’

>> y = yf + yic;

>> y1 = y;

>> y1(:,2) = []; % deletes the second column and keeps the first column of ‘y1 = y’

>> y2 = y;

>> y2(:,1) = []; % deletes the first column and keeps the second column of ‘y2 = y’

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 37 >> subplot(211);

>> plot(t,y1)

>> ylabel('y_1 (m)')

>> grid on

>> subplot(212);

>> plot(t,y2)

>> xlabel('Time (sec)')

>> ylabel('y_2 (m)')

>> grid on

>> Code with descriptor state space model

>> t = 0:0.01:10;

>> m = 30e-6;

>> da = 0.1;

>> k = 10;

>> u = 5e-6*sin(2*t);

>> ap = [0,1,0,0;-2*k,-2*da,k,da;0,0,0,1;k,da,-2*k,-2*da];

>> bp = [0;0;0;1];

>> c = [1,0,0,0;0,0,1,0];

>> d =[0;0];

>> e=[1,0,0,0;0,m,0,0;0,0,1,0;0,0,0,m];

>> sys2 = dss(ap,bp,c,d,e);

>> % individual response 'yf' to forcing

>> [yf,t,x] = lsim(sys2,u,t);

>> % individual response 'yic' to initial conditions

>> y01 = -1.5e-11;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 38 >> x0 = [y01;0;0;0];

>> [yic,t,x] = initial(sys2,x0,t);

>> % total response 'y' as superposition of individual responses ‘yf’ and ‘yic’

>> y = yf + yic;

>> y1 = y;

>> y1(:,2) = []; % deletes the second column and keeps the first column of ‘y1 = y’

>> y2 = y;

>> y2(:,1) = []; % deletes the first column and keeps the second column of ‘y2 = y’

>> subplot(211);

>> plot(t,y1)

>> ylabel('y_1 (m)')

>> grid on

>> subplot(212);

>> plot(t,y2)

>> xlabel('Time (sec)')

>> ylabel('y_2 (m)')

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 39 Chapter 9

Example 9.2

>> %symbolic calculations

>> syms m1 m2 k1 k2 m1t m2t k1t om

>> a2=[m1t,0;0,m2t];

>> a0=[k1t+k2,-k2;-k2,k2];

>> chareq=det(-om^2*a2+a0)%algebraic characteristic equation

>> %numerical calculation

>> m1=1;

>> m2=1.8;

>> k1=200;

>> k2=250;

>> m1t=4/9*m1;

>> m2t=1/9*m2;

>> k1t=1/9*k1;

>> chareq=om^4*m1t*m2t-om^2*m1t*k2-k1t*om^2*m2t+k1t*k2-k2*om^2*m2t;

>> %equation above has been obtained by symbolic calculation

>> solve(chareq,om)

Example 9.3

>> m1=1;

>> m2=1.8;

>> k1=200;

>> k2=250;

>> m1t=4/9*m1;

>> m2t=1/9*m2;

>> k1t=1/9*k1;

>> s=tf('s');

>> g11=m1t*s^2+k1t+k2;

>> g12=-k2;

>> g22=m2t*s^2+k2;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 40 >> ginv=[g11,g12;g12,g22];

>> g=inv(ginv);

>> damp(g)

Example 9.4

>> %symbolic calculations

>> syms r l c om positive% treats all symbolic variables as positive

>> z1=r+l*j*om;

>> z2=1/c/(j*om);

>> gjom=-z2/z1;

>> re=real(gjom);

>> im=imag(gjom);

>> mag=simplify(abs(gjom))

>> phi=simplify(atan(im/re))

>> %numerical calculations

>> r=220;

>> l=0.8;

>> c=360e-6;

>> om=0:0.00001:1;

>> mag=1/c./om./(r^2+l^2*om.^2).^(1/2);

>> om1=0:0.1:4000;

>> phi=atan(r/l./om1);

>> subplot(2,1,1)

>> plot(om,mag)

>> axis([0 1 0 150])%limits the x and y axis ranges

>> ylabel('Magnitude')

>> grid on

>> phi=atan(r/l./om1)*180/(4*atan(1));

>> subplot(2,1,2)

>> plot(om1,phi)

>> xlabel('Frequency (rad/sec)')

>> ylabel('Phase angle (deg)')

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 41 Example 9.5

>> syms xi rho

>> h=200e-9;

>> w=5e-6;

>> l=50e-6;

>> yo=160e9;

>> omn=5.65e5;

>> omr=5.1e5;

>> syms xi rho

>> eq1=omr-omn*sqrt(1-2*xi^2);

>> solve(eq1,xi)

>> xi=1/226*4730^(1/2)%value obtained from above command

>> ke=yo*w*h^3/4/l^3;

>> me=33/140*rho*w*h*l;

>> eq2=omn-sqrt(ke/me);

>> solve(eq2,rho)

>> rho=3.4022e3%value obtained from above command

>> c=2*xi*33/140*rho*w*h*l*omn

Example 9.6

>> r1=500;

>> r2=600;

>> c1=0.004;

>> c2=0.005;

>> a1=[r1*c1,0;0,r2*c2];

>> a0=[1+r1/r2,-r1/r2;-1,1];

>> syms om positive

>> gjom=inv([j*om*a1+a0]);

>> g11=gjom(1,1);

>> g21=gjom(2,1);

>> mg11=simplify(abs(g11))

>> mg21=simplify(abs(g21))

>> phi11=simplify(atan(imag(g11)/real(g11)))

>> phi21=simplify(atan(imag(g21)/real(g21)))

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 42 >> om=0:0.0001:20;

>> mg11=2*(1+9*om.^2).^(1/2)./(144*om.^4+177*om.^2+4).^(1/2);

>> mg21=2./(144*om.^4+177*om.^2+4).^(1/2);

>> subplot(2,2,1)

>> plot(om,mg11)

>> grid on

>> ylabel('Magnitude')

>> title('G_1_1')

>> subplot(2,2,2)

>> plot(om,mg21)

>> grid on

>> title('G_2_1')

>> %the phase angle equation below has been conditioned

>> phi11=180/(4*atan(1))*(atan(3*om)-atan(15*om./(2-12*om.^2)));

>> phi21=180/(4*atan(1))*(-atan(15*om./(2-12*om.^2)));

>> subplot(2,2,3)

>> plot(om,phi11)

>> grid on

>> xlabel('Frequency (rad/sec)')

>> ylabel('Phase angle (deg)')

>> subplot(2,2,4)

>> plot(om,phi21)

>> grid on

>> xlabel('Frequency (rad/sec)')

Example 9.7

(a)

>> %frequency response

>> m1=1;m2=1;m3=1;c1=2;c2=2;k1=1;k2=1;k3=1;

>> u1=5;u2=2;om1=1;om2=3;

>> syms om positive

>> %plots of magnitude and phase angle for G11

>> gjom=inv([m1*(j*om)^2+c1*j*om+k1,-c1*j*om-k1,0; ...

-c1*j*om-k1,m2*(j*om)^2+(c1+c2)*j*om+k1+k2,-c2*j*om-k2; ...

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 43 0,-c2*j*om-k2,m3*(j*om)^2+c2*j*om+k2+k3]);

>> g11=gjom(1,1);

>> mg11=simplify(abs(g11))

>> phi11=simplify(atan(imag(g11)/real(g11)))

>> om=0:0.00001:5;

>> mg11=(om.^8+20*om.^6-26*om.^4+16*om.^2+9).^(1/2)./ ...

(om.^12+30*om.^10+21*om.^8+46*om.^6-10*om.^4-4*om.^2+1).^(1/2);

>> %the phase angle equation next has been conditioned

>> phi11= 180/(4*atan(1))*(atan((8*om-6*om.^3)./(om.^4-8*om.^2+3)) ...

-atan((8*om.^5-18*om.^3+4*om)./(-om.^6+17*om.^4-10*om.^2+1)));

>> subplot(2,1,1)

>> plot(om,mg11)

>> grid on

>> ylabel('Magnitude')

>> title('G_1_1')

>> subplot(2,1,2)

>> plot(om,phi11)

>> grid on

>> xlabel('Frequency (rad/sec)')

>> ylabel('Phase angle (deg)')

(b)

>> %steady-state response

>> m1=1;m2=1;m3=1;c1=2;c2=2;k1=1;k2=1;k3=1;

>> u1=5;u2=2;om1=1;om2=3;

>> syms om positive

>> %plots of magnitude and phase angle for G11

>> gjom=inv([m1*(j*om)^2+c1*j*om+k1,-c1*j*om-k1,0; ...

-c1*j*om-k1,m2*(j*om)^2+(c1+c2)*j*om+k1+k2,-c2*j*om-k2; ...

0,-c2*j*om-k2,m3*(j*om)^2+c2*j*om+k2+k3]);

>> g11=gjom(1,1);

>> mg11=abs(g11);

>> mg11om1=subs(mg11,om1);%magnitude numerical value for ω1

>> phi11=atan(imag(g11)/real(g11));

>> phi11om1=subs(phi11,om1);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 44 >> g12=gjom(1,2);

>> mg12=abs(g12);

>> mg12om2=subs(mg12,om2);

>> phi12=atan(imag(g12)/real(g12));

>> phi12om2=subs(phi12,om2);

>> g21=gjom(2,1);

>> mg21=abs(g21);

>> mg21om1=subs(mg21,om1);

>> phi21=atan(imag(g21)/real(g21));

>> phi21om1=subs(phi21,om1);

>> g22=gjom(2,2);

>> mg22=abs(g22);

>> mg22om2=subs(mg22,om2);

>> phi22=atan(imag(g22)/real(g22));

>> phi22om2=subs(phi22,om2);

>> g31=gjom(3,1);

>> mg31=abs(g31);

>> mg31om1=subs(mg31,om1);

>> phi31=atan(imag(g31)/real(g31));

>> phi31om1=subs(phi31,om1);

>> g32=gjom(3,2);

>> mg32=abs(g32);

>> mg32om2=subs(mg32,om2);

>> phi32=atan(imag(g32)/real(g32));

>> phi32om2=subs(phi32,om2);

>> t=0:0.0001:20;

>> y1=u1*mg11om1*sin(om1*t+phi11om1)+u2*mg12om2*sin(om2*t+phi12om2);

>> y2=u1*mg21om1*sin(om1*t+phi21om1)+u2*mg22om2*sin(om2*t+phi22om2);

>> y3=u1*mg31om1*sin(om1*t+phi31om1)+u2*mg32om2*sin(om2*t+phi32om2);

>> subplot(3,1,1)

>> plot(t,y1)

>> grid on

>> ylabel('y_1 (m)')

>> subplot(3,1,2)

>> plot(t,y2)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 45 >> subplot(3,1,2)

>> plot(t,y2)

>> grid on

>> ylabel('y_2 (m)')

>> subplot(3,1,3)

>> plot(t,y3)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('y_3 (m)')

Example 9.8

>> g11 = tf(1,[1,2,10]);

>> g12 = tf([1,2],[1,2,10]);

>> g21 = tf(-2,[1,2,10]);

>> g22 = tf([2,0],[1,2,10]);

>> m = [g11,g12;g21,g22];

>> bode(m)

Example 9.9

>> a = [1,0;-3,2];

>> b = [1;-1];

>> c = [0,1;-1,4];

>> d = [-2;0.5];

>> sys = ss(a,b,c,d);

>> bode(sys)

Example 9.10

>> f = 0:10:100;

>> r = [0.1000, -0.0246-0.0031i, -0.0052-0.0003i, -0.0023-0.0001i, -0.0013, -8.0021e- 004-1.6123e-005i, -5.5038e-004-9.3211e-006i, -4.0675e-004-5.8120e-006i, -3.1004e-004- 3.9342e-006i, -2.5089e-004-2.7127e-006i, -2.0038e-004-2.0078e-006i];

>> g = frd(r, f);

>> bodemag (g)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 46 >> freqresp (g,88)

Example 9.11

(a)

>> z = [1,2];

>> p = [0,3,3,5];

>> f = zpk(z,p,1.5);

>> om = 0:1:1000;

>> g = frd(f,om);

>> bode(g)

(b)

>> f = tf([1.5,-4.5,3],[1,-11,39,-45,0]);

>> om = 0:1:1000;

>> g = frd(f,om);

>> bode(g)

Example 9.12

>> delm=5e-16;

>> c=1.55e-9;

>> rho=2500;

>> yo=160e9;

>> l=90e-6;

>> w=10e-6;

>> h=120e-9;

>> me=33/140*rho*w*h*l

>> ke=3*yo*w*h^3/12/l^3

>> om=sqrt(ke/me)

>> rm=delm/me

>> xi=c/me/2/om

>> delom=om-sqrt(ke/(me+delm))

>> be=0:0.00001:2;

>> g=sqrt(1+4*xi^2*be.^2)./sqrt((1-be.^2).^2+4*xi^2*be.^2);

>> gp=sqrt(1+4*xi^2*be.^2)./sqrt((1-(1+rm)*be.^2).^2+4*xi^2*be.^2);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 47 >> plot(be,g,be,gp)

>> xlabel('\beta')

>> ylabel('Transmissibility')

>> legend('Original response','Shifted response')

Example 9.13

>> mp=80;

>> mr=0.5;

>> c=80;

>> k=200;

>> e=0.004;

>> be=0.5;

>> m=mp+2*mr;

>> omn=sqrt(2*k/m)

>> xi=c/(2*m*omn)

>> fb=4*mr*be^2*omn^2*e*sqrt(1+4*xi^2*be^2)/sqrt((2-be^2)^2+16*xi^2*be^2)

Example 9.16

>> syms om positive

>> om1=3;

>> a=20;

>> g1=1/(1+3*om*j);

>> g2=5/((om*j)^2+2*om*j+0.01);

>> mg1=simplify(abs(g1));

>> mg1om1=subs(mg1,om1)

>> phi1=atan(imag(g1)/real(g1));

>> phi1om1=subs(phi1,om1)*180/(4*atan(1))

>> mg2=simplify(abs(g2));

>> mg2om1=subs(mg2,om1)

>> phi2=atan(imag(g2)/real(g2));

>> phi2om1=subs(phi2,om1) *180/(4*atan(1))

>> af=a*mg1om1*mg2om1

>> phi2= phi1om1+ phi2om1

>> s=tf('s')

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 48 >> g1s=1/(3*s+1);

>> g2s=1/(s^2+2*s+0.01);

>> gs=g1s*g2s;

>> bode(gs)

Example 9.17

>> r1=100;

>> c1=0.001;

>> r2=1000;

>> c2=0.01;

>> s=tf('s');

>> g1=1/(r1*c1*s+1);

>> g2=1/(r2*c2*s+1);

>> bodemag(g1,g2)

>> legend('R_1 = 100\Omega, C_1 = 0.001 F','R_2 = 1000\Omega, C_2 = 0.01 F')

Example 9.18

>> m=1;

>> c=2;

>> k=200;

>> s=tf('s');

>> g=c*s/(m*s^2+c*s+k);

>> bode(g)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 49 Chapter 10

Example 10.1

>> %symbolic calculations

>> syms s la ra ke kt jl c

>> pretty(simplify(inv([la*s+ra,ke;-kt,jl*s+c])))

>> %time-domain plots; it is best to exit MATLAB and start with:

>> jl=0.02;

>> c=0.1;

>> ml=0.02;

>> ra=6;

>> la=2;

>> va=80;

>> ke=0.015;

>> kt=0.2;

>> s=tf('s')

>> %these are transfer function components obtained symbolically

>> g11=(jl*s+c)/(jl*la*s^2+(jl*ra+c*la)*s+ke*kt+c*ra);

>> g12=ke/(jl*la*s^2+(jl*ra+c*la)*s+ke*kt+c*ra);

>> g21=kt/(jl*la*s^2+(jl*ra+c*la)*s+ke*kt+c*ra);

>> g22=(la*s+ra)/(jl*la*s^2+(jl*ra+c*la)*s+ke*kt+c*ra);

>> subplot(2,1,1)

>> step(va*g11-ml*g12)

>> grid on

>> ylabel('i_a (A)')

>> subplot(2,1,2)

>> step(va*g21-ml*g22)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('\omega (rad/sec)')

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 50 Example 10.5

>> l=200e-6;

>> h=100e-9;

>> w=40e-6;

>> rho=5200;

>> yo=1.6e11;

>> kg=2.2;

>> vi=20;

>> k=3*yo*w*h^3/12/l^3

>> m=33/140*rho*w*h*l

>> omn=sqrt(k/m)

>> om=0:0.01:2000;

>> K=(3*h*m*om.^2*kg*vi)./(k-m*om.^2)/(2*l^2);

>> plot(om,K)

>> grid on

>> xlabel('\omega (rad/sec)')

>> ylabel('Sensitivity, K')

Example 10.7

>> l=0.4;

>> w=0.02;

>> h=0.001;

>> yo=2.1e11;

>> rho=7800;

>> mm=0.02;

>> v1=1;

>> v2=0.98;

>> r=v1/v2;

>> xi=log(r)/sqrt(4*pi^2+log(r)^2)

>> me=13/35*rho*w*h*l;

>> m=me+mm

>> ke=192*yo*w*h^3/12/l^3

>> omn=sqrt(ke/m)

>> ce=2*m*omn*xi

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 51 Example 10.8

>> i=0.002;

>> om=40;

>> r=100e-6;

>> dr=1e-6;

>> l=50e-6;

>> rho=3600;

>> g=50e10;

>> h=0.002;

>> al0=pi/6;

>> d=4e-6;

>> m=rho*dr*(2*r)^2;

>> j=m*(2*r)^2/12

>> kt=g*pi*dr^4/32/l

>> b=(kt-j*om^2)*(al0-atan(tan(al0)-d/h))/(2*pi*r^2*i)

Example 10.9

>> l=0.03;

>> a=1e-4;

>> rho=7500;

>> em=50e9;

>> d=4e-10;

>> m=0.02;

>> k=200;

>> syms xi

>> eq=1.2-sqrt(1-2*xi^2+sqrt(4*xi^4-4*xi^2+2));

>> solve(eq,xi)

>> xi= 1/60*1129^(1/2)%this is the positive solution obtained through command above

>> mt=m+1/3*rho*l*a

>> kt=k+em*a/l

>> ce=2*xi*sqrt(mt*kt)

>> c=em*a*d/l

>> s=tf('s');

>> g=c/(mt*s^2+ce*s+kt)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 52 >> impulse(g)

Example 10.10

>> clear

>> h=0.001;

>> a=0.01;

>> em=5e10;

>> rhop=7500;

>> g=0.074;

>> m=0.05;

>> k=100;

>> ct=136.821;

>> v=0.05;

>> mt=m+1/3*rhop*h*a^2

>> kt=k+em*a^2/h

>> omnt=sqrt(kt/mt)

>> dm=1/(g*em)

>> ac=dm*omnt^2*v

>> g=tf([-mt,0,0],[dm*mt,dm*ct,dm*kt]);

>> bodemag(g)

Example 10.11

>> fa=5;

>> om=4;

>> lc=0.02;

>> m=0.05;

>> lf=0.006;

>> hf=0.001;

>> wf=0.002;

>> ef=2.1e11;

>> l=0.02;

>> a=(6.25e-3)^2;

>> em=5.2e10;

>> d=390e-12;

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 53 >> vi=15;

>> kg=2;

>> va=2*l*fa/(em*a*d)

>> c1=2*em*a/kg/vi;

>> c2=2*l/(kg*vi);

>> j=m*lc^2/12;

>> vo=67.16e-6;

>> syms a1

>> eq=c1*a1^2-lc*5/vo*a1+c2*(j*om^2-wf*hf^3*ef/12/lf);

>> solve(eq,a1)

Example 10.12

>> al1=22.2e-6;

>> al2=3.2e-6;

>> e1=69e9;

>> e2=400e9;

>> rho1=2400;

>> rho2=2400;

>> rho1=2700;

>> h1=100e-9;

>> h2=1e-6;

>> w=50e-6;

>> l=350e-6;

>> c=2.3e-7;

>> th=20;

>> m=33/140*w*l*(rho1*h1+rho2*h2)

>> k=w/(4*l^3)*(e1*h1^3+e2*h2^3)

>> a=w*(e1*h1^3+e2*h2^3)*(al1-al2)/(8*l*(h1+h2))

>> s=tf('s');

>> g=a*th/(m*s^2+c*s+k)

>> step(g)

>> ylabel('Deflection (m)')

>> grid on

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 54 Chapter 11

Example 11.1

>> %symbolic calculations

>> syms r c g1 g2 g3

>> e2=c/g1/g2;

>> e1=e2*(1+g1*g3);

>> eq=c+e1-r;

>> c=solve(eq,c)

>> gcl=c/r

>> eq1=h*g1*g2-g1*g2-g1*g3;

>> h=solve(eq1,h)

>> %numerical calculations

>> s=tf('s');

>> g1=2/s;

>> g2=1/(s^2+s+3);

>> g3=1/(s+1);

>> gcl1=g1*g2/(1+g1*g2+g1*g3);%obtained in symbolic calculation section

>> subplot(1,2,1)

>> step(gcl1)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('c')

>> gcl2=g2/(1+g2*g3);

>> subplot(1,2,2)

>> step(gcl2)

>> grid on

>> xlabel('Time (sec)')

>> ylabel('c')

Example 11.2

>> syms s r1 r2 r3 r4 l

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 55 >> z1=r1*l*s/(r1+l*s);

>> z2=r2;

>> z3=r3;

>> z4=r4;

>> g=simplify(z2*z4/z1/z3)

Example 11.4

>> syms s K

>> g=K/(s^2+2*s+5);

>> SgK=K/g*diff(g,K)

>> SgclK=simplify(1/(1+g)*SgK)

Example 11.5

Time response

>> %plots of time response c(t)

>> syms s

>> gcl1=1/(s^2-10*s+26)^3;

>> gcl2=1/(s^2+10*s+26)^3;

>> gcl3=1/(s^2+1)^3;

>> gcl4=1/(s^2+1);

>> c1=ilaplace(gcl1)

>> c2=ilaplace(gcl2)

>> c3=ilaplace(gcl3)

>> c4=ilaplace(gcl4)

>> t=0:0.0001:5;

>> c1=-1/8*(3*t.*cos(t)+sin(t).*(-3+t.^2)).*exp(5*t);

>> subplot(2,2,1)

>> plot(t,c1)

>> xlabel('Time (sec)')

>> ylabel('c_n_1')

>> c2=-1/8*exp(-5*t).*(3*t.*cos(t)+sin(t).*(-3+t.^2));

>> subplot(2,2,2)

>> plot(t,c2)

>> xlabel('Time (sec)')

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 56 >> ylabel('c_n_2')

>> t=0:0.0001:30;

>> c3=-3/8*t.*cos(t)-1/8*sin(t).*(-3+t.^2);

>> subplot(2,2,3)

>> plot(t,c3)

>> xlabel('Time (sec)')

>> ylabel('c_n_3')

>> c4=sin(t);

>> subplot(2,2,4)

>> plot(t,c4)

>> xlabel('Time (sec)')

>> ylabel('c_n_4')

Pole-zero maps

>> %pole-zero maps

>> clear

>> s=tf('s')

>> gcl1=1/(s^2-10*s+26)^3;

>> subplot(3,1,1)

>> pzplot(gcl1)

>> gcl2=1/(s^2+10*s+26)^3;

>> subplot(3,1,2)

>> pzplot(gcl2)

>> gcl3=1/(s^2+1)^3

>> roots([1 0 3 0 3 0 1])%this command gives the poles after obtaining gcl3 from above

>> gcl4=1/(s^2+1);

>> subplot(3,1,3)

>> pzplot(gcl4)

Example 11.6

>> gcl1=tf(1,[1 2 8 8 20 8 16]);

>> subplot(1,2,1)

>> pzmap(gcl1)

>> pole(gcl1)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 57 >> gcl2=tf([2 5 -3 -1],[1 12 51 100 146 216 96 128]);

>> subplot(1,2,2)

>> pzmap(gcl2)

>> pole(gcl2)

Example 11.7

>> sys1=tf(1,[1 0])*tf(1,[2 3 15 200 9]);

>> sys2=tf([1 5],1);

>> pole(feedback(sys1,sys2))

Example 11.14

>> s=tf('s');

>> gp=2/(s+3);

>> %proportional control

>> kp1=1;

>> kp2=5;

>> gcl1=gp;

>> gcl2=kp1*gp/(1+kp1*gp);

>> gcl3=kp2*gp/(1+kp2*gp);

>> subplot(3,1,1)

>> step(gcl1,gcl2,gcl3)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

>> legend('No control','K_P = 1','K_P = 5')

>> %integrative control

>> ki1=1;

>> ki2=5;

>> gc1=ki1/s;

>> gc2=ki2/s;

>> g1=gp*gc1;

>> g2=gp*gc2;

>> t=0:0.0001:10;

>> gcl1=gp;

>> gcl2=g1/(1+g1);

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 58 >> gcl3=g2/(1+g2);

>> subplot(3,1,2)

>> step(gcl1,t,gcl2,t,gcl3,t)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

>> legend('No control','K_I = 1','K_I = 5')

>> %derivative control

>> kd1=1;

>> kd2=5;

>> gc1=kd1*s;

>> gc2=kd2*s;

>> g1=gp*gc1;

>> g2=gp*gc2;

>> t=0:0.0001:20;

>> gcl1=gp;

>> gcl2=g1/(1+g1);

>> gcl3=g2/(1+g2);

>> subplot(3,1,3)

>> step(gcl1,t,gcl2,t,gcl3,t)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

>> legend('No control','K_D = 1','K_D = 5')

Example 11.15

>> s=tf('s');

>> gcl1=10000/(s^2+400*s+10000);

>> gcl2=10000/(s^2+200*s+10000);

>> subplot(3,1,1)

>> step(gcl1,gcl2)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

>> legend('Overdamping','Critical damping')

>> gcl3=10000/(s^2+120*s+10000);

>> subplot(3,1,2)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 59 >> step(gcl3,t)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

>> gcl4=10000/(s^2+10000);

>> t=0:0.000001:0.2;

>> subplot(3,1,3)

>> step(gcl4,t)

>> xlabel('Time (sec)')

>> ylabel('c(t)')

Example 11.16

>> f=@(xi)4*sqrt(1-xi.^2)*0.002-0.3*xi.*atan(sqrt(1-xi.^2)./xi);

>> fzero(f,[0,0.05])

>> xi=0.0172;

>> taus=0.3;

>> omn=4/xi/taus

>> s=tf('s');

>> gcl=omn^2/(s^2+2*xi*omn*s+omn^2)

>> xi=0:0.00001:0.9;

>> fxi=4*sqrt(1-xi.^2)*0.002-0.3*xi.*atan(sqrt(1-xi.^2)./xi);

>> plot(xi,fxi)

>> grid on

>> xlabel('\xi')

>> ylabel('f(\xi)')

Example 11.17

>> omn=50;

>> xi=40/(2*omn)

>> taur=atan(sqrt(1-xi^2)/xi)/(omn*sqrt(1-xi^2))

>> op=exp(-pi*xi/sqrt(1-xi^2))

>> taurp=taur-0.2*taur

>> opp=op-0.15*op

>> xip=-log(opp)/sqrt(pi^2+log(opp)^2)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 60 >> omnp=atan(sqrt(1-xip^2)/xip)/(taurp*sqrt(1-xip^2))

>> gclp=omnp^2/(s^2+2*xip*omnp*s+omnp^2)

>> tf1=tf(2500,[1,40,2500]);

>> tf2=tf(3770,[1,53.87 ,3770]);

>> pzmap(tf1,tf2)

Example 11.19

>> s=tf('s');

>> gp=5*(s+1)/(s+4)/(s+5);

>> gc=1/s^2;

>> g=gc*gp;

>> pole(feedback(g,1))

>> syms s

>> gp=5*(s+1)/(s+4)/(s+5);

>> gc=1/s^2;

>> g=gc*gp;

>> Ka=limit(s^2*g,0)

>> ess=1/Ka

Example 11.20

>> syms m k kp td s

>> gp=1/(m*s^2+2*k);

>> gc=kp*(1+td*s);

>> g=gp*gc;

>> gcl=simple(g/(1+g))

>> op=0.1;

>> xi=-log(op)/sqrt(pi^2+log(op)^2)

>> k=5;

>> m=1e-12;

>> ess=2*k/(2*k+kp);

>> essp=2*k;

>> eq=ess-essp/100;

>> solve(eq,kp)

>> kp=90

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 61 >> td=2*xi/kp*sqrt(m*(2*k+kp))

Example 11.23

>> syms s k

>> k=-(s^2+9*s+20)/(s^2+3*s+2);

>> eq=diff(k,s)

>> solve(eq,s)

>> s1=-3+2^(1/2)

>> s2=-3-2^(1/2)

>> k1=-(s1^2+9*s1+20)/(s1^2+3*s1+2)

>> k2=-(s2^2+9*s2+20)/(s2^2+3*s2+2)

Example 11.25

>> sys=tf([1],[1,2,3,0])

>> rlocus(sys)

Example 11.26

>> g=tf(1,[1,1,0.25,0]);

>> subplot(3,1,1)

>> nyquist(g)

>> axis([-12 12 -12 12])

>> subplot(3,1,2)

>> bode(g)

>> grid on

>> subplot(3,1,3)

>> rlocus(g)

Lobontiu: System Dynamics for Engineering Students Worked examples MATLAB code 62