Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USING MATLAB PLEASE WRITE A CODE TO SOLVE THE FOLLOWING QUESTION be a second ord

ID: 2248167 • Letter: U

Question

USING MATLAB PLEASE WRITE A CODE TO SOLVE THE FOLLOWING QUESTION

be a second order dynamical system. in which-fW is the input with unit (m/s2), x = x(t) is the output in (m), 2.5 is the damping ratio (dimensionless) and ,-4 (rad/s) is the natural frequency. a) (15 pts) Use ode230 to simulate the time response of the system fromt-0 to t- 10 s with zero input (ie..ft) = 0), and initial conditions of (0.45 m. O.21 m/s) for x and the first time derivative of x, respectively. Write the program using m-file(s) that output a plot of the change ofx and x with time. Plot both variables on the same graph with proper labeling and annotation such that x and i are clearly distinguishable. (Hint for ode23: You need to use the state space representation of the system to create a function that returns the derivatives of x. For instance, let | , X2-X. Hence, your function implementation will return dx such that dx-A*x+B* f. Note that x-[XI; X2] is a vector of two elements so A is 2x2 and B is 2x1. Note also that ode23 requires a 2x1 vector of initial values, b) (10 pts) Use ode23() to simulate the time response of the system from t-0 to t = 10 s ) { 1, 0sts 4 s 0, 4

Explanation / Answer

clc

clear all;

%Define parameters

global M K L g C;

M = 1;

K = 25.6;

L = 1;

C = 1;

g = 9.8;

% define initial values for theta, thetad, del, deld

e_0 = 1;

ed_0 = 0;

theta_0 = 0;

thetad_0 = .5;

initialValues = [e_0, ed_0, theta_0, thetad_0];

% Set a timespan

t_initial = 0;

t_final = 36;

dt = .01;

N = (t_final - t_initial)/dt;

timeSpan = linspace(t_final, t_initial, N);

% Run ode45 to get z (theta, thetad, del, deld)

[t, z] = ode45(@RotSpngHndl, timeSpan, initialValues);

%initialize variables

e = zeros(N,1);

ed = zeros(N,1);

theta = zeros(N,1);

thetad = zeros(N,1);

T = zeros(N,1);

V = zeros(N,1);

x = zeros(N,1);

y = zeros(N,1);

for i = 1:N

    e(i) = z(i, 1);

    ed(i) = z(i, 2);

    theta(i) = z(i, 3);

    thetad(i) = z(i, 4);

    T(i) = .5*M*(ed(i)^2 + (1/3)*L^2*C*sin(theta(i)) + (1/3)*L^2*thetad(i)^2 - L*ed(i)*thetad(i)*sin(theta(i)));

    V(i) = -M*g*(e(i) + .5*L*cos(theta(i)));

    E(i) = T(i) + V(i);

end

figure(1)

plot(t, T,'r');

hold on;

plot(t, V,'b');

plot(t,E,'y');

title('Energy');

xlabel('time(sec)');

legend('Kinetic Energy', 'Potential Energy', 'Total Energy');

function dz = RotSpngHndl(~, z)

% Define Global Parameters

global M K L g C

A = [1, -.5*L*sin(z(3));

    -.5*L*sin(z(3)), (1/3)*L^2];

b = [.5*L*z(4)^2*cos(z(3)) - (K/M)*z(1) + g;

    (1/3)*L^2*C*cos(z(3)) + .5*g*L*sin(z(3))];

X = A;

% return column vector [ed; edd; ed; edd]

dz = [z(2);

    X(1);

    z(4);

    X(2)];