Anda di halaman 1dari 18

From the SelectedWorks of D. K. Chaturvedi Dr.

July 2012

Matlab Examples

Contact Author

Start Your Own SelectedWorks

Notify Me of New Work

Available at: http://works.bepress.com/dk_chaturvedi/54

EXERCISES ON MATLAB

Prof. D.K. Chaturvedi

Dayalbagh Educational Institute Dayalbagh, Agra (U.P.)


E-mail: dkc_foe@rediffmail.com

Phone: 0562-2801224

DKC-31

Problem-1 The math behind bungee jumping


This demonstration shows how to use MATLAB to model a simple physics problem faced by a college student. During spring break, John Smith wants to go bungee jumping. John has to determine which elastic cord is best suited for his weight. Elastic Cord A B C Spring Constant =5 N/m =40 N/m =500 N/m

The air resistance that the bungee jumper faces is R= a1*v - a2*|v|*v Where A1=1 A2=1

The length of the unstretched cord is 30m. The bungee jumpers is 80m above the ground.

DKC-32

To solve this physics problem, we need to: 1. 2. 3. 4. Determine all the forces acting ON the body. Draw a free body diagram. Apply Newton's second law. Solve the equation.

1. Determine the forces acting ON the body. Weight (W): W = m*g m = 90 kg g = 10 m/s^2 Air Resistance (R): R=a1*v+a2*|v|*v a1=1 a2=1 v=dx/dt Force from the elastic cord (Fe): Fe= k*x if x>0 0 if x<0

DKC-33

2. Draw the free body diagram.

Please note that we have selected downwards as the positive axis. 3. Apply Newton's second law. Net forces=m*a W-R-Fe=m*a mg-Fe-a1*v+a2*|v|*v=m*a where v=dx/dt and a=dv/dt

Solution: The math behind bungee jumping


We need to use the ODE solvers. The ODE solvers solve initial value problems for ordinary differential equations (ODEs). In this example we are using ODE45. The syntax for ODE45 is: [T,Y] = ode45(odefun,tspan,y0,options,p1,p2...) odefun A function that evaluates the right-hand side of the differential equations. All solvers solve systems of equations in the form y= f(t,y) tspan A vector specifying the interval of integration, [t0,tf]. To obtain solutions at specific times (all increasing or all decreasing), use tspan = [t0,t1,...,tf].y0A vector of initial conditions.

Options Optional integration argument created using the

DKC-34

odeset function. See odeset for details. p1,p2.. Optional parameters that the solver passes to odefun and all the functions specified in options

First we need to create the odefun function: We need to rewrite our equation so that it will be in the form of y'=f(t,y) Assume : X1=x X2=dx/dt Therefore X1dot = x2; X2dot = g - Fe/m - a1/m*x2 - a2*|x2|*x2
function dxdt = bungeeode (t,x,k) m=90; g=10; a1=1; a2=1; W=m*g; R= a1*x(2)+a2*abs(x(2))*x(2); if x(1)>0 Fe = k*x(1); else Fe = 0; end dxdt= [ x(2) ; (W-Fe-R)/m];

DKC-35

The time span can be from 0 to 50 s. To use the default options, we assign the option as [ ]. In a script file:
figure [t,xsol]=ode45(@bungeeode,[0 50],[-30 0],[],5); plot(t,50-xsol(:,1)) figure [t,xsol]=ode45(@bungeeode,[0 50],[-30 0],[],40); plot(t,50-xsol(:,1))

DKC-36

DKC-37

Result: The best choice is ELASTIC CORD B.

DKC-38

Problem 2 The physics of baseball


A pitcher throws a baseball at 50m/s at an angle of 36.7 degrees in the air. How far will the ball go before it hits the ground? How high will the ball go? How long does it take to hit the ground ? There is more than one way to solve this problem: 1. Use Calculus 2. Use MATLAB ODE solvers 3. Solve it symbolically Using MATLAB ODE solvers:
function dxdt = trajectoryode(t , x) dxdt(1)= x(2); dxdt(2,1)= 0; dxdt(3,1) = x(4); dxdt(4,1) = -9.8;

MATLAB script:
v0= 20; theta = deg2rad(36.7); v0x = v0*cos(theta); v0y = v0*sin(theta); x0=0; y0=0; [t,xsol]=ode45(@trajectoryode,[0 2.5], ... [x0 v0x y0 v0y],[]); x=xsol(:,1); vx=xsol(:,2); y=xsol(:,3); vy=xsol(:,4); plot(x,y) axis([0 300 -40 50]) hold on for i= 1:size(x) plot(x,y,'+') pause(0.2) end axis([0 300 -40 50]) plot(t,y) plot(t,vy)

DKC-39

Solving the question symbolically:


x0=0; y0=0; v0= 20; theta = deg2rad(36.7); v0x = v0*cos(theta); v0y = v0*sin(theta); syms t ay=-9.8; vy=int(ay,t)+v0y; y= int(vy,t)+y0; ax=0; vx=int(ax,t)+v0x; x= int(vx,t)+x0;

To reduce number of digits use the function VPA:


Y=vpa(y,3); X=vpa(x,3); Pretty(X) Pretty(Y) 16.0 t 2 -4.90 t + 12.0 t

As expected:
Ezplot(X,[0 2.5]) Ezplot(Y,[0 2.5])

Time to reach back to the ground:


solve(Y) ans = [0] [ 2.4489795918367346938775510204082] Range=subs(X,t, 2.44897959183673469387755102040)

For maximum height:


Solve(vy)

DKC-40

ans = 1.2196431570929004168164635989435 MaxHeight=subs(Y,t, 1.2196431570929004168164635989435)

Problem 7 Trajectory of Parachute


% Parachute Trajectory clear all; Vx=350; Vy=30; dt=0.01; t=0; tsim=10; rhow=0.0023769; Cd=0.7; S=19.625; W=50; G=9.81; x=0; H=250; Drag=0; n=round((tsim-t)/dt); for i=1:n X1(i,:)=[t, Vx, Vy, x, H, Drag]; V=sqrt(Vx^2+Vy^2); Q=(rhow*V^2)/2; dVx=-(Q*Cd*S*G*Vx)/(W*V); dVy=-(Q*Cd*S*G*Vy)/(W*V)-G; dx=(Vx+dt*dVx/2); dh=(Vy+dt*dVy/2); Vx=Vx+dt*dVx; Vy=Vy+dt*dVy; x=x+dt*dx; H=H+dt*dh; Drag=-(W/G)*(dVx/Vx)*V; t=t+dt; end subplot(2,2,1) plot(X1(:,1),X1(:,2)); title('Vx')

Problem: Energy Absorber System


% Energy Absorber clear all; M=40000; J1=131; J2=131; K1=1518241/17; K2=1518241/17; K3=330*10^3; C1=16.427; C2=16.427;

DKC-41

R1=0.75; R2=0.75; DTHETA1=0; DTHETA2=0; T=0.0087; X=0.0; DX=87.3; L1=17 L2=17; B=0; NUM=0; t=0; TSIM=6; DT=0.01; ENG=0; THETA1=0;THETA2=0;A=0; N1=1;K=1;F=1;EXTN=0; EXTN1=0; X3=0; THETA3=0; P=0; D=0; DTHETA3=0; N=0; EC=0; TR=0; for i=t:DT:TSIM DDX=-((K1*(X-sin(A)*EXTN))+(K2*(X-sin(A)*EXTN))*sin(A))/M; DDTHETA1=-((C1*DTHETA1^2+K1*((sin(A)*EXTN-X)*R1))+D)/J1; DDTHETA2=-((C2*DTHETA2^2+K1*((sin(A)*EXTN-X)*R2))+D)/J2; % K*((sin(A)*extn-X*R2))J2; DTHETA1=DTHETA1+DT*DDTHETA1; DTHETA2=DTHETA2+DT*DDTHETA2; if X<=6.0 DTHETA1=0; DTHETA2=0; end DTHETA3=DT*DDTHETA1; THETA1=THETA1+DT*DTHETA1; THETA2=THETA2+DT*DTHETA2; THETA3=DT*DTHETA1; DX=DX+DT*DDX; X=X+DT*DX; X3=DT*DX; EXTN=EXTN+R1*THETA3; EXTN1=(X-sin(A)*EXTN); P=(C1*DTHETA1.^2-K1*((sin(A)*EXTN-X)*R1))/K3; D=(THETA3-P)*K3; DRTR=(C1*DTHETA1.^2); dral=(DDTHETA2*J1)/R1; A=atan(X/30.25); TAPELOAD1=(K1*EXTN1); GLOAD=DDX/9.8; B=sqrt(X^2+30.25^2); L1=B+30; L2=B+30; J1=J1/1.000;%J1-(L1*0.68)*(R1^2+0.259)); %J1= J2=J2/1.000;%J2-(L2*0.68)*(R2^2+0.259)); %J2/1.0008; F=(M*DDX); %((K1*(X-SIN(A)*SXTN))+(K2*(X-SIN(A)*EXTN)))*SIN(A); %abs(M*DDX) ENG=(0.5*F*X); V=R1*DTHETA1; N=DTHETA1*30/pi; EC=EC+2*(C1*DTHETA1^2+12^3*EXTN1*R1)*DTHETA1*DT; TR=-F*sin(A)*R1; if (-THETA1/(pi/2)+N1)<=0.0015 & R1>=0.190

DKC-42

R1=R1-T/4; %(T*THETA3/2*pi); R2=R2-T/4; %(T*THETA3/2*pi); N1=N1+1; end dddx=-DDX/9.8; time1(K,:)=[i]; X1(K,:)=[X,DX,dddx, K1, DTHETA1,EC, TAPELOAD1]; K=K+1; if (X<2) TAPELOAD1=0; K1=0; end if (X>=2 & X<=5) K1=0.3*stif1(TAPELOAD1); %elseif X>2&X<5 K1=125695; %elseif X>5&X<20 K1=114695; %elseif X>20&X<42 K1=129308; elseif (X>5 & X<=15) K1=0.15*stif1(TAPELOAD1); else K1=1518241/L1; end K2=K1; % if DTHETA1<60, C1=C1/1.1; % elseif DTHETA1>60 & DTHETA1<90 C1=C1*1.01; else C1=9.18; %end end X1(:,1)=time1; subplot(2,2,1) plot(X1(:,1),X1(:,2)) title('vel vs dis') xlabel('Distance, m') ylabel('velocity') subplot(2,2,2) plot(X1(:,1),X1(:,3)) title('decelration') xlabel('Distance, m') ylabel('g-load') subplot(2,2,3) plot(X1(:,1),X1(:,5)) title('Angular Velocity') xlabel('Distance, m') ylabel('rad/sec') subplot(2,2,4) plot(X1(:,1),X1(:,7)) title('Tape load') xlabel('Distance, m') ylabel('Joules')

DKC-43

Problem - 3 Model a basic cruise control system


Let us model a basic cruise control system. We first assume that the Inertia of the vehicle wheels are negligible, and it is understood that the viscous friction created from the car's speed is completely in the direction of the opposing motion of the automobile. Therefore, our cruise control problem is now reduced to a single mass and a constant damper system as shown in Figure 1.

Figure 1: Simple Cruise Control System.

Given: b = 55 N*sec/m (damper constant), m = 1220 kg (vehicle mass), u = 1000 N (engine force), x (position of the vehicle in m), v (velocity of the vehicle in m/s) Objective: Once you determine the modeling equations of this control system, find the response of the car velocity when the input (u) is 1000 N. Use MATLAB to determine the following control characteristics of this cruise control system: 1. 2. 3. 4. Transfer Function Determine Poles: Are the poles stable? Step Response: At what amplitude does it settle to? Root-Locus: Explain the behavior.

Calculated: State-Space variable form

DKC-44

Extra Credit: If you were to vary the mass of the automobile from 1220 kg to either 500 kg or 2000 kg, what effect does this do to the response of the cruise control system?

Solution: Model a basic cruise control system

Figure 1: Simple Cruise Control System.

%// Given values m = 1220; % mass of the vehicle b = 55; % damper constant u = 1000; % Input (engine force) % Setup the state-space matrices A = [0 1; 0 -b/m]; B = [0; 1/m]; C = [0 1]; D = [0];

DKC-45

% 1. Calculate the Transfer Function sys = ss(A,B*u,C,D); sys1 = tf(sys); (Answer) Transfer function: 0.8197 --------------s + 0.04508

% 2. Determine the poles and zeros and plot them num = [0.8197]; % zeros den = [1 0.04508]; % poles [z p k] = tf2zp(num,den); figure; pzmap(p,z); axis([-0.06 0.01 -1 1]); (Answer)

From the Pole-Zero plot, we can determine that the poles are stable since they are in the left-half plane of the s-plane. % 3. Do a step response of the system figure; sys = ss(A,B*u,C,D); sys1 = tf(sys); step(sys1);

DKC-46

(Answer)

Based on the step response plot, the system settles out at a magnitude value of K = 18.18. % 4. Plot the Root Locus figure; rlocus(num,den); axis([-0.06 0.01 -1 1]); (Answer)

Looking at the root locus plot, we can tell that the system is definitely stable with one pole at p = -0.04508. With an increase in gain (K) the pole moves to the left in the s-plane towards infinity.

DKC-47

Anda mungkin juga menyukai