For loop not generating result

I have the equation below to program
k should assume 1 if and assume 0 if .
All other parameters are constants.
R = 2; C = 2.5; eta = 2.5; Pac = 5; Tp = 76; t_delta = 1;
t = 0:5/12:24;
alpha = 1-t_delta/(60*R*C);
beta = eta*R*(9/5)+32;
T0 = 86 - 14*cos(0.0035*t);
Ta(1) = Tp;
k(1) = 0;
for j = 1:length(t)
Ta(j+1) = alpha*Ta(j)+(1-alpha).*(T0(j)-k(j)*beta*Pac);
if (Ta(1:j) <= Tp-0.5)
k(j) = repelem(0,length(j));
Ta(j+1) = alpha*Ta(j)+(1-alpha).*(T0(j)-k(j)*beta*Pac);
for m = 1:j
if (Ta(1:m) >= Tp+0.5)
k(m) = repelem(1,length(m));
Ta(m+1) = alpha*Ta(m)+(1-alpha).*(T0(m)-k(m)*beta*Pac);
end
end
end
end
plot(t,Ta) suppose to give a pulse-like shape, similar to the shape in orange in the attached file. But I'm not getting anything close.
I don't know what is wrong. Please assist. I'm very new to matlab.

7 件のコメント

Cris LaPierre
Cris LaPierre 2021 年 10 月 6 日
Depending how new to MATLAB you are, you may find going through MATLAB Onramp will get you up to speed faster than going it alone.
One question about your code. What do you want the result of this line of code to be?
t = 0:5/12:24;
shaddai
shaddai 2021 年 10 月 6 日
That line is my time interval, 25 minutes division over a 24 hour period. I will check the MATLAB Onramp out as suggested.
Cris LaPierre
Cris LaPierre 2021 年 10 月 6 日
編集済み: Cris LaPierre 2021 年 10 月 6 日
When I try to run your code, I get an error in the indexing a k. Basically, when you code loops the second time, k still only has 1 value in it, but your calcualtion tries to index the 2nd value, which doesn't exist.
a=0;
% works
a(1)
ans = 0
% doesn't work
a(2)
Index exceeds the number of array elements. Index must not exceed 1.
shaddai
shaddai 2021 年 10 月 6 日
Do you suggest that I make k into an array?
Cris LaPierre
Cris LaPierre 2021 年 10 月 6 日
編集済み: Cris LaPierre 2021 年 10 月 6 日
Your code will create k as a vector. The issue is your assignment is one step behind where it needs to be.
There is also a question of what the value of k should be if ?
shaddai
shaddai 2021 年 10 月 6 日
Thank you, Cris. I figured it out.
R = 2; C = 2.5; eta = 2.5; Pac = 5; Tp = 76; t_delta = 1;
t = 0:5/12:1440;
alpha = 1-t_delta/(60*R*C);
beta = eta*R*(9/5)+32;
T0 = 86 - 14*cos(0.0035*t);
Ta(1) = Tp;
k = 0;
for j = 1:length(t)-1
if (Ta(j) < Tp-0.5)
k = 0;
elseif (Ta(j) >= Tp+0.5)
k=1;
end
Ta(j+1) = alpha*Ta(j)+(1-alpha).*(T0(j)-k*beta*Pac);
end
plot(t,Ta,'r',t,T0,'b'),grid
Cris LaPierre
Cris LaPierre 2021 年 10 月 7 日
Nice. I suggest moving you last post to an Answer. You can accept your own answer, and doing so will let others know this question has a solution.

サインインしてコメントする。

回答 (0 件)

カテゴリ

質問済み:

2021 年 10 月 6 日

コメント済み:

2021 年 10 月 7 日

Community Treasure Hunt

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

Start Hunting!

Translated by