I am trying to write a function but it will not work.

조회 수: 4 (최근 30일)
Meghan
Meghan 2014년 11월 12일
댓글: Star Strider 2014년 11월 12일
function [ absorption ] = solar_rad( nc,lat,t,I)
%Calculating absorbed solar radiation
% Calculating the absorbed solar radiaiton as a function of time of
% day and year, cloud cover, and latitude, using a time step of one hour.
%BEGIN
for I=1:365;
for T=[0:24];
t=T*3600;
end
delta=-23.45*(pi/180)*cosd(2*pi)/365*(I+9); %Declination
S=1357+45*cosd((2*pi)/365)*(I); %Solar constant
for lat=0;
sinh=max(sind(lat)*sind(delta)+cosd(lat)*cosd(delta)*cosd((2*pi/86400)*(t+43200)));
m=1/sinh; %optical length
Cext=0.128-0.0253*(log(m)); %Extinction
end
for h=asin(max(sind(lat)*sind(delta)+cosd(lat)*cosd(delta)*cosd((2*pi)/86400)*(t+43200)));
i=(pi/2)-h; %angle of incidence
j=asin(0.75*sin(i)); %angle of reflection
r=0.5*abs((sin(i-j)^2/sin(i+j)^2)+(tan(i-j)^2)/(tan(i+j)^2)); %reflectance
end
end
for nc=0:100;
if h>0
Insd=S*(exp(-Cext*m))*(sin(h))*(1-(0.71*nc)); %direct incoming solar radiation at sea level
end
if h<=0
Insd=0;
end
for Insg=0.52*nc*Insd; %global radiation
Q_sun=Insd*(1-r)+0.97*Insg;
end
end
[absorption]=solar_rad(0,0,t,I);
Whenever I run it I get this error message:
Error in solar_rad (line 7)
for I=1:365;
Output argument "absorption" (and maybe others) not assigned during call to "\\fs-home-k\home-005\osp42f\My
Documents\MATLAB\Programmes\solar_rad.m>solar_rad".

채택된 답변

Star Strider
Star Strider 2014년 11월 12일
You don’t have any variable named ‘absorption’ on the left-hand side of any statement anywhere in your code for your ‘solar_rad’ function. It has to have a variable by that specific name assigned somewhere in your code or it will not return any value for that variable.
  댓글 수: 2
Meghan
Meghan 2014년 11월 12일
Yea I realised that and ended up fixing my code again! Now I'm just trying to figure out how to make a matrix joining I and T!
Star Strider
Star Strider 2014년 11월 12일
I don’t understand what you mean by ‘a matrix joining I and T’. ‘T’ and ‘I’ are indices.
Also, I don’t understand what you’re doing here:
cosd(2*pi)
since the trig functions ending with ‘d’ take their arguments in degrees. Those without take their arguments in radians. In any event, cos(2*pi) will always evaluate to 1, and cosd(2*pi) will always evaluate to 0.994.

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

추가 답변 (1개)

Adam
Adam 2014년 11월 12일
편집: Adam 2014년 11월 12일
r=0.5*abs((sin(i-j)^2/sin(i+j)^2)+(tan(i-j)^2)/(tan(i+j)^2));
That is your maths for 'r'. Without putting it into my Matlab it seems unlikely that will return an integer every time.
Q_sun=Insd(1-r)+0.97*Insg;
That is your maths indexing into Insd, which uses 'r'. Even if 'r' were an integer though the fact you use '1-r' means you will never end up with an integer index into your Insd array unless r == 0;
  댓글 수: 2
Meghan
Meghan 2014년 11월 12일
편집: Star Strider 2014년 11월 12일
I changed my code a little and now I get a new error message:
function [ absorption ] = solar_rad( nc,lat,t,I)
%Calculating absorbed solar radiation
% Calculating the absorbed solar radiaiton as a function of time of
% day and year, cloud cover, and latitude, using a time step of one hour.
%BEGIN
for I=1:365;
for T=[0:24];
t=T*3600;
end
delta=-23.45*(pi/180)*cosd(2*pi)/365*(I+9); %Declination
S=1357+45*cosd((2*pi)/365)*(I); %Solar constant
for lat=0;
sinh=max(sind(lat)*sind(delta)+cosd(lat)*cosd(delta)*cosd((2*pi/86400)*(t+43200)));
m=1/sinh; %optical length
Cext=0.128-0.0253*(log(m)); %Extinction
end
for h=asin(max(sind(lat)*sind(delta)+cosd(lat)*cosd(delta)*cosd((2*pi)/86400)*(t+43200)));
i=(pi/2)-h; %angle of incidence
j=asin(0.75*sin(i)); %angle of reflection
r=0.5*abs((sin(i-j)^2/sin(i+j)^2)+(tan(i-j)^2)/(tan(i+j)^2)); %reflectance
end
end
for nc=0:100;
if h>0
Insd=S*(exp(-Cext*m))*(sin(h))*(1-(0.71*nc)); %direct incoming solar radiation at sea level
end
if h<=0
Insd=0;
end
for Insg=0.52*nc*Insd; %global radiation
Q_sun=Insd*(1-r)+0.97*Insg;
end
end
[absorption]=solar_rad(0,0,t,I);
Error in solar_rad (line 7)
for I=1:365;
Output argument "absorption" (and maybe others) not assigned during call to "\\fs-home-k\home-005\osp42f\My
Documents\MATLAB\Programmes\solar_rad.m>solar_rad".
Any idea what I'm doing wrong? I've been trying to write this for 3 days!
Adam
Adam 2014년 11월 12일
편집: Adam 2014년 11월 12일
You don't want a ';' at the end of that for loop line, although that maybe does no harm.
The error message tells you exactly what the problem is though despite perhaps erroneously pointing at that line.
You have stated there should be an output argument called 'absorption' yet you never create it.

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

카테고리

Help CenterFile Exchange에서 Logical에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by