summaryrefslogtreecommitdiff
path: root/260/DEPENDENCIES/cubicspline.sci
diff options
context:
space:
mode:
authorprashantsinalkar2017-10-10 12:27:19 +0530
committerprashantsinalkar2017-10-10 12:27:19 +0530
commit7f60ea012dd2524dae921a2a35adbf7ef21f2bb6 (patch)
treedbb9e3ddb5fc829e7c5c7e6be99b2c4ba356132c /260/DEPENDENCIES/cubicspline.sci
parentb1f5c3f8d6671b4331cef1dcebdf63b7a43a3a2b (diff)
downloadScilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.gz
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.tar.bz2
Scilab-TBC-Uploads-7f60ea012dd2524dae921a2a35adbf7ef21f2bb6.zip
initial commit / add all books
Diffstat (limited to '260/DEPENDENCIES/cubicspline.sci')
-rw-r--r--260/DEPENDENCIES/cubicspline.sci50
1 files changed, 50 insertions, 0 deletions
diff --git a/260/DEPENDENCIES/cubicspline.sci b/260/DEPENDENCIES/cubicspline.sci
new file mode 100644
index 000000000..178a716eb
--- /dev/null
+++ b/260/DEPENDENCIES/cubicspline.sci
@@ -0,0 +1,50 @@
+function cubicspline(X,Y)
+ n = length(X);
+ //c(1) = 0;
+ //a = zeros(n-1,n-1);
+ for(i = 2:n-1)
+ c(i-1) = (6/(X(i+1)-X(i-1)))*((Y(i+1)-Y(i))/(X(i+1)-X(i)) - (Y(i)-Y(i-1))/ (X(i)-X(i-1)));
+ end
+ for(i = 2:n-1)
+ a(i-1,i-1) = (X(i)-X(i-1))/(X(i+1)-X(i-1));
+ a(i-1,i) = 2;
+ a(i-1,i+1) = (X(i+1)-X(i))/(X(i+1)-X(i-1));
+
+ end
+[m,n] = size(a);
+
+b = a(:,2:n-1); //For the case of natural splines, double derivative is zero at the first and last of data points. So removing the first and last columns since these are the only non-zero terms in the respective columns.
+
+//[r2 c2] = size(b);
+
+//disp(c)
+//disp(b)
+x = inv(b)*c;
+
+//disp(x)
+x1(1,1) = 0;
+x1(n,1) = 0;
+x1(2:n-1,1) = x(:,1);
+
+printf('The values of second derivatives at the data points are :\n')
+disp(x1)
+
+x = poly(0,'x');
+
+for(i = 2:n)
+ f(i-1) = [Y(i-1)*(X(i)-x)/(X(i)-X(i-1)) + Y(i)*(x-X(i-1))/(X(i)-X(i-1))] + x1(i-1)/6*[(X(i)-x)^3/(X(i)-X(i-1))-(X(i)-X(i-1))*(X(i)-x)] + x1(i)/6*[(x-X(i-1))^3/(X(i)-X(i-1))-(X(i)-X(i-1))*(x-X(i-1))];
+end
+
+printf('\nThe expressions for the cubic splines are \n')
+disp(f)
+
+for(i = 1:n-1)
+ p = X(i):0.01:X(i+1);
+ q = horner(f(i),p);
+ plot(p,q)
+end
+plot(X,Y,'ks')
+xlabel('x')
+ylabel('y')
+
+endfunction \ No newline at end of file