clc; clear;  %最小化， 优化参数
global F v L T T0 u0 t0
F=[25,175,195,235,255,25];v=7/6;L=50+30.5*11+5*10; T=L/v; T0=19;
filename="附件.xlsx";
E=xlsread(filename,1);
u0=E(:,2); t0=E(:,1);
xm=[6.955337193621688e-04;2.738544323025462e+03;0.001107930618471;2.563787849379793e+02;5.929551984955881e-04;5.282989961113213e+02]; %fv=9.717344977672112e+02
%xm=[7.094792228468003e-04;2.019401739773151e+03;0.001176162262659;2.082486120937800e+02;6.080931613908350e-04;4.602488848608684e+02],fv=8.793774286691689e+02;
%xm=[7.114481817530871e-04;1.923365271990470e+03;0.001184466631737;2.034140604282468e+02;6.066493954535490e-04;4.679787229260577e+02];fv=8.788398701536670e+02;
%[xm, fv]=CLSPSO(@zuixiao, 100 ,0.8, 2, 2,[9e-4,4000,2e-3,600,7e-4,600],[4e-4,1000,4e-4,100,4e-4,200],500,6);
%xm=[7.114481817530871e-04;1.923365271990470e+03;0.001184466631737;2.034140604282468e+02;6.066493954535490e-04;4.679787229260577e+02];
u=heat(xm(1),xm(2),xm(3),xm(4),xm(5),xm(6));
tmp=u-u0;
figure
ax1=subplot(2,1,1); ax2=subplot(2,1,2);
plot(ax1,t0,u0,t0,u);
xlabel(ax1, '距离（cm）');ylabel(ax1,'温度（℃）');
title(ax1,'实验温度与模型温度比较图');
legend(ax1,'实验温度','模型温度');
plot(ax2,t0,u-u0);
title(ax2,'模型温度与实验温度差');
xlabel(ax2, '距离（cm）');ylabel(ax2,'温度（℃）');


function y=zuixiao(x)
global u0
u=heat(x(1),x(2),x(3),x(4),x(5),x(6));
tmp=u-u0;
y=tmp'*tmp;
end



function y=f(x)
global F
l=5; L=30.5; s=25;
x1=0; x2=25;
x3=x2+5*L+4*l; x4=x3+l;
x5=x4+L;x6=x5+l;
x7=x6+L;x8=x7+l;
x9=x8+2*L+l;x10=x9+l;
y=((F(2)-F(1))/s.*(x-x1)+F(1)).*(x<=x2)+F(2).*(x>x2).*(x<=x3)+((F(3)-F(2))/l.*(x-x3)+F(2)).*(x>x3).*(x<=x4)+F(3).*(x>x4).*(x<=x5)...
+((F(4)-F(3))/l.*(x-x5)+F(3)).*(x>x5).*(x<=x6)+F(4).*(x>x6).*(x<=x7)...
+((F(5)-F(4))/l.*(x-x7)+F(4)).*(x>x7).*(x<=x8)+F(5).*(x>x8).*(x<=x9)...
+((F(6)-F(5))/l.*(x-x9)+F(5)).*(x>x9).*(x<=x10) + F(6).*(x>x10);
end

function t=heat(a1,h1,a2,h2,a3,h3)     % a^2=k/(c*rho)  h=h1/k, h1 热交换系数
    global T T0  v
    L1=25+5*30.5+5*5; 
    t1=L1/v;dt=0.5; 
    m1=floor(t1/dt)+1;
    l=0.015; 
    x=1e-4; 
    r1=a1^2*dt/(x^2);
    r2=a2^2*dt/(x^2);
    r3=a3^2*dt/(x^2);
    n=ceil(l/x)+1; m=floor(T/dt)+1;
    u=zeros(n,m);t=ones(m,1)*25;
    u(:,1)=25;
    u0=f(v*(0:floor(T/dt))*dt);
    k=ceil(l/2/x);i=floor(T0/dt)+1;
    A1=diag([1+h1*x,2*ones(1,n-2)*(1+r1),1+h1*x]);
    A1=A1+diag([-1,-r1*ones(1,n-2)],1);
    A1=A1+diag([-r1*ones(1,n-2),-1],-1);          
    B1=diag([0,2*ones(1,n-2)*(1-r1),0]);
    B1=B1+diag([0,r1*ones(1,n-2)],1);
    B1=B1+diag([r1*ones(1,n-2),0],-1);
    C1=A1\B1;
    c=zeros(n,m); c(1,:)=h1*u0*x; 
    c(n,:)=c(1,:);
    c=A1\c;    
    for j=1:m1-1
       u(:,j+1)=C1*u(:,j)+c(:,j+1);
       t(j+1)=u(k,j+1);
    end
    
    A2=diag([1+h2*x,2*ones(1,n-2)*(1+r2),1+h2*x]);
    A2=A2+diag([-1,-r2*ones(1,n-2)],1);
    A2=A2+diag([-r2*ones(1,n-2),-1],-1);          
    B2=diag([0,2*ones(1,n-2)*(1-r2),0]);
    B2=B2+diag([0,r2*ones(1,n-2)],1);
    B2=B2+diag([r2*ones(1,n-2),0],-1);
    C2=A2\B2;
    c2(1,:)=h2*u0*x; 
    c2(n,:)=c2(1,:);
    c2=A2\c2;    
        
    A3=diag([1+h3*x,2*ones(1,n-2)*(1+r3),1+h3*x]);
    A3=A3+diag([-1,-r3*ones(1,n-2)],1);
    A3=A3+diag([-r3*ones(1,n-2),-1],-1);          
    B3=diag([0,2*ones(1,n-2)*(1-r3),0]);
    B3=B3+diag([0,r3*ones(1,n-2)],1);
    B3=B3+diag([r3*ones(1,n-2),0],-1);
    C3=A3\B3;
    c3(1,:)=h3*u0*x; 
    c3(n,:)=c3(1,:);
    c3=A3\c3;    
    for j=m1:m-1
       if t(j)>=t(j-1)
            u(:,j+1)=C2*u(:,j)+c2(:,j+1);            
       else
           u(:,j+1)=C3*u(:,j)+c3(:,j+1); 
       end
       t(j+1)=u(k,j+1);
    end
    t=t(i:length(t));
end