MATLAB: How to create nested for loops that has two changing variables to output one variable

nested for loop

This is the code I am trying to run, and I am unsure how to write nested for loops to run over a certain time period and length.
Context: I am trying to find what the function will look like after the time ends and how r and hz evolve. Ultimately I will want to plot the r and hz together.
%Intial Conditions
rho=(3.6*30*24*60*60); %3.6 months
u=10^9;
u0=2*10^-5;
v=1*10^6;
r0=2000; %Intial radius
h0=180; %Intial height
V=(3/4)*pi()*(r0^2)*h0; %volume
v0=1*10^6;
g=1.31;
tau=((3/4)^5)*(((pi()^3)*v0*(r0^8))/(g*V^3));
%% Theta
for t=1:172800; %Using the time for 2 days
theta(t)=rho*(1-exp(-t/rho));
end
%% Solving function
for r=1:3000;
for ta=1:theta;
hz(r,ta)=(((4*V)/(3*pi()*r0^2)).*(1./((1+(ta./tau)).^1/4)).*(1-(((r.^2)./(r0^2)).*(1./(1+((ta./tau).^1/4))))).^(1/3));
end
end

Best Answer

rho=(3.6*30*24*60*60); %3.6 months
u=10^9;
u0=2*10^-5;
v=1*10^6;
r0=2000; %Intial radius
h0=180; %Intial height
V=(3/4)*pi()*(r0^2)*h0; %volume
v0=1*10^6;
g=1.31;
tau=((3/4)^5)*(((pi()^3)*v0*(r0^8))/(g*V^3));
%% Theta
for t=1:172800; %Using the time for 2 days
theta(t)=rho*(1-exp(-t/rho));
end
%% Solving function
hz = zeros(3000, length(theta));
for r=1:3000;
for ta=1:length(theta);
hz(r,ta)=(((4*V)/(3*pi()*r0^2)).*(1./((1+(theta(ta)./tau)).^1/4)).*(1-(((r.^2)./(r0^2)).*(1./(1+((theta(ta)./tau).^1/4))))).^(1/3));
end
end
This is rather slow and you should try to vectorize it.