Creating a loop within a loop

[EDIT: 20111027 17:42 CDT - merge another revised question - WDR]
[EDIT: 20111027 17:10 CDT - merge revised question - WDR]
Hello everyone,
I'm presently trying to construct a loop within a loop that based on changing date (1:365) and changing latitude (0:90), and the subsequent effects on the below equation:
x=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*((t+1)+43200));
I've tried this:
for t=1:365
x(t+1)=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*((t+1)+43200));
for lat=0:90
x(lat+1)=sin(lat+1).*sin(Dec)+cos(lat+1).*cos(Dec).*cos(wd.*(t+43200));
end
end
x
and this:
for t=365;
lat=1;
x=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200));
while length(lat)==90;
lat=lat+1;
while length(t)==365;
t=t+1;
end
end
end
x;
Anyone have any ideas?
[Revised Question]
Hello everyone,
I'm presently trying to construct a loop with two variables, changing date (1:365) and changing latitude (0:90), with the aim of the loop to calculate all the possible outputs of the below equation:
x=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200));
So far i've tried this but it doesn't work:
lat=1;
t=1;
x=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200));
while length(lat)==90;
lat=lat+1;
x=[x,sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200))];
while length(t)==365;
t=t+1;
x=[x,sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200))];
end
end
x
[Revision #2]
I'm just trying to construct a loop that calculates all the possible outputs based on two variables (lat=0:90, t=1:365), with the equation:
x=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200));
So far, my latest attempt is:
for lat=0:90;
for t=1:365;
x(lat,t)=sin(lat).*sin(Dec)+cos(lat).*cos(Dec).*cos(wd.*(t+43200));
end
end
x
Can anyone recommend a workable method?

1 Commento

the cyclist
the cyclist il 27 Ott 2011
Please make your code more readable by using the "Code" markup button.

Accedi per commentare.

 Risposta accettata

For your revision #2:
[LAT,T] = ndgrid(0:90, 1:365);
x=sin(LAT).*sin(Dec)+cos(LAT).*cos(Dec).*cos(wd.*(T+43200));

8 Commenti

Josh
Josh il 27 Ott 2011
Apologies, I thought the question had been deleted.
Might I point out that LAT of 0 to 90 looks suspiciously like "degrees", but that sin() and cos() work on RADIANS ?
Josh
Josh il 27 Ott 2011
??? Error using ==> times
Matrix dimensions must agree.
The above code creates two grids but does not successfully calculate x, tried to manipulate it a bit but with no success yet.
Josh
Josh il 27 Ott 2011
That is an excellent point ^Radians^ - I may have had a rather embarrassing moment, I'll just check...
Is there a possibility that Dec or wd might not be scalars? The above code requires Dec and wd to be scalars. I did test it with a couple of sample scalar values, so I know that normally it would not give the message about matrix dimensions not agreeing.
When you are checking radians, be sure to check Dec and wd .
Josh
Josh il 28 Ott 2011
Dec and wd were already in radians, changed the grid to make it more radian appropriate but still have matrix dimensions error:
[LAT,T]=ndgrid(0:RC:RC*90,RC*1:RC:RC*365);
x=sin(LAT).*sin(Dec)+cos(LAT).*cos(Dec).*cos(wd.*(T+43200));
RC=0.0018 as an approximate radians calculation.
Dec is a 1x365 matrix and wd is a conversion coefficient.
Nothing like omitting some fundamental information :(
Your desired output is not well defined. You want one output per lat and t value, but sin(lat) .* sin(Dec) is going to result in a vector of values for a single lat and t value.
Perhaps what you want to calculate is
sin(lat) .* sin(Dec(t)) + cos(lat) .* cos(Dec(t)) .* cos(wd.*(t+43200))
If so...
[LAT,T] = ndgrid(0:90,1:365);
x = sind(LAT) .* sin(Dec(T)) + cosd(LAT) .* cos(Dec(T)) .* cosd(wd .* (T+43200));
Please be sure to cross-check degrees vs radians here. sind() and cosd() work on degrees, and are a lot easier to read in your situation.
Josh
Josh il 28 Ott 2011
Sorry, thank you so much :-)

Accedi per commentare.

Più risposte (1)

[T,LAT] = ndgrid(1:365,0:90);
x=sin(LAT).*sin(Dec)+cos(LAT).*cos(Dec).*cos(wd.*((T+1)+43200));

1 Commento

Josh
Josh il 27 Ott 2011
This creates the size of the grid I would explain as an output, but it doesn't calculate the outputs in relation to the equation.

Accedi per commentare.

Categorie

Scopri di più su Functions in Centro assistenza e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by