(0) I couldn't run this code on Matlab. I want to see the output generated from Simulink

1 view (last 30 days)
% Define system parameters
R = 1; % Resistance of the motor armature (in ohms)
L = 142e-3; % Inductance of the motor armature (in henries)
C = 160e-6; % Capacitance of the parallel capacitor (in farads)
V = 240; % Supply voltage (in volts)
f = 60; % Frequency of the supply voltage (in Hz)
% Create a new Simulink model
model = 'car_speed_control';
new_system(model);
% Add input block
add_block('simulink/Sources/Sine Wave', [model '/Input']);
set_param([model '/Input'], 'Amplitude', '10', 'Frequency', '0.2');
% Add controller block
add_block('simulink/Discrete/Transfer Fcn', [model '/Controller']);
There is no block named 'simulink/Discrete/Transfer Fcn'
set_param([model '/Controller'], 'Numerator', '10', 'Denominator', '1');
% Add armature voltage block
add_block('simulink/Continuous/Integrator', [model '/Armature Voltage']);
set_param([model '/Armature Voltage'], 'InitialCondition', '0');
% Add motor block
add_block('simulink/Continuous/State-Space', [model '/Motor']);
set_param([model '/Motor'], 'A', '-R/L', 'B', '1/L', 'C', '1', 'D', '0');
% Add main pulley block
add_block('simulink/Math Operations/Gain', [model '/Main Pulley']);
set_param([model '/Main Pulley'], 'Gain', '10');
% Add car speed block
add_block('simulink/Math Operations/Integrator', [model '/Car Speed']);
% Add feedback block
add_block('simulink/Math Operations/Subtract', [model '/Feedback']);
% Connect the blocks
add_line(model, 'Input/1', 'Controller/1');
add_line(model, 'Controller/1', 'Armature Voltage/1');
add_line(model, 'Armature Voltage/1', 'Motor/1');
add_line(model, 'Motor/1', 'Main Pulley/1');
add_line(model, 'Main Pulley/1', 'Car Speed/1');
add_line(model, 'Car Speed/1', 'Feedback/2');
add_line(model, 'Main Pulley/1', 'Feedback/1');
add_line(model, 'Feedback/1', 'Controller/2');
% Set simulation parameters
set_param(model, 'StopTime', '10');
% Run the simulation
sim(model);
% Plot the results
plot(tout, Car_Speed);
title('Car Speed vs. Time');
xlabel('Time (s)');
ylabel('Car Speed (m/s)');

Answers (2)

Jon
Jon on 1 May 2023
You have many errors in your code. I have attached a cleaned up version that at least runs. I put a comment %** on each line I added or changed so you can review.
The connectivity of the model is questionable. I just connected it so at least it didn't throw errors. Also I doubt that your controller transfer function is assigned properly, you just have the numerator and denominator as constants.
At least this might help get you going, but it looks like you have more work to do.
By the way you can display your model after you build it by using open_system(model), if you then go to the format tab, and click auto arrange you can visualize what you have done.
% Define system parameters
R = 1; % Resistance of the motor armature (in ohms)
L = 142e-3; % Inductance of the motor armature (in henries)
C = 160e-6; % Capacitance of the parallel capacitor (in farads)
V = 240; % Supply voltage (in volts)
f = 60; % Frequency of the supply voltage (in Hz)
% Create a new Simulink model
% if model already exists close it %**
if exist('model','var')
close_system(model,0)
end
model = 'car_speed_control';
new_system(model);
% Add input block
add_block('simulink/Sources/Sine Wave', [model '/Input']);
set_param([model '/Input'], 'Amplitude', '10', 'Frequency', '0.2');
% Add controller block
add_block('simulink/Discrete/Discrete Transfer Fcn', [model '/Controller']); %**
set_param([model '/Controller'], 'Numerator', '10', 'Denominator', '1');
% Add armature voltage block
add_block('simulink/Continuous/Integrator', [model '/Armature Voltage']);
set_param([model '/Armature Voltage'], 'InitialCondition', '0');
% Add motor block
add_block('simulink/Continuous/State-Space', [model '/Motor']);
set_param([model '/Motor'], 'A', '-R/L', 'B', '1/L', 'C', '1', 'D', '0');
% Add main pulley block
add_block('simulink/Math Operations/Gain', [model '/Main Pulley']);
set_param([model '/Main Pulley'], 'Gain', '10');
% Add car speed block
add_block('simulink/Continuous/Integrator', [model '/Car Speed']); %**
% Add feedback block
add_block('simulink/Math Operations/Subtract', [model '/Feedback']);
% Add output block
add_block('simulink/Sinks/To Workspace', [model '/OutBlock']) %**
% Connect the blocks
add_line(model, 'Input/1', 'Feedback/1'); %*** (setpoint?)
add_line(model, 'Controller/1', 'Armature Voltage/1');
add_line(model, 'Armature Voltage/1', 'Motor/1');
add_line(model, 'Motor/1', 'Main Pulley/1');
add_line(model, 'Main Pulley/1', 'Car Speed/1');
add_line(model, 'Car Speed/1', 'Feedback/2');
% % % add_line(model, 'Main Pulley/1', 'Feedback/1');
add_line(model, 'Feedback/1', 'Controller/1');%**
add_line(model, 'Car Speed/1','OutBlock/1'); %**
% Set simulation parameters
set_param(model, 'StopTime', '10');
% Run the simulation
out = sim(simIn); %**
% Plot the results
plot(out.simout.Time, out.simout.Data); %**
title('Car Speed vs. Time');
xlabel('Time (s)');
ylabel('Car Speed (m/s)');

Walter Roberson
Walter Roberson on 1 May 2023
The correct block name is simulink/Discrete/Discrete Transfer Fcn

Products


Release

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!