my code doesn't work as it should,need help
이전 댓글 표시
Hello, I am working on a project and have run into some issues,namely I am trying to make a certain model and to do so first I need to solve system of 3 differential equations.Inputs need to change over time to simulate changing real life conditions,so I made matrices with some realistic values and for loop.When looking at graph ,I realised that only last values in input matrices alter values of temperatures in graph. So here is the code.
Wc = [1 ;2 ;3 ;0 ;0 ;1 ;2 ;0 ;0 ;1];
Fi_s = [0; 30; 70; 0; 0; 20; 90; 20; 0; 15];
Ta = [0; 3; 7; 5; 2; 4; 6; 3; 0; 2];
inputs = [Wc Fi_s Ta]
T_0 = [10 10 10];
T_sim = T_0;
Ts = 48*3600;
x = zeros(3,1);
%x should be vector with x(1) being Tr, x(2) being Tf %and x(3) being Tw %so x = [Tr;Tf;Tw]
for k=1:10
[t, x] = ode23(@(t, x) model_of_pump(t, x, inputs(k,:)), [0 Ts], T_0);
T_0=x(end,:);
T_sim=[T_sim;T_0];
end
figure
plot(t/3600,x(:,1),'-r',t/3600,x(:,2),'-b',t/3600,x(:,3),'-g') legend('Tr', 'Tf', 'Tw')
xlabel('t')
ylabel('Temperature')
and the function code
function dxdt = model_of_pump(t,x,inputs)
p = 0.1; ni = 3;
Wc = inputs(1); Fi_s = inputs(2); Ta = inputs(3);
dxdt = zeros(3,1);
Qra = 28 * (x(1,end) - Ta);
Qfr = 624 * (x(2,end) - x(1,end));
Qwf = 28 * (x(3,end) - x(2,end)); Qc = ni * Wc;
dxdt(1) = (Qfr - Qra + (1 - p) * Fi_s)/810;
dxdt(2) = (Qwf - Qfr + p * Fi_s)/3315;
dxdt(3) = (Qc - Qwf)/836;
end
댓글 수: 3
How did you want to display all 10 inputs? I placed a hold on below to graph each iteration of k.
Wc = [1 ;2 ;3 ;0 ;0 ;1 ;2 ;0 ;0 ;1];
Fi_s = [0; 30; 70; 0; 0; 20; 90; 20; 0; 15];
Ta = [0; 3; 7; 5; 2; 4; 6; 3; 0; 2];
inputs = [Wc Fi_s Ta]
T_0 = [10 10 10];
T_sim = T_0;
Ts = 48*3600;
x = zeros(3,1);
%x should be vector with x(1) being Tr, x(2) being Tf %and x(3) being Tw %so x = [Tr;Tf;Tw]
figure
hold on
for k=1:10
[t, x] = ode23(@(t, x) model_of_pump(t, x, inputs(k,:)),[0 Ts], T_0);
T_0=x(end,:);
T_sim=[T_sim;T_0];
plot(t/3600,x(:,1),'-r',t/3600,x(:,2),'-b',t/3600,x(:,3),'-g')
end
legend('Tr', 'Tf', 'Tw')
xlabel('t')
ylabel('Temperature')
function dxdt = model_of_pump(t,x,inputs)
p = 0.1; ni = 3;
Wc = inputs(1); Fi_s = inputs(2); Ta = inputs(3);
dxdt = zeros(3,1);
Qra = 28 * (x(1,end) - Ta);
Qfr = 624 * (x(2,end) - x(1,end));
Qwf = 28 * (x(3,end) - x(2,end)); Qc = ni * Wc;
dxdt(1) = (Qfr - Qra + (1 - p) * Fi_s)/810;
dxdt(2) = (Qwf - Qfr + p * Fi_s)/3315;
dxdt(3) = (Qc - Qwf)/836;
end
Walter Roberson
2022년 6월 9일
"and for calculating temperatures for next 4.8 hours I want to use input values with index 2 and so on."
You need to stop the ode45 computation every time there is a discontinuity in the control inputs, and then resume with the boundaries adjusted appropriately. The mathematics of the RK process is only valid if the inputs have continuous second derivatives for the duration of the ode45 call.
채택된 답변
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Electromagnetics에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

