July 2012
Matlab Examples
Contact Author
EXERCISES ON MATLAB
Phone: 0562-2801224
DKC-31
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
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
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
DKC-38
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
As expected:
Ezplot(X,[0 2.5]) Ezplot(Y,[0 2.5])
DKC-40
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
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.
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?
%// 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