-
Notifications
You must be signed in to change notification settings - Fork 0
/
Finite_Difference_Methods_for_Non_Linear_BVPs.m
139 lines (112 loc) · 3.01 KB
/
Finite_Difference_Methods_for_Non_Linear_BVPs.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
clear;clc
%Here we supposed that y_prime is 'z'.
g=input('Enter Function f(x,y,z)= ','s');
g=str2sym(g);
syms f(x,y,z)
f(x,y,z)=g;
fy=diff(f,y);
dfy=diff(fy,x);
%Converting 'f','fy' and 'dfy' to function handle.
%Because it takes less time in calculations
f=matlabFunction(f);
fy=matlabFunction(fy);
dfy=matlabFunction(dfy);
EndPoints=input('Input Endpoints as [a,b] = ');
aa=EndPoints(1); bb=EndPoints(2);
alpha=input('Input Coundary Condition 1: alpha= ');
beta=input('Input Coundary Condition 1: beta= ');
h=input('Input step-size: h= ');
tol=input('Input Tolerance: tol= ');
iterlimit=100;
n=(bb-aa)/h-1;
a = zeros(1,n);
b = zeros(1,n);
c = zeros(1,n);
d = zeros(1,n);
l = zeros(1,n);
u = zeros(1,n);
z = zeros(1,n);
v = zeros(1,n);
w = zeros(1,n);
ya_matrix=zeros(1,n+2);
for i=1:n
w(i)=alpha+i*((beta-alpha)/(bb-aa))*h;
end
iter=0;
while iter<iterlimit
x=aa+h;
t=(w(2)-alpha)/(2*h);
a(1) =2+h^2*fy(x,w(1),t);
b(1) =-1+0.5*h*dfy(x,w(1),t);
d(1) =-(2*w(1)-w(2)-alpha+h^2*f(x,w(1),t));
for i=2:n-1
x=aa+i*h;
t=(w(i+1)-w(i-1))/(2*h);
a(i) =2+h^2*fy(x,w(i),t);
b(i) =-1+0.5*h*dfy(x,w(i),t);
c(i) =-1-0.5*h*dfy(x,w(i),t);
d(i) =-(2*w(i)-w(i+1)-w(i-1)+h^2*f(x,w(i),t));
end
x=bb-h;
t=(beta-w(n-1))/(2*h);
a(n) =2+h^2*fy(x,w(n),t);
c(n) =-1-0.5*h*dfy(x,w(n),t);
d(n) =-(2*w(n)-w(n-1)-beta+h^2*f(x,w(n),t));
%Step 8
l(1) = a(1);
u(1) = b(1)/a(1);
z(1) = d(1)/l(1);
%Step 9
for i = 2 : n-1
l(i) = a(i)-c(i)*u(i-1);
u(i) = b(i)/l(i);
z(i) = (d(i)-c(i)*z(i-1))/l(i);
end
%Step 10
l(n) = a(n)-c(n)*u(n-1);
z(n) = (d(n)-c(n)*z(n-1))/l(n);
w_old=w;
%Step 11
v(n)=z(n);
w(n)=w(n)+v(n);
%Step 12
for i =n-1:-1:1
v(i)=z(i)-u(i)*v(i+1);
w(i) = w(i)+v(i);
end
if max(abs(w-w_old))<=tol
for i=0:n+1
x=aa+i*h;
end
fprintf('\n\nConvergence after Number of iterations= %d \n',iter)
break;
end
iter=iter+1;
end
if iter>iterlimit
disp('/n/nDid''nt converge to the specified tolerance')
fprintf('Number of iterations=%d \n',iter)
end
w=[alpha,w,beta];
fprintf('%4.8f \n',w)
%For comparing with actual solution
disp('Wanna compare result to actual values?')
ask=input('Reply yes or no..... ','s');
if strcmpi(ask,'yes')
fa=input('Enter Actual Function f(x)= ','s');
fa=str2func(['@(x)',fa]);
obs=0;
fprintf('\nobs. \t x(i) \t w(approx) \t w(Actual) \t\t Error \n');
for t=aa:h:bb
ya=fa(t);
obs=obs+1;
xi(1,obs)=t;
ya_matrix(1,obs)=ya;
end
Error=abs(w-ya_matrix);
fprintf('%3d \t %3.3f \t %3.8f \t %3.8f \t %1.4e \n',[(1:n+2);xi;w;ya_matrix;Error]);
elseif strcmpi(ask,'no')
disp('Best of luck then........')
else
disp('The keyword you entered is not correct.Run program again...')
end