You are on page 1of 2

11.

14 11:35 PM
% Given data points
data = [0.00, 0.83, 1.37, 0.87, 0.67, 0.95, 1.01, 0.86, 0.85, 0.93, 0.92, 0.88,
0.89, 0.91, 0.90, 0.89, 0.90, 0.90, 0.90, 0.90, 0.90];

% Time vector corresponding to the data points


time = 0:0.1:(length(data) - 1)*0.1;
Actual

% Definition of the second-order transfer function model


model = @(params, t) params(1) ./ (t.^2 + 2 * params(2) * params(3) * t +
params(3)^2);

% Initial guess for the parameters [K, damping_ratio, natural_frequency]


initial_guess = [1, 0.5, 1];

% Fitting the model to the data using least squares


params = lsqcurvefit(model, initial_guess, time, data);

% Extracting the fitted parameters


K = params(1);
damping_ratio = params(2);
natural_frequency = params(3);

% Generating the theoretical step response using the fitted parameters


theoretical_step_response = K ./ (time.^2 + 2 * damping_ratio * natural_frequency *
time + natural_frequency^2);

% Plotting the actual and theoretical step responses


figure;
plot(time, data, 'o-', 'DisplayName', 'Actual Response');
hold on;
plot(time, theoretical_step_response, '--', 'DisplayName', 'Theoretical Response');
title('Actual vs Theoretical Step Response');
xlabel('Time (s)');
ylabel('System Response');
legend('Actual Response', 'Theoretical Response');
grid on;

Model
% Given data points
data = [0.00, 0.83, 1.37, 0.87, 0.67, 0.95, 1.01, 0.86, 0.85, 0.93, 0.92, 0.88,
0.89, 0.91, 0.90, 0.89, 0.90, 0.90, 0.90, 0.90, 0.90];

% Time vector corresponding to the data points


time = 0:0.1:(length(data) - 1)*0.1;

% Definition of the second-order transfer function model


model = @(params, t) params(1) ./ (t.^2 + 2 * params(2) * params(3) * t +
params(3)^2);

% Initial guess for the parameters [K, damping_ratio, natural_frequency]


initial_guess = [1, 1, 1];

% Fitting the model to the data using least squares


params = lsqcurvefit(model, initial_guess, time, data);

% Extracting the fitted parameters


K = params(1);
damping_ratio = params(2);
natural_frequency = params(3);

% Generating the theoretical step response using the fitted parameters


theoretical_step_response = K ./ (time.^2 + 2 * damping_ratio * natural_frequency *
time + natural_frequency^2);

% Calculating the modeling error


error = data - theoretical_step_response;

% Plotting the modeling error


figure;
plot(time, error);
title('Error Model Between the Difference of Actual and Theoretical Response');
xlabel('Time (s)');
ylabel('Data Error');
grid on;

You might also like