Running a for loop for a function across two variables

3 views (last 30 days)
Hey all! I am trying to model the average temperature changes of the atmosphere and the earth's surface and I would like to see how changing surface albedo AND the longwave abosrbtion properties of the atmosphere affect the temp values of the model. so i created a function, declared surface albedo and lw absorbtion. Then in another script a created a nested for loop to see if i can get these variables to play with each other but the loop wont work. I think the problem is in my for my equations? thanks
%% earth emission increase
for n = 1:10
lwabsorbedatm = n/2;
[Tatm(n),Tsurf(n)]=modelwithatm(lwabsorbedatm);
for n = 1:10
surface_albedo = n/2;
[Tatm(n),Tsurf(n)]=modelwithatm(surface_albedo);
end
end
plot(.2:.2:2, Tatm)
hold on
plot(.2:.2:2, Tsurf)
hold off
legend('ta','ts')
axis([0 2 100 400])
%% and my function
function [Tatm, Tsurf] = modelwithatm(lwabsorbedatm,surface_albedo)
%variables
albedo =.31;
Ks = 1361; %W m-2
Re = 6173; %km
Sigma = 5.67E-8;%E m-2 k-4
Epsilon = 1;
% with atm variables
atm_albedo = .35;
%surface_albedo = .2;
swradiation_sun = .9;
lwradiation_sun = .1;
% atm absorbs .95 ALL lw and .02 ALL sw multiply by these values
Epsilon_atm = .9;
%lwabsorbedatm = .95
%symbols
syms Tsurf
syms Tatm
%equations
% absorbed by atm = 2swvectors sun/earthreflection, 2lw vectors sun/earth
% absorbed by surface sw sun, lw sun, lw atm
%sw
a = Ks*.9*pi*Re^2*0.02; % sun to atm
d = Ks*.9*pi*Re^2*(1-.02 -.35)*.2*.02; %sun to surface to atm
c = Ks*.9*pi*Re^2*(1-.02 -.35)*(1-surface_albedo); %sw sun to surface
%lw
q =Ks*.1*pi*Re^2*lwabsorbedatm; %Lw sun to atm
f = Ks*.1*pi*Re^2*(1-.95); % lw sun to surface
y = 4*pi*Re^2*Epsilon*Sigma*Tsurf^4*lwabsorbedatm; %lw earth to atm % whats the vibe?
z = 4*pi*Re^2*Epsilon_atm*Sigma*Tatm^4; %lw from atm to surface
p = 4*pi*Re^2*Epsilon*Sigma*Tsurf^4;%lw to atm % whats the vibe?
%what could change? what wont? why would they change?
%emission vectors
%lw emitted from surface = p
%lw emitted from atm or 2*z = atm emission
%sw emitted from atm = sun reflected to space = a (.98) rather than .02 &
%albedo .35 so basically c
%absorbption & emission equations pieces
Energy_absorbed_atm = a + d + q + (p*.95); %swsuntoatm + swsuntoearthtoatm + lwsuntoatm + lwearthtoatm
Energy_absorbed_surf = c + f + z; %swsuntoearth + lwsuntoearth + lwatmtoearth
Energy_emitted_atm = 2*z; %lwatmbothvectors
Energy_emitted_surface = p; %lwfromearth
%packed equations solving for temperatures
equationa = (Energy_absorbed_atm == Energy_emitted_atm);
equations = (Energy_absorbed_surf == Energy_emitted_surface);
[tempa, temps] = solve([equationa, equations],[Tatm,Tsurf]);
%equation solutions for temperature in absolute terms
Tsurf = abs(real(double(temps(1))))
Tatm = abs(real(double(tempa(1))))
end
  1 Comment
Theodore Anderson
Theodore Anderson on 15 Oct 2020
% Define t and g
t=1:10;
g=21:30
% Use a loop to compute a value that depends on t(k) and g(1:k)
for k = 1 : length(t)
% Get the scalar t, and g array needed
this_t = t(k);
this_g = g(1:k);
% Now pass those to some function
% that will compute an output value.
output(k) = SomeFunction(this_t, this_g);
end
% Print to command window
output
might something like this work?

Sign in to comment.

Answers (1)

Sudhakar Shinde
Sudhakar Shinde on 15 Oct 2020
Edited: Sudhakar Shinde on 15 Oct 2020
looks like your function is 2 input arguments and 2 output arguments:
function [Tatm, Tsurf] = modelwithatm(lwabsorbedatm,surface_albedo)
But you are calling by 1 input argument and 2 output argument. try replcing input argument by(~) whenever you dont want.
or else you can call this fucntion:
for n = 1:10
lwabsorbedatm = n/2;
surface_albedo = n/2;
[Tatm(n),Tsurf(n)]=modelwithatm(lwabsorbedatm,surface_albedo);
end

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!