MATLAB Answers

how to get hourly meshgrids?

2 views (last 30 days)
Iris Kiebert
Iris Kiebert on 16 Dec 2019
Commented: Iris Kiebert on 16 Dec 2019
for counter=1:n_param
wh=(h+12)*(pi/12); %OPDRAcht 4
sinoaz=-23.45*(pi/180)*cos(((2*pi)/365)*(10+counter));
oaz=asin(sinoaz);
sinashadow=cos(oaz).*cosd(wh).*cosd(p)+sinoaz.*sind(p);
ashadow=asin(sinashadow);
sinAshadow=(sind(wh).*cos(oaz))./sinashadow;
Ashadow=asind(sinAshadow+0.5*pi);%in degrees and shadow is on opposite direction as sunlight
%Tan(a)=overstaande/aanliggende=height block/lenth shadow
%lenth shadow=heigth block/Tan(a)
lenthofshadow=heigthblock./(tan(ashadow));
x=0:lenthofshadow.*cos(Ashadow);
y=0:lenthofshadow.*sin(Ashadow);
figure
%[X,Y]=meshgrid(100,100);
%hold on;
%grid on;
%plot(x,y,'k');
%plot(y,x,'k');
%x=lenthofshadow.*cos(Ashadow);
%y=lenthofshadow.*sin(Ashadow);
A=ones(100,100);
A(0:y,0:x)=0;
axis off
while 15<ashadow
n_loop=n_loop+1;
if (n_loop >= max_loops)
disp('exceeded max number of loops')
break
end
end
i want to make a meshgrid for every hour so i can calculate the amount of shaduw of ech cell whan i count them all together but it gives an error at line 29(x=0:lenthofshadow.*cos(Ashadow);). when i put a . before* it doesnt run and without the . it gives me an error. opdr5
this is the error below:
Warning: Colon operands must be real scalars.
> In opdr5 (line 28)
Warning: Colon operands must be real scalars.
> In opdr5 (line 29)
Error using :
Maximum variable size allowed by the program is exceeded.
Error in opdr5 (line 29)
y=0:lenthofshadow.*sin(Ashadow);

  2 Comments

Guillaume
Guillaume on 16 Dec 2019
"i want to make a meshgrid for every hour"
What does that mean? Why would a grid change every hour?
"it gives an error at line 29"
We don't know which is line 29. Moreover, there's no comment in your code that explains what it does.
If you get an error, give us the full text of the error message.

Sign in to comment.

Answers (1)

Guillaume
Guillaume on 16 Dec 2019
It's unclear what you're trying to do with your code since it's not commented. There are certainly a number of things that don't look right:
1)
Ashadow = asind(sinAshadow+0.5*pi)
It's a bit unusual to add an angle to something you're going to take the arcsinus of. In any case, to get a real value out of that equation, sinAshadow+0.5*pi must be between -1 and 1. 0.5*pi is about 1.5, so this equation will result in a complex value if sinAshadow is outside [-1, -0.5].
2)
ashadow=asin(sinashadow);
%...
Ashadow = asind(sinAshadow+0.5*pi)
You have two variables whose name only differ by the case of the 1st letter. That's really asking for trouble. You can be sure at one point you'll be mistaking one for the other.
3)
lenthofshadow=heigthblock./(tan(ashadow));
You're using ./ on that first line which would imply you expect heightblock or ashadow (or both) to be vector, and hence lenthofshadow to be vector. You're then using that value on the right part of a colon. It's allowed to use a vector with the colon operator : but it's usually a mistake. Matlab will only use the first value of the vector.
In addition, if ashadow is 0, then the tangent is 0 and you've got a division by 0. Conversely, if ashadow is near pi/2, you've got infinite or very large tangent and lenthofshadow is going to be near 0.
4)
x=0:lenthofshadow.*cos(Ashadow);
y=0:lenthofshadow.*sin(Ashadow);
This will create a vector of integers from 0 to whatever lenthofshadow times the sinus or cosinus of Ashadow is. Depending on the angles, this can be negative (in which case x or y would be empty) or very large, in which case you'll run out of memory. In addition see point 1, Ashadow (with capital A) is very likely to be complex. From the warning messages you get, it looks like it is indeed complex and from the error, it looks like lenthofshadow.*cos(Ashadow) the real part is indeed very large. Matlab ignores the imaginary part in a : operation.
5)
A(0:y,0:x)=0;
For a start, indexing in matlab starts at 1, so the 0 on the left side of the : is invalid. Secondly, you create x and y as vectors, so it's likely that the right part is also a mistake. As said, matlab only uses the first element of a vector in a colon operation
I would recommend that you
a) check your equations
b) step through your code one line at a time using the debugger and see if each line produces what you were expecting (in term of value and number of elements).
c) write comments and do not use variable names that only differ by case.

  3 Comments

Iris Kiebert
Iris Kiebert on 16 Dec 2019
okay so im totally on the wrong track. the x and the y that i gave : x=0:lenthofshadow.*cos(Ashadow);
y=0:lenthofshadow.*sin(Ashadow);
are the coordinates for all cells the shadow covers. so could you please tell me if i should do something totally diffrent than what i posted
Steven Lord
Steven Lord on 16 Dec 2019
We don't understand what the code you've posted should do.
I second Guillaume's suggestion about writing comments. In fact, I'd suggest doing that first. Write down what steps you'll need to follow to achieve your goal. Use prose (words) not code for those comments and focus on what you do at each step not how you're going to complete that step.
Those comments capturing the what may help you when it comes time to figure out the how. Write code that does what your comment says the code should do, and if you're not sure how to implement a step break it down further into sub-steps. The words you use in your comments may also help you decide what descriptive variable names to use in the code.
Ideally the human reading the comments and the computer reading the code should understand what's going on equally well.

Sign in to comment.

Sign in to answer this question.

Products