Error in my code

1 view (last 30 days)
Brwa
Brwa on 18 May 2013
Answered: Edward Desmond on 14 Jul 2023
Hi everyone, Im trying to run a dynamic system, but i got some error. if someone know the source of this error please explain it. sys=ss(A,Bc,C,Dc); y=lsim(sys,u,t);
??? Error using ==> DynamicSystem.lsim at 85 When simulating the response to a specific input signal, the input data U must be a matrix with as many rows as samples in the time vector T, and as many columns as *input channels.***
number of rows of U is exactly the same as T. I dont know what does Input Channels mean?
Your help will be apprecuated
  2 Comments
Azzi Abdelmalek
Azzi Abdelmalek on 18 May 2013
Can you post the values of A,Bc,C,Dc,u and t
Brwa
Brwa on 19 May 2013
Hi Azzi, its abig matrix, so please look at my answer i have explained the question in more detail
Thanks

Sign in to comment.

Accepted Answer

Azzi Abdelmalek
Azzi Abdelmalek on 19 May 2013
%Dc is (9,2), which means that your system has 2 inputs ans 9 outputs
the input u should be (100,2)
and t (100,1)

More Answers (5)

Walter Roberson
Walter Roberson on 18 May 2013
If I recall correctly (dubious), the number of input channels is set when you construct "sys", so you have to match one of the sizes with that.

Brwa
Brwa on 19 May 2013
Edited: Azzi Abdelmalek on 19 May 2013
thanks for your answers and comments. actually Im trying to solve a state space sytem which is
x'=Ax + Bc*u
y =Cx + Dc*u
sys=ss(A,Bc,C,Dc);
y=lsim(sys,u,t);
where
A (6,6) matrix
Bc is (6,2)
C is (9,6)
Dc is (9,2).
u is (100,1) vector
t is (100,1) vector
I hope someone can help me, I appreciate all your helps

Brwa
Brwa on 20 May 2013
Thanks Mr. Azzi Abdelmalek. yes, you are right.

Brwa
Brwa on 20 May 2013
I have posted another question, please take alook if you can answer me. I appreciate your help

Edward Desmond
Edward Desmond on 14 Jul 2023
% Given parameters
Pr = 2.65; % Average density (g/cm³)
Kr = 7.75e-3; % Thermal conductivity (cal/cm s °C)
Cp = 0.197; % Heat capacity (cal/g °C)
Tinit = 323; % Initial temperature (K)
Tapplied = 423; % Applied temperature (K)
tmax = 2; % Simulation time (years)
reservoirExtent = 75; % Reservoir extent (m)
% Convert units
Kr = Kr * 418.4; % Convert thermal conductivity to (W/m K)
Cp = Cp * 4.184; % Convert heat capacity to (J/kg K)
% Discretization parameters
nr = 100; % Number of radial grid points
dr = reservoirExtent / (nr - 1); % Radial grid spacing
% Time discretization parameters
dt = 0.01; % Time step size
nt = round(tmax * 365.25 * 24 * 60 * 60 / dt); % Number of time steps
% Initialize temperature matrix
T = zeros(nr, nt+1);
T(:, 1) = Tinit; % Set initial temperature
% Perform time-stepping
for i = 1:nt
% Perform radial discretization
for j = 2:nr-1
% Calculate thermal diffusivity
alpha = Kr / (Cp * Pr);
% Calculate radial derivatives
dT_dr = (T(j+1, i) - T(j-1, i)) / (2 * dr);
d2T_dr2 = (T(j+1, i) - 2 * T(j, i) + T(j-1, i)) / (dr^2);
% Update temperature using finite difference method
T(j, i+1) = T(j, i) + alpha * dt * (d2T_dr2 + (1 / j) * dT_dr);
end
% Apply boundary conditions
T(1, i+1) = T(2, i+1); % Symmetry boundary condition
T(nr, i+1) = T(nr-1, i+1) + dr * (Tapplied - T(nr, i+1)); % Heat conduction at reservoir boundary
end
% (1) Reservoir temperature at a distance of 20 m after 2 years
distance = 20;
index = round(distance / dr) + 1; % Add 1 to account for MATLAB indexing
temperature = T(index, end);
% (2) Energy needed to heat the reservoir at 20 m
mass = Pr * reservoirExtent * pi * dr^2;
energy = mass * Cp * (Tapplied - Tinit);
% (3) Viscosity change at 20 m (using a hypothetical correlation)
viscosityInitial = 10; % Initial viscosity (cP)
correlationConstant = 0.5;
viscosityChange = correlationConstant * (temperature - Tinit);
% Plotting temperature profile
r = linspace(0, reservoirExtent, nr);
figure;
plot(r, T(:, end));
xlabel('Radial Distance (m)');
ylabel('Temperature (K)');
title('Temperature Profile in the Reservoir');
% Displaying the results
fprintf('Reservoir temperature at 20 m after 2 years: %.2f K\n', temperature);
fprintf('Energy needed to heat the reservoir at 20 m: %.2f J\n', energy);
fprintf('Viscosity change at 20 m: %.2f cP\n', viscosityChange);

Categories

Find more on General Applications 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!