0% found this document useful (0 votes)
92 views8 pages

Fractional Crank Nicolson Code

This document contains 7 MATLAB files that define functions for solving a fractional diffusion equation numerically using the L1 scheme. The main file sets up the problem, defines the mesh and time steps, computes the solution iteratively using the implicit scheme, and plots the exact, approximate, and error solutions. The other files define functions for the exact solution, its derivative, the right hand side of the equation, and computing elements of the mass and stiffness matrices and right hand side vector for each element.

Uploaded by

Pragya Dhakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
92 views8 pages

Fractional Crank Nicolson Code

This document contains 7 MATLAB files that define functions for solving a fractional diffusion equation numerically using the L1 scheme. The main file sets up the problem, defines the mesh and time steps, computes the solution iteratively using the implicit scheme, and plots the exact, approximate, and error solutions. The other files define functions for the exact solution, its derivative, the right hand side of the equation, and computing elements of the mass and stiffness matrices and right hand side vector for each element.

Uploaded by

Pragya Dhakar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 8

I matlab File

clear; close all; clc;

format long

for p=1:4

al=0.5;

al1=(1-al/2);

al2=al/2;

h=2^(-p-1);

x=0:h:1;

n=size(x,2);

nodes1=1:n-1;

nodes2=2:n;

for i=1:n-1;

nodes(i,1)=nodes1(i);

nodes(i,2)=nodes2(i);

end

T=1;

k=1/500; % time stepsize

d(p)=k;

NT=T/k;

U00=ones(size(x'))*0; %initial value

t=[1:1:NT]'*k;

1
exact=(t.^2)*sin(2*pi*x); % exact solution

exact=exact';

exact=[U00 exact];

UU=U00;

U_old=U00;

U_new=U_old;

w0=k^(al);

M=zeros(n,n);

S=zeros(n,n);

for e1=1:n-1

[dA,dB] = elementmatrix(x,nodes,e1 );

nn=nodes(e1,:);

M(nn,nn)=M(nn,nn)+dA;

S(nn,nn)=S(nn,nn)+dB;

end

A1=M+al1*w0*S;

tic

for j=1:NT

b=zeros(n,1);

U=zeros(n,1);

ssum=zeros(n,1);

kk=0;

for mm=1:j

2
kk(mm+1)=t(mm);

end

for e1=1:n-1

[db]=elementcontributions(al1*t(j)+al2*kk(j),x,nodes,e1,w0,al);

nn=nodes(e1,:);

b(nn)=b(nn)+db;

end

w=-al;

for k=1:j

w(k+1)=(1-(al+1)/(k+1))*w(k);

end

for g=2:j+1

ssum=ssum+w(g-1)*UU(:,j-g+2);

%w denotes the coefficients of the fractional time derivative

end

R=ssum;

for nl=1:60

U_tmp=U_new;

b1=h*((al1*U_tmp+al2*U_old).^2+sin(al1*U_tmp+al2*U_old));

Mdf=zeros(n,n);

pf=al1*U_tmp+al2*U_old;

for i=1:n-1

3
xmid=(pf(i+1)+pf(i))/2;

amid=df(xmid);

Mdf(i,i)= Mdf(i,i)+(amid)*(h/3);

Mdf(i,i+1)= Mdf(i,i+1)+(amid)*(h/6);

Mdf(i+1,i)= Mdf(i+1,i)+(amid)*(h/6);

Mdf(i+1,i+1)= Mdf(i+1,i+1)+(amid)*(h/3);

end

J=A1-al*w0*Mdf;

rho=A1*U_new+M*R-b-w0*b1+w0*al2*S*U_old;

innodes=2:n-1;

A11=J(innodes,innodes);

b11=rho(innodes);

U1=A11\b11;

U_new(innodes)=U_tmp(innodes)-U1;

fixpterror=norm(U_tmp-U_new);

if fixpterror<10^(-7)

break

end

end

UU=[UU U_new];

U_old=U_new;

end

end

4
figure(1)

subplot(1,2,1)

mesh(exact)

xlabel('t');ylabel('x');

title('The exact solution u(t, x) at t=1 for \alpha =0.1')

subplot(1,2,2)

mesh(UU)

xlabel('t');ylabel('x');

title('The approximate solution U^{N} at t_{N}=1 for \alpha =0.1')

error=UU-exact;

figure(2)

mesh(error)

xlabel('t');ylabel('x');

title('The error at t=1 for \alpha =0.1')

toc

II matlab File

function uv=ue(t,x)

uv=sin(2*pi*x)*t^2;

end

III matlab File

function uxv=uxe(t,x)

uxv=2*pi*cos(2*pi*x)*t^2;

5
end

IV matlab File

function [y ] = right(t,x,al)

y=(2/gamma(3-al))*t^(2-al)*sin(2*pi*x)+4*pi^2*t^2*sin(2*pi*x)-(t^2
*sin(2*pi*x))^2-sin(t^2*sin(2*pi*x));

end

V matlab File

function [dA,dB] = elementmatrix(x,nodes,e1 )

%ELEMENTCONTRIBUTION Summary of this function goes here

% Detailed explanation goes here

n1=nodes(e1,1);

n2=nodes(e1,2);

x1=x(n1);

x2=x(n2);

length=x2-x1;

localmass=[1/3*length 1/6*length; 1/6*length 1/3*length];

localstiffness=[1/length -1/length; -1/length 1/length];

dA=localmass;

dB=localstiffness;

end

VI matlab File

function [db] = elementcontributions( t,x,nodes,e1,w0,al )

%ELEMENTCONTRIBUTION Summary of this function goes here

6
% Detailed explanation goes here

n1=nodes(e1,1);

n2=nodes(e1,2);

x1=x(n1);

x2=x(n2);

length=x2-x1;

f=[(length/2)*right(t,(x1/2+x2/2),al);(length/2)*right(t,(x1/2+x2/2),al)];

%f=0;

db=w0*f;

end

VII matlab File

function y=df(U)

y=2*(U)+cos(U);

end

7
8

You might also like