You are on page 1of 19

NAME:HANI SBINATI

STUDENT ID:2106203

6.5 problem
A=[0 1; -34 -2];
B=[0;2.5];
C=[1 0];
D=0;
sys=ss(A,B,C,D);
time=0:0.001:6;
u=0.6*ones(size(time));
X0=[2; -1.5];
[y,time]=lsim(sys,u,time,X0);
plot(time,y);
6.1 Problem
J = 0.5;

omega_0 = 40;

b = 0.06;

t_start = 0;

t_end = 10;

dt = 0.01;

theta_0 = 0;

omega = omega_0;

time = t_start:dt:t_end;

theta = zeros(size(time));

omega_vals = zeros(size(time));

energy_dissipated = zeros(size(time));

for i = 1:numel(time)

torque_friction = -b * omega;

alpha = torque_friction / J;

omega = omega + alpha * dt;

theta_0 = theta_0 + omega * dt;

theta(i) = theta_0;

omega_vals(i) = omega;

energy_dissipated(i) = torque_friction * omega * dt;

end

initial_energy = 0.5 * J * omega_0^2;


figure;

subplot(2, 1, 1);

plot(time, theta);

xlabel('Time (s)');

ylabel('Angular Position (rad)');

title('Angular Position vs. Time');

subplot(2, 1, 2);

plot(time, omega_vals);

xlabel('Time (s)');

ylabel('Angular Velocity (rad/s)');

title('Angular Velocity vs. Time');

figure;

plot(time, energy_dissipated);

xlabel('Time (s)');

ylabel('Dissipated Energy (J)');

title('Dissipated Energy vs. Time');

total_dissipated_energy = sum(energy_dissipated) * dt;

disp(['Initial Energy: ' num2str(initial_energy) ' J']);

disp(['Total Dissipated Energy: ' num2str(total_dissipated_energy) ' J']);


Problem 6.4
% System parameters

m = 2; % Mass (kg)

b = 5; % Damping coefficient (N-s/m)

k = 200; % Spring constant (N/m)

% Time settings

t_start = 0;

t_end = 1; % Adjust as needed

dt = 0.001; % Time step

% Force pulse function

F = @(t) 8 * (t >= 0 & t <= 0.05);

% Initial conditions

z0 = 0; % Initial position

v0 = 0; % Initial velocity

% Initialize arrays for storing results

time = t_start:dt:t_end;

z = zeros(size(time));

v = zeros(size(time));

% Numerical integration using Euler's method

for i = 1:numel(time)

t = time(i);
% Calculate acceleration

F_t = F(t);

a = (F_t - b * v0 - k * z0) / m;

% Update velocity and position

v1 = v0 + a * dt;

z1 = z0 + v1 * dt;

% Store values

v(i) = v1;

z(i) = z1;

% Update initial conditions for the next iteration

v0 = v1;

z0 = z1;

end

% Plotting

plot(time, z);

xlabel('Time (s)');

ylabel('Response (z)');

title('Dynamic Response');
Problem 7.24
% System parameters

J = 0.2; % Moment of inertia (kg-m²)

k = 100; % Torsional spring constant (N-m/rad)

% Input torque function

Ti = @(t) 0.5 * sin(3 * t);

% Simulation time

t_start = 0;

t_end = 10;

dt = 0.001;

time = t_start:dt:t_end;

% Initial conditions

theta_0 = 0; % Initial angular displacement

omega_0 = 0; % Initial angular velocity

% Initialize arrays for storing results

theta = zeros(size(time));

omega = zeros(size(time));
% Numerical integration using Euler's method

for i = 2:numel(time)

t = time(i);

% Calculate angular acceleration

alpha = (Ti(t) - k * theta_0) / J;

% Update angular velocity and displacement

omega(i) = omega_0 + alpha * dt;

theta(i) = theta_0 + omega(i) * dt;

% Update initial conditions for the next iteration

omega_0 = omega(i);

theta_0 = theta(i);

end

% Plotting

plot(time, theta);

xlabel('Time (s)');

ylabel('Angular Displacement (rad)');

title('Complete Response (θ(t))');


Problem 7.20
% Define the transfer function

num = 800;

den = [2, 24, 144, 640];

G = tf(num, den);

% Find the characteristic roots (poles)

poles = pole(G);

disp('Characteristic roots (poles):');

disp(poles);

% Simulate the system response to a unit-step input

t = 0:0.01:10;

u = ones(size(t));

[y, ~, x] = lsim(G, u, t);

% Plot the response

plot(t, y);

xlabel('Time');

ylabel('Output');

title('System Response to a Unit-Step Input');


Problem 7.22
% Define the transfer function

num = 0.25;

den = [1, 2, 9];

G = tf(num, den);

% Define the step input

t = 0:0.01:10;

u = 4 * ones(size(t));

% Simulate the system response

[y, ~, ~] = lsim(G, u, t);

% Plot the response

plot(t, y);

xlabel('Time (s)');

ylabel('Position (m)');

title('System Response to Step Input');


Problem 6.3
% System parameters

m = 0.5; % mass (kg)

b = 3; % viscous friction coefficient (N-s/m)

% External force function

f = @(t) 5*(t >= 0 & t < 0.02); % pulse function

% Initial conditions

x0 = 0; % initial displacement (m)

v0 = 0; % initial velocity (m/s)

% Solve the differential equation

[t, y] = ode45(@(t, y) [y(2); (f(t) - b*y(2))/m], [0 0.04], [x0 v0]);

% Extract displacement and velocity from the solution

x = y(:, 1);

v = y(:, 2);

% Plot displacement and velocity

figure;

subplot(2, 1, 1);

plot(t, x);
xlabel('Time (s)');

ylabel('Displacement (m)');

title('Mass-Damper System Response');

subplot(2, 1, 2);

plot(t, v);

xlabel('Time (s)');

ylabel('Velocity (m/s)');
Problem 7.16
% Define the state matrix A

A = [-0.2 -0.6; 2 -4];

% Define the input matrix B

B = [0; 1.5];

% Define the output matrix C

C = [1 0];

% Define the feedthrough matrix D

D = 0;

% Create the state-space model

sys = ss(A, B, C, D);

% Define the time span for simulation

t = 0:0.1:10;

% Define the input signal

u = ones(size(t)); % Unit step input

% Simulate the response of the system


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

% Plot the response

plot(t, y);

xlabel('Time');

ylabel('Output');

title('System Response');

grid on;

THIS CODE IS FOR QUESTION C 7.16

% Define the state matrix A

A = [-0.2 -0.6; 2 -4];

% Define the input matrix B

B = [0; 1.5];

% Define the output matrix C

C = [1 0];

% Define the feedthrough matrix D

D = 0;
% Create the state-space model

sys = ss(A, B, C, D);

% Define the initial state vector X0

X0 = [1; 2]; % Arbitrary initial state

% Define the time span for simulation

t = 0:0.1:10;

% Calculate the free response of the system

[y, t, x] = initial(sys, X0, t);

% Plot the free response of the output

plot(t, y);

xlabel('Time');

ylabel('Output');

title('Free Response of the Output');

grid on;
Problem 6.8
clc;

clear;

close all;

R1 = 0.4;

R2 = 0.2;

C = 0.04;

L = 0.01;

Q0 = 0.01;

V0 = Q0 / C;

t = 0:0.001:1.5;

Ein = 0.5 * sin(10 * t);

sys = tf((R2 + s * L) / (R1 + R2 + s * L), C);

y = isimT(sys * Ein, t, V0);

plot(t, y);

xlabel('Time');

ylabel('Output');

title('System Response');

grid on;

You might also like