0

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 ?

sametsokel
  • 334
  • 3
  • 11
  • 1
    I love this title! xD Simulink uses non-equally spaced time stamps. The depend on the function, because Simulink tries to mimic continuous time. This might a handicap (but you can fix it by setting the time to a fixed step size). Why are you using Simulink over MATLAB scripts (they seem a bit closer to R)? – max Jul 30 '20 at 14:46
  • I am glad you liked title :D The reason why I am using Simulink is that we are modelling a bigger system, the sun is a subsytem. Is there any way to fix this issue ? I couldn't grasp how to get a whole day simulation by this idea.. by the way, like I said I am so new in MATLAB and Simulink environment, I just have a R and Python background. – sametsokel Jul 30 '20 at 15:00

1 Answers1

1

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

Waldi
  • 39,242
  • 6
  • 30
  • 78
  • what a clear solution ! thank you for your perfect explaining to simulink rookie. but the point I don't understand why the sentence "modeling the sun" is funny – sametsokel Aug 01 '20 at 01:01
  • @sametsokel, happy I could help. I found "modeling the sun" funny probably because of previous comments, and because it looks like quite a wide challenge. "Modeling sun irradiance" looks more reasonable. – Waldi Aug 01 '20 at 05:25
  • sun irradiance would absolutely sound more sensible, you so right :) – sametsokel Aug 01 '20 at 10:48