0

I have created this loop in order to find a temperature, change in albedo and latitude ice reaches after 100 iterations of the code.

import numpy as np
import matplotlib.pyplot as plt

#define constants
L = 1280
albedo = 0.15
nIters = 100
LRange = [1200, 1600] #W/m2
Episilon = 1 #no units
Sigma = 5.67E-08 #Wm-2K-4


Ice_Lat_m = 1.5
Ice_lat_c = -322.5
Alb_m = -0.01
Alb_c = 2.8

plotType = "iterDown" #"L", "iterUp", "iterDown"

x = []
y = []
while (L > LRange[0] - 1):
    for iter in range(nIters):
        T = ((L * ( 1- albedo)) / 4) / Sigma
        T = T ** (1/4)
        albedo = Alb_m * T + Alb_c
        albedo = min(albedo, 0.65)
        albedo = max(albedo, 0.15)
        lat_ice = Ice_Lat_m * T + Ice_lat_c
        lat_ice = min(lat_ice, 90)
        lat_ice = max(lat_ice, 0)
        if plotType is "iter" or plotType is "iterDown":
            x.append(iter)
            y.append(T)
    if plotType is "iter" or plotType is "iterDown":
        x.append(np.nan)
        y.append(np.nan)
    if plotType is "L":
        x.append(L)
        y.append(T)
    L = L - 10

while (L < LRange[1] + 1):
    for iter in range(nIters):
        T = ((L * ( 1- albedo)) / 4) / Sigma
        T = T ** (1/4)
        albedo = Alb_m * T + Alb_c
        albedo = min(albedo, 0.65)
        albedo = max(albedo, 0.15)
        lat_ice = Ice_Lat_m * T + Ice_lat_c
        lat_ice = min(lat_ice, 90)
        lat_ice = max(lat_ice, 0)
        if plotType is "iter" or plotType is "iterDown":
            x.append(iter)
            y.append(T)
    if plotType is "iter" or plotType is "iterDown":
        x.append(np.nan)
        y.append(np.nan)
    if plotType is "L":
        x.append(L)
        y.append(T)
    L = L + 10    

plt.plot(x, y)
plt.show()
print(T, albedo, lat_ice)

The output from this code is:

278.2748546226214 0.15 90

This tells me that the code is using the variable "LRange[1]" as the input into the loop rather than the variable "L" when it is within the range defined "LRange".

Desired output should be:

255.45242794389384 0.24547572056106137 

Could anyone explain to me why this is happening? It would be greatly appreciated!

1 Answer 1

1

You have two loops there: The first loop will count L down from 1280 to 1200, and for the second loop it will thus start with 1200.

You probably want to reset L before the second loop.

Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for responding. Having played around with this a bit more I have realised that both loops are using the variable LRange[0] rather L in the loop rather than L variable that has been defined. Do you know why that would be?
It is hard to infer what your code tries to do. Can you please explain what your code tries to do (in terms of L and LRange[])?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.