sametsokel
sametsokel

Reputation: 384

I modelled the sun in R, but can't do the same in Simulink

I'm very new in modeling systems. With a friend's help, We created this model in R

hours <- seq(6+20/60, 18+31/60, 0.01)

h_radians <- (pi/12) * (hours - (12+26/60))

doy <- 268

decl_radians <- 23.45 * sin(2*pi*(284+doy)/365) * pi / 180

lat_radians <- 23.45 * pi / 180

sin_gamma <- sin(lat_radians)*sin(decl_radians) + cos(lat_radians)*cos(decl_radians)*cos(h_radians)

m <- 1/sin_gamma


irradiance[is.na(irradiance)] <- 0
irradiance <- 1353 * sin_gamma * 0.687 ^ (m ^ 0.678)

the output is like that;

enter image description here

When I try to move this mathematical model to simulink, cannot do it. That is what I tried to do

enter image description here

Actually, I cant imagine how the time is used in Simulink. Would you help me to fix my issue ?

Upvotes: 0

Views: 191

Answers (1)

Waldi
Waldi

Reputation: 41220

You could simply use a Matlab function block to make the calculation exactly as you did it in R: enter image description here

function irr = irradiance(h,day,lat)
  h_radians = (pi/12) * (h - (12+26/60));
  decl_radians = lat * sin(2*pi*(284+day)/365) * pi / 180;
  lat_radians = lat * pi / 180;
  sin_gamma = sin(lat_radians)*sin(decl_radians) + cos(lat_radians)*cos(decl_radians)*cos(h_radians);
  m = 1./sin_gamma;
  pow = m .^ 0.678;
  pow(imag(pow)~=0)=NaN;
  irr = 1353 .* sin_gamma .* 0.687 .^pow;
end

Then you should configure the solver to define simulation duration / step : enter image description here The simulation will run 12 hours with a step of 0.01 hour.
As you want to simulate between 6h and 18h, I added 6 hours to the simulation clock block.
Without surprise, the result is similar to what you got in R : Simulink can also "model the sun"! enter image description here

Upvotes: 1

Related Questions