%Case 2
%k(x)*T''+4*hc/d(x)*(Tinf-T)+100=0
%(T(xi+1)-(2+h^2/d)*T(xi)+T(xi-1))=(-100-4/d*Tinf)*h^2/k
%T(n+1)=T(n-1), T(0)=0
h=[.01 .005 .001 .0005 .00025];
Tinf=-5;T0=0;hc=1;
kpipe=54;dpipe=.05;
kss=16;dthick=.15;
kplat=70;kcu=401;
for k=1:5
    n=round(2.5/h(k));
    tnp=[1 -(2+h(k)^2*4*hc/(dpipe*kpipe)) 1];
    tnh=[1 -(2+h(k)^2*4*hc/(dthick*kss)) 1];
    tnc=[1 -(2+h(k)^2*4*hc/(dthick*kplat)) 1];
    tnm=[1 -(2+h(k)^2*4*hc/(dthick*kcu)) 1];
    tsolp=(-4*hc/dpipe*Tinf)*h(k)^2/kpipe;
    tsolh=(-4*hc/dthick*Tinf)*h(k)^2/kss;
    tsolc=(-100000-4*hc/dthick*Tinf)*h(k)^2/kplat;
    tsolm=(-4*hc/dthick*Tinf)*h(k)^2/kcu;
    a=zeros(n);
    a(1,1:2)=[-(2+h(k)^2*4*hc/(dpipe*kpipe)) 1];
    a(n,(n-1):n)=[2, -(2+h(k)^2*4*hc/(dpipe*kpipe))];
    tsol=zeros(n,1);
    tsol(1)=(-4*hc/dpipe*Tinf)*h(k)^2/kpipe-T0;
    for i=2:(n*.4)
        a(i,(i-1):(i+1))=tnp;
        tsol(i)=tsolp;
    end
    for i=(n*.4+1):(n*.44)
        a(i,(i-1):(i+1))=tnh;
        tsol(i)=tsolh;
    end
    for i=(n*.44+1):(n*.46)
        a(i,(i-1):(i+1))=tnc;
        tsol(i)=tsolc;
    end
    for i=(n*.46+1):(n*.5)
        a(i,(i-1):(i+1))=tnh;
        tsol(i)=tsolh;
    end
    for i=(n*.5+1):(n*1.75/2.5)
        a(i,i-1:i+1)= tnp;
        tsol(i)=tsolp;
    end
    for i=(n*1.75/2.5+1):(n*.8)
        a(i,i-1:i+1)= tnm;
        tsol(i)=tsolm;
    end
    for i=(n*.8+1):(n-1)
        a(i,i-1:i+1)= tnp;
        tsol(i)=tsolp;
    end
    temp=a\tsol;
    plot((0:h(k):2.5),[0,temp'])
    xlabel('Distance (m)')
    ylabel('Temperature')
    hold on
    Tavg(k)=sum(temp)/numel(temp);
    num(k)=n;
end
legend('h=.01','h=.005','h=.0001','h=.0005','h=.00025'),title('Case 1')
hold off
figure
plot(num,Tavg,'--o')
title('Average Temperature vs. Number of Elements')
xlabel('Number of Elements')
ylabel('Average Temperature (C)')