Multiple if statement are not updating my equation over time.

조회 수: 1 (최근 30일)
Lukas Walden-Richards
Lukas Walden-Richards 2023년 3월 7일
답변: Shushant 2023년 3월 13일
I am trying to update the variable Feed ,which is the number of logs I am adding to a fire, at different time intervals. The first if statement changes the function but the following elseif statement do nothing to the function when it is graphed.
clear all; clc;
% Inputs
Lam = 0.000127921; % assuming a log is putting out negligible heat transfer at 200 W/m^2 %0.000089
k1= 0.2; % W/m^2*k
k2 = 10; % variable from 1-10 W/m^2*k [lower will increase temps between floors]
k3 = 0.5; % W/m^2*k
Qin = @(t) 500*exp(-Lam*t); % 500 is one log variable with max 2500 W/m^2
Ain = 0.5; % m^3
Cv = 1005; % J/kg*k
p = 1.2; % Kg/m^3 density air
Tout = @(t) -10*sin((2*pi*t)/86400) + 273;
A1 = 2*(6*3)+(2*(5*3)); % Areas for Q1
A2 = 5*6; % Area for Q2
A3 = 6*3 + sqrt(18)*5*2; % Area for Q3
V1 = 6*5*3; % volume downstairs
V2 = .5*3*6*5; % volume upstairs
h = 1; % step size
Lt = 172800; % 48 hours to seconds
N = Lt/h;
Feed = 1; % preallocate
% Initial Values
T1(1) = 278;
T2(1) = 280;
t(1) = 0;
% functions
if t == 0
Feed = 4;
k2 = 8;
elseif t == 60000
Feed = 100;
k2 = 10;
elseif t == 2*3600
Feed = 100;
k2 = 10;
elseif t == 3*3600
Feed = 100;
k2 = 10;
elseif t == 4*3600
Feed = 100;
k2 = 10;
elseif t == 5*3600
Feed = 100;
k2 = 10;
elseif t == 6*3600
Feed = 100;
k2 = 10;
elseif t == 180000
Feed = 1;
k2 =1;
end
dT1 = @(t,T1,T2) ((Ain*Qin(t)*Feed+((-A1*(k1*(T1-Tout(t))))-(A2*(T1-T2+5)))))/(Cv*p*V1);
dT2 = @(t,T1,T2) ((A2*k2*(T1-T2+5))-(A3*k3*(T2-Tout(t))))/(Cv*p*V2);
perfect(1) = 293;
t_last_log = 0;
for i = 1:N
% independent var update
t(i+1) = t(i) + h;
perfect(i+1) = perfect(i);
plusfive(1) = 298;
minusfive(1) = 288;
plusfive(i+1) = plusfive(i);
minusfive(i+1) = minusfive(i);
% dependent var update
T1(i+1) = T1(i) + h*dT1(t(i),T1(i),T2(i));
T2(i+1) = T2(i) + h*dT2(t(i),T1(i),T2(i));
Terror(i) = max((abs(T1(i)-293)+abs(T2(i)-293))/(2*N));
end
MaxError = max(Terror(i))
MaxError = 8.6099e-05
figure(1); clf(1);
plot(Tout(t),'r-')
xlabel('Time');
ylabel('Temperature (K)')
hold on
% figure(2); clf(2);
plot(t,T1,'b-')
plot(t,T2,'g-')
plot(t,perfect,'k--','linewidth',0.1);
plot(t,plusfive,'r--','linewidth',0.1);
plot(t,minusfive,'r-.','linewidth',0.1);
xlabel('Time (seconds)')
ylabel('Temperature (K)')
legend('Outside Temp','Ground Floor','Top Floor','20 C','25 C','15 C')
  댓글 수: 5
Lukas Walden-Richards
Lukas Walden-Richards 2023년 3월 8일
I am not apposed to other options that are not anonymous functions. I am just getting started in matlab. For the example function that you wrote for k2, I am getting the same result where there is only one value for k2 when I run the script.
Rik
Rik 2023년 3월 8일
How did you check that? And how did you implement the function I suggested?

댓글을 달려면 로그인하십시오.

답변 (1개)

Shushant
Shushant 2023년 3월 13일
According to my understanding you are looking for the conditional statements to work as a function which should change the values of "Feed" and "k2" based on the value of "t" preferably after each iteration of the loop. To accomplish this, there are two ways:
  1. Place conditional statements inside the loop and modifying t to t(i).
  2. Create a function which takes "t" as an input and outputs "Feed" and "k2". There is also a need to put an "else" statement so that if for a certain value of "t" none of the condition gets satisfied a value of "Feed" and "k2" is defined to be passed back. After defining the function, we call it from the inside of "for" loop to update the value of "Feeds" and "k2" in every iteration based on the current value of "t". Please find the below code for the same.
clear all; clc;
% Inputs
Lam = 0.000127921; % assuming a log is putting out negligible heat transfer at 200 W/m^2 %0.000089
k1= 0.2; % W/m^2*k
k2 = 10; % variable from 1-10 W/m^2*k [lower will increase temps between floors]
k3 = 0.5; % W/m^2*k
Qin = @(t) 500*exp(-Lam*t); % 500 is one log variable with max 2500 W/m^2
Ain = 0.5; % m^3
Cv = 1005; % J/kg*k
p = 1.2; % Kg/m^3 density air
Tout = @(t) -10*sin((2*pi*t)/86400) + 273;
A1 = 2*(6*3)+(2*(5*3)); % Areas for Q1
A2 = 5*6; % Area for Q2
A3 = 6*3 + sqrt(18)*5*2; % Area for Q3
V1 = 6*5*3; % volume downstairs
V2 = .5*3*6*5; % volume upstairs
h = 1; % step size
Lt = 172800; % 48 hours to seconds
N = Lt/h;
Feed = 1; % preallocate
% Initial Values
T1(1) = 278;
T2(1) = 280;
t(1) = 0;
dT1 = @(t,T1,T2) ((Ain*Qin(t)*Feed+((-A1*(k1*(T1-Tout(t))))-(A2*(T1-T2+5)))))/(Cv*p*V1);
dT2 = @(t,T1,T2) ((A2*k2*(T1-T2+5))-(A3*k3*(T2-Tout(t))))/(Cv*p*V2);
perfect(1) = 293;
t_last_log = 0;
for i = 1:N
[Feed,k2]=CalculateMyLogs(t(i)); % passing t(i) to the function to get the value of Feed.
dT1 = @(t,T1,T2) ((Ain*Qin(t)*Feed+((-A1*(k1*(T1-Tout(t))))-(A2*(T1-T2+5)))))/(Cv*p*V1);
dT2 = @(t,T1,T2) ((A2*k2*(T1-T2+5))-(A3*k3*(T2-Tout(t))))/(Cv*p*V2);
perfect(1) = 293;
t_last_log = Feed;
% independent var update
t(i+1) = t(i) + h;
perfect(i+1) = perfect(i);
plusfive(1) = 298;
minusfive(1) = 288;
plusfive(i+1) = plusfive(i);
minusfive(i+1) = minusfive(i);
% dependent var update
T1(i+1) = T1(i) + h*dT1(t(i),T1(i),T2(i));
T2(i+1) = T2(i) + h*dT2(t(i),T1(i),T2(i));
Terror(i) = max((abs(T1(i)-293)+abs(T2(i)-293))/(2*N));
end
MaxError = max(Terror(i))
MaxError = 1.0463e-04
MaxError = 8.6099e-05
MaxError = 8.6099e-05
figure(1); clf(1);
plot(Tout(t),'r-')
xlabel('Time');
ylabel('Temperature (K)')
hold on
% figure(2); clf(2);
plot(t,T1,'b-')
plot(t,T2,'g-')
plot(t,perfect,'k--','linewidth',0.1);
plot(t,plusfive,'r--','linewidth',0.1);
plot(t,minusfive,'r-.','linewidth',0.1);
xlabel('Time (seconds)')
ylabel('Temperature (K)')
legend('Outside Temp','Ground Floor','Top Floor','20 C','25 C','15 C')
% function
function [Feed, k2] = CalculateMyLogs(t)
if t == 0
Feed = 4;
k2 = 8;
elseif t == 60000
Feed = 100;
k2 = 10;
elseif t == 2*3600
Feed = 100;
k2 = 10;
elseif t == 3*3600
Feed = 100;
k2 = 10;
elseif t == 4*3600
Feed = 100;
k2 = 10;
elseif t == 5*3600
Feed = 100;
k2 = 10;
elseif t == 6*3600
Feed = 100;
k2 = 10;
elseif t == 180000
Feed = 1;
k2 =1;
else
Feed = 1;
k2 = 1;
end
end
I will recommend you to use breakpoints in the code to check and get a better understanding on the execution of the code. More information on breakpoints can be found here - Set Breakpoints - MATLAB & Simulink (mathworks.com)
For more information on conditional statements and relational operators, kindly go through the following documentation links.

카테고리

Help CenterFile Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품


릴리스

R2022a

Community Treasure Hunt

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

Start Hunting!

Translated by