print "=== Problema N° 1 ==="
c = [0.004, 0.0087, 0.019, 0.024, 0.094, 0.195]
q = [0.026, 0.053, 0.075, 0.082, 0.123, 0.129]
print "c en kg/L y q en kg/kg"
datos_eq = zip(c,q)
%var c_A, q_A_max, K_Ac, n
L = 15
c0 = 1.39
M = 180.1559
S1 = 25
q0 = 0
S2 = 25
c0 = c0*M/1000
print "parte a) Ajustar a el modelo de Langmuir y Freundlich"
show("===== Isoterma de Langmuir =====")
qA_L(c_A) = q_A_max*K_Ac*c_A / (1+K_Ac*c_A)
ajuste = find_fit(datos_eq, qA_L, solution_dict = True)
qA_L = qA_L(q_A_max= numerical_approx(ajuste[q_A_max], digits = 4), K_Ac= numerical_approx(ajuste[K_Ac], digits=4))
show(r"$K_{A,c} = $ ", numerical_approx(ajuste[K_Ac], digits=4), r" L/kg; $q_{A,máx} = $", numerical_approx(ajuste[q_A_max], digits=4), " kg/kg")
show("$q_{A_L} = $", qA_L)
show("===== Isoterma de Freundlich =====")
qA_F(c_A) = K_Ac*c_A^n
ajuste = find_fit(datos_eq, qA_F, solution_dict = True)
qA_F = qA_F(n= numerical_approx(ajuste[n], digits=4), K_Ac=numerical_approx(ajuste[K_Ac], digits=4))
show(r"$K_{A,c} = $ ", numerical_approx(ajuste[K_Ac], digits=4), r" L/kg; $n = $", numerical_approx(ajuste[n], digits=4))
show("$q_{A_F} = $", qA_F)
gr_datos = scatter_plot(datos_eq, axes_labels = ['$c_A$', '$q_A$'])
gr_Langm = plot(qA_L, xmin = 0, xmax = max(max(c),c0), legend_label = 'Langmuir', color = 'blue')
gr_Frlch = plot(qA_F, xmin = 0, xmax = max(c), legend_label = 'Freundlich', color = 'green')
show(gr_datos+gr_Langm+gr_Frlch)
print "parte b) Una etapa"
def Calcula_etapa(W_L, W_S, c_entra, q_entra, q_isot):
R.<c_sale> = QQ[]
q_sale = q_isot(c_sale)
f = c_sale - (W_S/W_L * (q_entra - q_sale) + c_entra)
csale = find_root(f, 0, c_entra)
return [csale, q_sale(csale)]
c1, q1 = Calcula_etapa(L, S1, c0, q0, qA_L)
show (r'$c_1 = %s$ kg/L; $q_1 = %s$ kg/kg' %(numerical_approx(c1, digits = 3), numerical_approx(q1, digits=3)))
print "parte c) Dos etapas a flujo cruzado, idénticas cantidades de sólido"
c2, q2 = Calcula_etapa(L, S2, c1, q0, qA_L)
show (r'$c_2 = %s$ kg/L; $q_2 = %s$ kg/kg' %(numerical_approx(c2, digits = 3), numerical_approx(q2, digits=3)))
gr_etapa1 = line([(c0,q0),(c1,q1),(c1,0)], color = 'purple', legend_label = 'Etapa 1')
gr_etapa2 = line([(c1,q0),(c2,q2),(c2,0)], color = 'magenta', legend_label = 'Etapa 2')
show(gr_datos+gr_Langm+gr_etapa1+gr_etapa2)
print "parte d) Dos etapas a flujo cruzado, calcular sólido mínimo"
S1m = L*(c0-c_A)/(qA_L(c_A) + q0)
S2m = L*(c_A-c2)/(qA_L(c2) + q0)
STm = S1m+S2m
S_Tm, c1 = find_local_minimum(STm, c2, c0)
show (r"$S_{T}$ /kg $=$", STm)
show (r"$S_{T, mín}= %s$ kg; $c_{1}= %s$ kg/L" %(numerical_approx(S_Tm, digits=3), numerical_approx(c1, digits=3)))
show (r"$Ahorro = %s $ kg" %(numerical_approx(((S1+S2)-S_Tm), digits=3)))
show (r"$S_{1}= %s$ kg; $S_{2}= %s$ kg" %(numerical_approx(S1m(c1), digits=3), numerical_approx(S2m(c1), digits=3)))
=== Problema N° 1 ===
c en kg/L y q en kg/kg
parte a) Ajustar a el modelo de Langmuir y Freundlich
===== Isoterma de Langmuir =====
KA,c= 61.43 L/kg;
qA,maˊx= 0.1413 kg/kg
qAL= 61.43cA+18.678cA ===== Isoterma de Freundlich =====
KA,c= 0.2282 L/kg;
n= 0.3019 qAF= 0.2282cA0.3019 
parte b) Una etapa
<string>:1: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
c1=0.0632 kg/L;
q1=0.112 kg/kg
parte c) Dos etapas a flujo cruzado, idénticas cantidades de sólido
c2=0.00531 kg/L;
q2=0.0347 kg/kg

parte d) Dos etapas a flujo cruzado, calcular sólido mínimo
/projects/sage/sage-7.3/local/lib/python2.7/site-packages/smc_sagews/sage_server.py:957: DeprecationWarning: Substitution using function-call syntax and unnamed arguments is deprecated and will be removed from a future release of Sage; you can use named arguments instead, like EXPR(x=..., y=...)
See http://trac.sagemath.org/5930 for details.
exec compile(block+'\n', '', 'single') in namespace, locals
ST /kg
= 431.9cA+cA0.1152(61.43cA+1)(−15cA+3.75625051500000)−2.292 ST,mıˊn=46.3 kg;
c1=0.0365 kg/L
Ahorro=3.68 kg
S1=32.9 kg;
S2=13.5 kg