-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBVP 1.sce
125 lines (86 loc) · 1.68 KB
/
BVP 1.sce
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
//TO SOLVE GIVEN ODE 2ND ORDER BOUNDARY VALUE PROBLEM AND PLOT THE RESULTS
/* Given Boundary Value Problem
y'' = p(x)y'+q(x)y+r(x)
y(a)=ya
y(b)=yb
*/
///BY VISHU SAINI
clc()
clear
//1. DEFINE BVP
//a.ode
function [p,q,r]=ode(x)
p=1
q=0 //Defining the coeffecients of ode
r=30*(x*x*x*x) - 20*(x*x*x) + 12*(x*x) -4*x
endfunction
//b. boundary conditions
a=0
b=2
ya=0
yb=40
//2.DISCRETIZE SPACE
N= input("enter number of points")
h=(b-a)/(N-1) //step-size
//a.initialize
for i=1:N
X(i)=0
end
//b.store nodal points as an array
for i=1:N
X(i)= a + (i-1)*h
end
disp(X)
//3.FDM APPROXIMATION
////for i=1:N
//
// ys=(Y(i+1,1)+Y(i-1,1) -2*Y(i,1))/(h*h)
// yf=(Y(i+1,1)-Y(i-1,1))/2*h
//
// y=Y(i,1)
// x=X(i,1)
//
//end
//4.MATRIX FORMULATION
//a. constants of the matrix
for i=1:N
[p,q,r]=ode(X(i,1))
A(i,1)= 2+(h*p)
B(i,1)= -4-2*q*h*h
C(i,1)= 2-h*p
D(i,1)= 2*h*h*r
end
//disp(A)
//disp(B)
//disp(C)
//disp(D)
//b. The Matrix
//initialize n x n zero matrix
for i=1:N
for j = 1:N
H(i,j)=0
end
end
for i=2:N-1
H(i,i)=B(i,1)
H(i,i-1)=A(i,1)
H(i,i+1)=C(i,1)
end
H(1,1)=1
H(N,N)=1
//c.coefficient matrix
for i=1:N
J(i,1)= D(i,1)
end
J(1,1)=ya
J(N,1)=yb
disp(J)
disp(H)
//5.SOLVE THE N LINEAR EQAUTIONS
S=inv(H)*J
//6.PLOT THE RESULTS
//a.plot solution
plot(X,S)
xlabel("x","fontsize",3,"color","red") //x and y label
ylabel("y(x)","fontsize",3,"color","red")
gca().grid=[1 1 1] //turn on grid