-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpline_linear.m
More file actions
56 lines (54 loc) · 1.63 KB
/
Spline_linear.m
File metadata and controls
56 lines (54 loc) · 1.63 KB
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
clc;
clear;
% Taking the data inputs
x=input("Enter the x-coordinates of the data points as row vector : ");
f=input("Enter the y-coordinates of the data points as row vector : ");
N1=length(x);
N2=length(f);
p=0;
while (p==0)
if N1~=N2
fprintf("Please check the number of data points and re-enter.");
x=input("Enter the x-coordinates of the data points as row vector : ");
f=input("Enter the y-coordinates of the data points as row vector : ");
N1=length(x);
N2=length(f);
else
N=N1;
p=1;
fprintf("\n The data is given in a table as: \n\n ");
D=[' x ' ' f(x) '];
disp(D);
disp(cell2mat(compose('%14.8f', [x ; f]')));
end
end
a=zeros(1,N-1);
b=zeros(1,N-1);
for j=1:N-1
a(j)=f(j);
b(j)=(f(j+1)-f(j))/(x(j+1)-x(j));
end
k=1:N-1;
fprintf("\n The coefficients of a_j, b_j of the sub-spline S_j are given in a table as: \n\n ");
D=[' j ' ' a_j ' ' b_j '];
disp(D);
disp(cell2mat(compose('%14.8f', [k; a ; b]')));
% Finding values of Spline (which approximates the given data) at some point
w=input("\n Enter the point at which we want to find the value of the function : ");
i=1;
while i<= N-1
if (w-x(i)>=0) && (w-x(i+1)<=0)
k=i;
i=N;
end
i=i+1;
end
v=a(k)+b(k)*(w-x(k));
fprintf("\n The value of the Spline at %2.2f is : %14.8f \n\n", w, v);
% Later part is optional
% Printing the graph of linear Spline
grid on
plot(x,f, '-b*');
xlabel('x-axis (x-->)')
ylabel('y-axis (f(x)-->)')
legend('Linear spline', 'Location', 'northwest')