Problem loading variables to Simulink
I have a Simulink model that uses variables set in Matlab code. However it keeps giving me the same error.
I have this function
function y = tclabsim(t, x0, u, p)
% Ensure input sizes match
if length(t) ~= length(u)
error(‘Time vector t and input profile u must have the same length.’);
end
% Extract parameters from p
U = p(1); % Heat transfer coefficient
alpha = p(2); % Ambient heat loss parameter
tau = p(3); % Time constant
% Initial conditions
x0_T = x0; % Initial temperature
% Ensure t and u are column vectors
t = t(:);
u = u(:);
% Create input matrices
inputMatrix = [t, u];
U_matrix = [0, U];
alpha_matrix = [0, alpha];
tau_matrix = [0, tau];
x0_T_matrix = [0, x0_T];
% Set simulation stop time
stopTime = t(end);
% Assign all required variables to base workspace
assignin(‘base’, ‘x0_T’, x0_T_matrix);
assignin(‘base’, ‘U’, U_matrix);
assignin(‘base’, ‘alpha’, alpha_matrix);
assignin(‘base’, ‘tau’, tau_matrix);
assignin(‘base’, ‘inputMatrix’, inputMatrix);
% Run the Simulink model
simOut = sim(‘SingleHeaterTCLab_1.slx’, ‘SrcWorkspace’, ‘current’, …
‘StopTime’, num2str(stopTime));
% Extract the output from the simulation
y = simOut.Ts; % Sensor temperature output
end
And I call the function like this
% Time vector (0 to 1000 seconds with 1-second steps)
t = (0:999)’; % Make sure t is a column vector
% Initial temperature (20°C)
x0 = 20;
% Heater input (step response: 50% heater power after 100s)
u = zeros(size(t));
u(t >= 100) = 50;
% Parameters [U, alpha, tau]
p = [0.01, 0.01, 200]; % Example values for U, alpha, tau
% Simulate the system
y = tclabsim(t, x0, u, p);
However, every time I try to run it gives the same error.
I have tried to load them as simple double and I have also tried a matrix with t on the first column and the same value in every line of the second column. They also gave the same error.
The block in Simulink has these properties. And I have also tried to change the sample time to -1.
This is the entire simulink if it helps.
How can I fix it?
Thank youI have a Simulink model that uses variables set in Matlab code. However it keeps giving me the same error.
I have this function
function y = tclabsim(t, x0, u, p)
% Ensure input sizes match
if length(t) ~= length(u)
error(‘Time vector t and input profile u must have the same length.’);
end
% Extract parameters from p
U = p(1); % Heat transfer coefficient
alpha = p(2); % Ambient heat loss parameter
tau = p(3); % Time constant
% Initial conditions
x0_T = x0; % Initial temperature
% Ensure t and u are column vectors
t = t(:);
u = u(:);
% Create input matrices
inputMatrix = [t, u];
U_matrix = [0, U];
alpha_matrix = [0, alpha];
tau_matrix = [0, tau];
x0_T_matrix = [0, x0_T];
% Set simulation stop time
stopTime = t(end);
% Assign all required variables to base workspace
assignin(‘base’, ‘x0_T’, x0_T_matrix);
assignin(‘base’, ‘U’, U_matrix);
assignin(‘base’, ‘alpha’, alpha_matrix);
assignin(‘base’, ‘tau’, tau_matrix);
assignin(‘base’, ‘inputMatrix’, inputMatrix);
% Run the Simulink model
simOut = sim(‘SingleHeaterTCLab_1.slx’, ‘SrcWorkspace’, ‘current’, …
‘StopTime’, num2str(stopTime));
% Extract the output from the simulation
y = simOut.Ts; % Sensor temperature output
end
And I call the function like this
% Time vector (0 to 1000 seconds with 1-second steps)
t = (0:999)’; % Make sure t is a column vector
% Initial temperature (20°C)
x0 = 20;
% Heater input (step response: 50% heater power after 100s)
u = zeros(size(t));
u(t >= 100) = 50;
% Parameters [U, alpha, tau]
p = [0.01, 0.01, 200]; % Example values for U, alpha, tau
% Simulate the system
y = tclabsim(t, x0, u, p);
However, every time I try to run it gives the same error.
I have tried to load them as simple double and I have also tried a matrix with t on the first column and the same value in every line of the second column. They also gave the same error.
The block in Simulink has these properties. And I have also tried to change the sample time to -1.
This is the entire simulink if it helps.
How can I fix it?
Thank you I have a Simulink model that uses variables set in Matlab code. However it keeps giving me the same error.
I have this function
function y = tclabsim(t, x0, u, p)
% Ensure input sizes match
if length(t) ~= length(u)
error(‘Time vector t and input profile u must have the same length.’);
end
% Extract parameters from p
U = p(1); % Heat transfer coefficient
alpha = p(2); % Ambient heat loss parameter
tau = p(3); % Time constant
% Initial conditions
x0_T = x0; % Initial temperature
% Ensure t and u are column vectors
t = t(:);
u = u(:);
% Create input matrices
inputMatrix = [t, u];
U_matrix = [0, U];
alpha_matrix = [0, alpha];
tau_matrix = [0, tau];
x0_T_matrix = [0, x0_T];
% Set simulation stop time
stopTime = t(end);
% Assign all required variables to base workspace
assignin(‘base’, ‘x0_T’, x0_T_matrix);
assignin(‘base’, ‘U’, U_matrix);
assignin(‘base’, ‘alpha’, alpha_matrix);
assignin(‘base’, ‘tau’, tau_matrix);
assignin(‘base’, ‘inputMatrix’, inputMatrix);
% Run the Simulink model
simOut = sim(‘SingleHeaterTCLab_1.slx’, ‘SrcWorkspace’, ‘current’, …
‘StopTime’, num2str(stopTime));
% Extract the output from the simulation
y = simOut.Ts; % Sensor temperature output
end
And I call the function like this
% Time vector (0 to 1000 seconds with 1-second steps)
t = (0:999)’; % Make sure t is a column vector
% Initial temperature (20°C)
x0 = 20;
% Heater input (step response: 50% heater power after 100s)
u = zeros(size(t));
u(t >= 100) = 50;
% Parameters [U, alpha, tau]
p = [0.01, 0.01, 200]; % Example values for U, alpha, tau
% Simulate the system
y = tclabsim(t, x0, u, p);
However, every time I try to run it gives the same error.
I have tried to load them as simple double and I have also tried a matrix with t on the first column and the same value in every line of the second column. They also gave the same error.
The block in Simulink has these properties. And I have also tried to change the sample time to -1.
This is the entire simulink if it helps.
How can I fix it?
Thank you simulink, matlab, workspace MATLAB Answers — New Questions