How to compute several results from incremental input values using loops?

1 次查看(过去 30 天)
Hi, I'm new to MATLAB and I'm writing a program to compute a solar cell's operating temperature at different ambient temperature and a fixed solar irradiation levels. Say I want to compute Tcell starting at 32 until the value of X (where X = 32 + 0.2*N, N is number of iterations), so yeah basically it increments 32 by 0.2 and stops at a value X.
Here's my code btw but it's very messed up:
clc
I = input('Enter solar irradiance here: ');
A = input('Enter ambient temperature here: ');
N = input('Enter number of iterations to perform: ');
disp(' Ambient Temp NOCT Tcell ')
n = 1;
nFinal = N + 1;
i = 1;
S = I*0.1;
NOCT = 0.0174*I + 20;
format long
while (n <= nFinal)
Tc = A + (NOCT - 20) * (S/80);
disp([A NOCT Tc])
if (abs(Tc) <= 1E-6)
nFinal = n;
break;
end
n = n + 1
i = i + 1
end
  1 个评论
KALYAN ACHARJYA
KALYAN ACHARJYA 2019-11-18
编辑:KALYAN ACHARJYA 2019-11-18
I couldn't find the same logic in the code and expressed by the text. Note, one should always use the same variable name to text and code, so that the code is easily understood.

请先登录,再进行评论。

回答(1 个)

KSSV
KSSV 2019-11-18
x0 = 32 ;
dx = 0.2 ;
N = 20 ; % iterations needed
for i = 1:N
x = x0+(i-1)*dx ;
end
YOu can avoid for loop using:
x0 = 32 ;
dx = 0.2 ;
N = 20 ;
x = x0:dx:(x0+(N-1)*dx) ;

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

产品


版本

R2018a

Community Treasure Hunt

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

Start Hunting!

Translated by