Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
personqianduixue committed Mar 3, 2022
1 parent 209aeb5 commit 7f6a456
Show file tree
Hide file tree
Showing 55 changed files with 5,047 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
function y = code1(A,B,C,x0)

test = -1;

delta = 0.001;
N = 1000;
t = linspace(0,1,N+1);
h = 1/N;
h2 = h/2;

u = zeros(1,N+1);

x = zeros(1,N+1);
x(1) = x0;
lambda = zeros(1,N+1);

while(test < 0)

oldu = u;
oldx = x;
oldlambda = lambda;

for i = 1:N
k1 = -0.5*x(i)^2 + C*u(i);
k2 = -0.5*(x(i) + h2*k1)^2 + C*0.5*(u(i) + u(i+1));
k3 = -0.5*(x(i) + h2*k2)^2 + C*0.5*(u(i) + u(i+1));
k4 = -0.5*(x(i) + h*k3)^2 + C*u(i+1);
x(i+1) = x(i) + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

for i = 1:N
j = N + 2 - i;
k1 = -A + lambda(j)*x(j);
k2 = -A + (lambda(j) - h2*k1)*0.5*(x(j)+x(j-1));
k3 = -A + (lambda(j) - h2*k2)*0.5*(x(j)+x(j-1));
k4 = -A + (lambda(j) - h*k3)*x(j-1);
lambda(j-1) = lambda(j) - ...
(h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

u1 = C*lambda/(2*B);
u = 0.5*(u1 + oldu);

temp1 = delta*sum(abs(u)) - sum(abs(oldu - u));
temp2 = delta*sum(abs(x)) - sum(abs(oldx - x));
temp3 = delta*sum(abs(lambda)) - ...
sum(abs(oldlambda - lambda));
test = min(temp1, min(temp2, temp3));
end

y(1,:) = t;
y(2,:) = x;
y(3,:) = lambda;
y(4,:) = u;
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
function y = code10(a,b,c,x10,A,l,T)

test = -1;

delta = 0.001;
N = 1000;
t = linspace(0,T,N+1);
h = T/N;
h2 = h/2;

x1 = zeros(1,N+1);
x2 = zeros(1,N+1);
x1(1) = x10;

lambda1 = zeros(1,N+1);
lambda2 = zeros(1,N+1);

u = zeros(1,N+1);

while(test < 0)

oldu = u;
oldx1 = x1;
oldx2 = x2;
oldlambda1 = lambda1;
oldlambda2 = lambda2;

for i = 1:N
k11 = -a*x1(i) - b*x2(i);
k12 = -c*x2(i) + u(i);

k21 = -a*(x1(i)+h2*k11) - b*(x2(i)+h2*k12);
k22 = -c*(x2(i)+h2*k12) + 0.5*(u(i)+u(i+1));

k31 = -a*(x1(i)+h2*k21) - b*(x2(i)+h2*k22);
k32 = -c*(x2(i)+h2*k22) + 0.5*(u(i)+u(i+1));

k41 = -a*(x1(i)+h*k31) - b*(x2(i)+h*k32);
k42 = -c*(x2(i)+h*k32) + u(i+1);

x1(i+1) = x1(i) + (h/6)*(k11 + 2*k21 + 2*k31 + k41);
x2(i+1) = x2(i) + (h/6)*(k12 + 2*k22 + 2*k32 + k42);
end

for i = 1:N
j = N + 2 - i;
k11 = -2*A*(x1(j) - l) + a*lambda1(j);
k12 = b*lambda1(j) + c*lambda2(j);

k21 = -2*A*(0.5*(x1(j)+x1(j-1)) - l) + a*(lambda1(j)-h2*k11);
k22 = b*(lambda1(j)-h2*k11) + c*(lambda2(j)+h2*k12);

k31 = -2*A*(0.5*(x1(j)+x1(j-1)) - l) + a*(lambda1(j)-h2*k21);
k32 = b*(lambda1(j)-h2*k21) + c*(lambda2(j)+h2*k22);

k41 = -2*A*(x1(j-1) - l) + a*(lambda1(j)-h*k31);
k42 = b*(lambda1(j)-h*k31) + c*(lambda2(j)+h*k32);

lambda1(j-1) = lambda1(j) - (h/6)*(k11 + 2*k21 + 2*k31 + k41);
lambda2(j-1) = lambda2(j) - (h/6)*(k12 + 2*k22 + 2*k32 + k42);
end

u1 = -lambda2./2;
u = 0.5*(u1+oldu);

temp1 = delta*sum(abs(u)) - sum(abs(oldu - u));
temp2 = delta*sum(abs(x1)) - sum(abs(oldx1 - x1));
temp3 = delta*sum(abs(x2)) - sum(abs(oldx2 - x2));
temp4 = delta*sum(abs(lambda1)) - sum(abs(oldlambda1 - lambda1));
temp5 = delta*sum(abs(lambda2)) - sum(abs(oldlambda2 - lambda2));
test = min(temp1, min(temp2, min(temp3, min(temp4, temp5))));
end

y(1,:) = t;
y(2,:) = x1;
y(3,:) = x2;
y(4,:) = u;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
function y = code11(x0,k,r,T)

test = -1;

delta = 0.001;
N = 1000;
t = linspace(0,T,N+1);
h = T/1000;
h2 = h/2;

x = zeros(1,N+1);
lambda = zeros(1,N+1);
u = zeros(1,N+1);

x(1) = x0;

while(test < 0)

oldu = u;
oldx = x;
oldlambda = lambda;

for i = 1:N
k1 = k*u(i)*x(i);
k2 = k*0.5*(u(i)+u(i+1))*(x(i)+h2*k1);
k3 = k*0.5*(u(i)+u(i+1))*(x(i)+h2*k2);
k4 = k*u(i+1)*(x(i)+h*k3);
x(i+1) = x(i) + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

for i = 1:N
j = N + 2 - i;
k1 = u(j)*(exp(-r*t(j)) - k*lambda(j)) - exp(-r*t(j));
k2 = 0.5*(u(j)+u(j-1))*(exp(-r*(t(j)-h2)) - k*(lambda(j)-h2*k1)) - exp(-r*(t(j)-h2));
k3 = 0.5*(u(j)+u(j-1))*(exp(-r*(t(j)-h2)) - k*(lambda(j)-h2*k2)) - exp(-r*(t(j)-h2));
k4 = u(j-1)*(exp(-r*(t(j)-h)) - k*(lambda(j)-h*k3)) - exp(-r*(t(j)-h));
lambda(j-1) = lambda(j) - (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

for i=1:N+1
temp = x(i)*(k*lambda(i)-exp(-r*t(i)));
if(temp>0)
u1(i) = 1;
else
u1(i) = 0;
end
end
u = 0.5*(oldu + u1);

temp1 = delta*sum(abs(u)) - sum(abs(oldu - u));
temp2 = delta*sum(abs(x)) - sum(abs(oldx - x));
temp3 = delta*sum(abs(lambda)) - sum(abs(oldlambda - lambda));
test = min(temp1, min(temp2, temp3));
end

y(1,:) = t;
y(2,:) = x;
y(3,:) = u;
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
function y = code12(K,G,D,x0,z0,M,T)

test = -1;

delta = 0.001;
N = 1000;
t=linspace(0,T,N+1);
h=T/N;
h2 = h/2;

x=zeros(1,N+1);
z=zeros(1,N+1);
lambda=zeros(1,N+1);

x(1)=x0;
z(1)=z0;

u=zeros(1,N+1);

while(test < 0)

oldu = u;
oldx = x;
oldlambda = lambda;

for i = 1:N
k1 = G*u(i)*x(i) - D*x(i)^2;
k2 = G*(0.5*(u(i)+u(i+1)))*(x(i)+h2*k1) - D*(x(i)+h2*k1)^2;
k3 = G*(0.5*(u(i)+u(i+1)))*(x(i)+h2*k2) - D*(x(i)+h2*k2)^2;
k4 = G*u(i+1)*(x(i)+h*k3) - D*(x(i)+h*k3)^2;
x(i+1) = x(i) + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

for i = 1:N
j = N + 2 - i;
k1 = -lambda(j)*(G*u(j) - 2*D*x(j)) - K;
k2 = -(lambda(j)-h2*k1)*(G*0.5*(u(j)+u(j-1)) - 2*D*0.5*(x(j)+x(j-1))) - K;
k3 = -(lambda(j)-h2*k2)*(G*0.5*(u(j)+u(j-1)) - 2*D*0.5*(x(j)+x(j-1))) - K;
k4 = -(lambda(j)-h*k3)*(G*u(j-1) - 2*D*x(j-1)) - K;
lambda(j-1) = lambda(j) - (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

for i = 1:N+1
temp = G*x(i)*lambda(i) - 1;
if(temp>=0)
u1(i) = M;
else
u1(i) = 0;
end
end
u = 0.5*(oldu + u1);

temp1 = delta*sum(abs(u)) - sum(abs(oldu - u));
temp2 = delta*sum(abs(x)) - sum(abs(oldx - x));
temp3 = delta*sum(abs(lambda)) - sum(abs(oldlambda - lambda));
test = min(temp1, min(temp2, temp3));
end

for i=1:N
k1 = -K*z(i)*x(i);
k2 = -K*(z(i)+h2*k1)*0.5*(x(i)+x(i+1));
k3 = -K*(z(i)+h2*k2)*0.5*(x(i)+x(i+1));
k4 = -K*(z(i)+h*k3)*x(i+1);
z(i+1) = z(i) + (h/6)*(k1 + 2*k2 + 2*k3 + k4);
end

y(1,:) = t;
y(2,:) = x;
y(3,:) = z;
y(4,:) = u;
Loading

0 comments on commit 7f6a456

Please sign in to comment.