How to update x from ode45 result y? how to see the plot of y from t = 0 to the end of simulation?
1 次查看(过去 30 天)
显示 更早的评论
%State Space Model of Literature from X.PENG
%Coded by Hng Ming Yang
function dx = system1(t,x) %#ok<*INUSL>
%Defining the constants a1:a5 represent product of (volume rate*air pressure*conductivity)
a1 = 8.657e3;
a2 = 2.3324e3;
a3 = 6.8269e3;
a4 = 12078;
a5 = 10681;
%Defining the heat flux which will varies if different set up
b1 = 1.9845e3;
b2 = 1.1276e3;
b3 = 913.8;
b4 = 649.8546;
b5 = 1.4396e3;
%System Matrix formation
A = [-484.9/a1 84.6/a1 51.0/a1 152.4/a1 60.9/a1;
49.1/a2 -120.7/a2 0 0 45.1/a2;
225.2/a3 0 -485.1/a3 215.6/a3 0;
64.1/a4 0 390.0/a4 -532.3/a4 48.2/a4;
10.4/a5 9.6/a5 0 134.2/a5 -226.3/a5];
%Input Matrix formation
B = [1/a1 0 0 0 0;
0 1/a2 0 0 0;
0 0 1/a3 0 0;
0 0 0 1/a4 0;
0 0 0 0 1/a5];
%Measurement matrix C1:C5 each simulating temperature changes from zone 1
%to zone 5.
C1 = [1 0 0 0 0];
C2 = [0 1 0 0 0];
C3 = [0 0 1 0 0];
C4 = [0 0 0 1 0];
C5 = [0 0 0 0 1];
%output is Y = CX, in this case D = 0
D = 0;
% Initialising the Original Temperature
X0 = [32;33;33;36;31];
global u;
dx = A*x + B*u;
end
% and then in another script i run the main file,as follows
setpoint = 25;
global u;
x=[35;34;33;35;34]; %initial value for x
ts=0.01; %Sampling time
t0=0;
TS=600; %Total simulation time
for j=1:TS/ts
T=[T t0+(j-1)*ts];
end
for j=1:(TS/ts-1)
t=T(j);
kp = [500 0 0 0 0;
0 500 0 0 0;
0 0 500 0 0;
0 0 0 500 0;
0 0 0 0 500];
ki = [10 0 0 0 0;
0 10 0 0 0;
0 0 10 0 0;
0 0 0 10 0;
0 0 0 0 10];
integral = 0;
DeltaE1= setpoint - x(1);
DeltaE2= setpoint - x(2);
DeltaE3= setpoint - x(3);
DeltaE4= setpoint - x(4);
DeltaE5= setpoint - x(5);
integral = integral + [DeltaE1;DeltaE2;DeltaE3;DeltaE4;DeltaE5]*ts;
u = kp*[DeltaE1;DeltaE2;DeltaE3;DeltaE4;DeltaE5] + ki*integral ;
[t, y] = ode45(@system1, [T(j) T(j+1)], x);
x(1)=y(length(y(:,1)),:);
x(2)=y(length(y(:,2)),:);
x(3)=y(length(y(:,3)),:);
x(4)=y(length(y(:,4)),:);
x(5)=y(length(y(:,5)),:);
end
plot(t,y);
% i can't run the file due to wrong updating of x values from y,
% i can't get a plot that shows y plot from initial to end of simulation, instead the plot always shows me the output of the final "loop", which the time span is a few milliseconds.
Thank you guys, i am sorry for the trouble caused.
0 个评论
回答(1 个)
Jordan Ross
2016-9-21
Hi Ming,
There are a few issues with the code that you provided.
1. The value of "T" is undefined on the following line of your script:
>> T = [T t0+(j-1)*ts];
2. When you set the values of "x" in your "for" loop you have an error because you are trying to store a row-vector of length 5 to a scalar. You should instead do the following:
>> numYCols = size(y,2); % get the number of columns of y.
>> x(1:(1:numYCols)) = y(length(y(:,1)),:);
Also note that you are going to have a problem storing values into "x" because you use "x" as an input to the "ode45" function. This function expects "x" to be a column-vector, not a matrix.
For more information about indexing into vectors and matrices please see: http://www.mathworks.com/help/matlab/learn_matlab/array-indexing.html
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Ordinary Differential Equations 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!