clear;clc
%%%%%%%%%%%%方程里的参量%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
alpha=0.5;beta=0.5;
%%%%%%%%%%%%%%%%%%%%%%%%%%网格划分%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
L=1;%区间x的长度
h=0.1; %划分小区间 Delta x的长度
Mx=L/h; %划分的份数
x=[1:Mx-1]*h; %得到 x 的网格值注意是去掉 x=0 和 x=1 的.[1,Mx-1]
tau=h;
N=10; % 时间 t 的层数
t=[1:N]*tau; %时间t的网格点
%%%%%%%%%%%%%%%%%%%%系数矩阵里的参数定义%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
R=(tau^alpha)*(gamma(2-alpha))/(h*h);
for j=1:Mx-1
r(j)=beta*R/j;
end
r;
%%%%%%%%%%%%%%%%%%%%%%%%分数阶系数矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
v0=1+2*R-r; %主对角线上元素
A=diag(v0);
v1=r-R ;
v1=v1(2:end);%下次对角线上的元素
B=diag(v1,-1);
v3=-R*ones(1,Mx-2); %上次对角线上的元素
C=diag(v3,1);
D=A+B+C;
E=inv(D);
%%%%%%%%%%%%%%%%%非齐次项f(x,t)构成的矩阵%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:N-1
for j=1:Mx-1
f(k,j)=(t(k).^(1-alpha))*(sin(pi*x(j)))/gamma(2-alpha)-pi*t(k)*(beta*cos(pi*x(j))/x(j)-pi*sin(pi*x(j)));
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%递推关系%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:N
w(k)=(1+k)^(1-alpha)-k^(1-alpha);
end
for k=1:N-1
d(k)=w(k)-w(k+1);
end
u(1,:)=E*((tau^alpha)*gamma(2-alpha)*f(1,:))';
u(2,:)=E*((1-w(1))*u(1,:)+(tau^alpha)*gamma(2-alpha)*f(2,:))';
for n=2:N-2
for k=1:n-1
s(k,:)=d(n-k)*u(k,:);
end
u(n+1,:)=E*((1-w(1))*u(n,:)+sum(s(1:n-1,:),1)+(tau^alpha)*gamma(2-alpha)*f(n+1,:))';
end
u;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%计算误差%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
for k=1:N-1
for j=1:Mx-1
e(k,j)=u(k,j)-t(k)*sin(pi*x(j));
end
end
err=max(max(abs(e))) %最大误差
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%画图u(x,t)%%%%%%%%%%%%%%%%%%%%%%%%%%