I'm trying to sove this problem. Most of the code is given. Only the while loops must be done. I tried to do it but the calculations never ends.

4 views (last 30 days)
% This is the script for completing Task 1e only.
% The input data and modulator parameters are defined here in the same way as
% it was done for tasks 1a - 1d. This part is left unchanged.
% After the part "Initialisation and input data declaration", the specific
% code to implement Task 1e is supposed to be written.
% Step-by-step instructions will be given.
%
% As far as Matlab Grader does not support the Simulink models at the
% moment, Task 1e is supposed to be done partially in local PCs, and partially
% using the Matlab Grader tool in Moodle. Please start completing the task
% here and follow the instruction given further in the code. You will be
% explained all the details there.
%% Initialisation and input data declaratiob (just copied from tasks 1a-1d)
close all; clear all; clc
% Supply network parameters
U_grid_ll = 400; % (V) RMS line-to-line netwotk voltage
U_grid_ph = U_grid_ll/sqrt(3); % (V) RMS phase network voltage
f_grid = 50; % (Hz) network frequency
% Rectified voltage
U_DC = 3*sqrt(2)/pi*sqrt(3)*U_grid_ph; % (V) The average DC voltage of an ideal 6 pulse rectifier
%modulator parameters
m_f = 21; % frequency modulation ratio
m_a = 0.5; % amplitude modulation ratio
f_ref = 50; % (Hz) frequency of reference sine waveform
f_car = m_f * f_ref; % (Hz) carrier frequency (synchronous modulation)
%f_car = m_f * 41; % (Hz) fixed carrier frequency (asynchronous modulation)
% (Uncomment to use instead of synchronous)
T_car = 1/f_car; % (s) carrier wave period
u_tri = 1; % (V) peak triangle voltage
u_ref = m_a * u_tri; % (V) peak reference voltage
SimTime = 1; % (s) simulation length
%% Task 1e
% Step 1. Create the vector of m_a values to be simulated
% The m_a values will be used as x-axis data when building the plot
% We can use 2 options to build the plot. Both will be evaluated as
% acceptable way of task implementation.
% Option 1: Pre-defined m_a-values for a plot accurate enough and fast to run
m_a_vect = [0 0.975 1 1.025 1.05 1.075 1.1 1.13 1.15 1.2 1.3 1.5 2 2.8 4.5]; % m_a values
n_fundplot = length(m_a_vect); % number of the m_a values
% Option 2: Using evenly spaced m_a values (100 values looks good but is very slow 5-10 minutes)
% n_fundplot = 100; % number of m_a-points to run
% m_a_vect = linspace(0,4.5,n_fundplot); % evenly spaced vector with values from 0 to 4.5
% Step 2. Create the values of fundamental output voltage with sinusoidal
% reference waveform
% Note: you are free to do decide on how to switch between sinusoidal reference
% and reference which contains 3rd harmonic. Anyway, this must affect the
% Simulink model. The switching mechanism is suggested to be placed in
% "Sin-Tri PWM", and can be controlled via special variable from this
% script. For example, we can create the variable "Enable_3rd" and write the
% values of 1 or 0 into it.
%set_param('Enable3rd','sw','0')
Enable_3rd = 0 ;
% But, how to implement this switching in the model, you should decide by
% yourself.
% If you did not add any circuit to add 3rd harmonic, the results of THIS
% step will be correct anyway. But, the results of the NEXT step will
% require the 3rd harmonic in reference signal anyway!
% We will use cycle "while" here. If you are good at Matlab, you can use any
% cycle. The objective of this step is to receive the correct values in
% the vector "U_fund_sine".
% Please replace ??? with the suitable lines of the code.
i_sineref = 1; % index for sinusoidal plot points
while(i_sineref <= (n_fundplot)) % create the condition for cycle to work.
% The cycle must run until all the values of m_a
% will be simulated!
m_a = m_a_vect(i_sineref) ; % m_a value for the point to run
u_ref = m_a*u_tri; % [V] peak reference voltage according to m_a
sim('ED_Ex5_T1_Sim',SimTime); % run the model
i_u = length(U_fund.signals.values);% length of the Simulink output signal data, i.e.
% the number of thr last element in the struct "U_fund"
U_fund_sine(i_sineref) = U_fund.signals.values(i_u); % this line picks the last value from signal data
i_sineref = i_sineref ; % increment the "i_sineref" variable to proceed the cycle run
end
% Step 3. Create the values of fundamental output voltage when the
% reference waveform has 3rd harmonic
% This step repeats the previous step but in Simulink model the 3rd
% harmonic must be included in the reference sine signal.
% As was suggested, this can be done if we set:
Enable_3rd = 1;
% REMEMBER: the corresponding circuit/block/function in the model which
% changes its state based on this variable must be done beforehand! If you
% did not modify the model in the way to support the 3rd harmonic in PWM
% reference signal, even correct code from this step will not bring the correct
% result!
% Please replace ??? with the suitable lines of the code.
i_3rd_ext_ref = 1; % index for sinusoidal plot points
while(i_3rd_ext_ref <= (n_fundplot)) % create the condition for cycle to work.
% The cycle must run until all the values of m_a
% will be simulated!
m_a = m_a_vect(i_sineref) ; % m_a value for the point to run
u_ref = m_a*u_tri; % [V] peak reference voltage according to m_a
sim('ED_Ex5_T1_Sim',SimTime); % run the model
i_u = length(U_fund.signals.values); % length of the Simulink output signal data, i.e.
% the number of thr last element in the struct "U_fund"
U_fund_3rd_ext(i_3rd_ext_ref) = U_fund.signals.values(i_u); % this line picks the last value from signal data
i_3rd_ext_ref = i_3rd_ext_ref+1; % increment the "i_3rd_ext_ref" variable to proceed the cycle run
end
save('m_a_vect.txt','m_a_vect','-ASCII'); % This is ready code, don't modify it
save('U_fund_sine.txt','U_fund_sine','-ASCII'); % This is ready code, don't modify it
save('U_fund_3rd_ext.txt','U_fund_3rd_ext','-ASCII'); % This is ready code, don't modify

Answers (1)

Karl
Karl on 10 Dec 2023
You need to follow the suggestion given in the comment for the line:
i_sineref = i_sineref ; % increment the "i_sineref" variable to proceed the cycle run

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!