Skip to content

Commit b8d7c3c

Browse files
committed
Beetje tekenen, periospline functie is afgewerkt
1 parent 83cc39e commit b8d7c3c

File tree

8 files changed

+121
-23
lines changed

8 files changed

+121
-23
lines changed

Tante_Sidonia.gif

11.9 KB
Loading

excercise22.m

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
% Excercise 2.2
22
plotval = linspace(-pi, pi, 1000);
3-
x = linspace(-pi, pi, 50);
3+
x = linspace(-pi, pi, 20);
44
t = linspace(-pi, pi, 200);
55

66
func = @(x) sin(x) + sin(4.*x)./2;
@@ -9,11 +9,14 @@
99
f = func(x(1:end-1));
1010

1111
yEst = periospline(x,f,t);
12-
close;
12+
close all;
1313
plot(plotval, yplotval);
1414
hold on;
1515
plot(x(1:end-1), f, 'ro')
1616
plot(t, yEst, '-go');
17+
title('x=20')
18+
xlabel('x');
19+
ylabel('y')
1720
hold off;
1821

1922
x1 = linspace(-pi,pi ,10);
@@ -22,23 +25,29 @@
2225
figure;
2326
plot(t, abs(func(t)-yEst1));
2427
title('x=10');
28+
xlabel('x');
29+
ylabel('error')
2530

2631
x2 = linspace(-pi,pi ,50);
2732
f2 = func(x2(1:end-1));
2833
yEst2 = periospline(x2,f2,t);
2934
figure;
3035
plot(t, abs(func(t)-yEst2));
3136
title('x=50');
37+
xlabel('x');
38+
ylabel('error')
3239

33-
errMax = zeros(1,50);
40+
errMax = zeros(1,40);
3441
index = 1;
35-
for i=10:10:500
42+
for i=5:5:200
3643
x = linspace(-pi,pi ,i);
3744
f = func(x(1:end-1));
3845
yEst = periospline(x,f,t);
3946
errMax(index) = max(abs(func(t)-yEst));
4047
index = index + 1;
4148
end
4249
figure;
43-
plot(10:10:500, errMax);
44-
title('MaxErr');
50+
plot(5:5:200, errMax);
51+
title('MaxErr');
52+
xlabel('aantal of abscissen');
53+
ylabel('max fout');

excercise23.m

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
% Excercise 2.3
2+
[x,y] = click(); % Gives us x_1 to x_n-1 and y_1 to y_n-1
3+
%x_n = x_1 and y_n = y_1, this happens implicitly in
4+
%periospline
5+
6+
t = 1:1:(length(x)+1);
7+
evalX = linspace(1, length(x)+1, 1000);
8+
estimate = periospline(t,[x;y], evalX);
9+
plot(estimate(1,:), estimate(2,:));

excercise24.m

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
% Exercise 2.4
2+
3+
% Excercise 2.3
4+
open sidonia.mat;
5+
t = 1:1:(length(x)+1);
6+
evalX = linspace(1, length(x)+1, 1000);
7+
estimate = periospline(t,[x;y], evalX);
8+
plot(estimate(1,:), estimate(2,:));

periospline.m

Lines changed: 14 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
n = length(x)-1;
55
[d,~] = size(f);
66
N = length(t);
7-
y = zeros(d,N);
7+
y = zeros(N, d);
88
f = f';
99

1010
% Setup matrix & right hand side to determine s''(x_i)
@@ -20,12 +20,12 @@
2020
mainDiag(i-1) = 2.*((x(end) - x(end-1)) + (x(i) - x(i-1))); % 2*(x_n - x_n-1 + x_1 - x_0)
2121
downDiag(i-1) = x(i) - x(i-1); % x_1 - x_0
2222

23-
RHS(i-1,:) = ((f(i,:) - f(i-1,:))./(x(i) - x(i-1))) - ((f(i,:) - f(end,:))./(x(end)-x(end-1))); %f_n = f_0, f_n-1 = f(end)
23+
RHS(i-1,:) = ((f(i,:) - f(i-1,:))./(x(i) - x(i-1))) - ((f(i-1,:) - f(end,:))./(x(end)-x(end-1))); %f_n = f_0, f_n-1 = f(end)
2424

2525
elseif i == n+1
2626
% Other diags don't have values here
2727
mainDiag(i-1) = 2.*(x(i) - x(i-2)); % 2*(x_i-1 - x_i-2 + x_i - x_i-1)
28-
RHS(i-1,:) = ((f(1,:) - f(i-1,:))./(x(i) - x(i-1))) - ((f(i-1,:) - f(i-2,:))./(x(i-1)-x(i-2))); %f_n = f_0, f_n-1 = f(end)
28+
RHS(i-1,:) = ((f(1,:) - f(end,:))./(x(i) - x(i-1))) - ((f(end,:) - f(end-1,:))./(x(i-1)-x(i-2))); %f_n = f_0, f_n-1 = f(end)
2929

3030
else
3131
upDiag(i-1) = x(i) - x(i-1); %x_i - x_i-1
@@ -42,11 +42,12 @@
4242
% Add corner coeff to matrix
4343
Coef(1,end) = x(end) - x(end-1);
4444
Coef(end,1) = x(end) - x(end-1);
45+
RHS = 6.*RHS;
4546

4647
% Solve all at once. This gives us the s''(x_i) for each function set
4748
% This is a matrix where each row gives us the s''(x_i) for a function set
4849
% s''(x_0) to s''(x_n-1), s''(x_n) = s''(x_0)
49-
SCoef = (Coef\RHS)
50+
SCoef = (Coef\RHS);
5051

5152
% For each p_i(x) determine c_1i & c_2i and put in array
5253
% We use a matrix where each colomn for one function set => n x d
@@ -60,7 +61,6 @@
6061
end
6162
C2(i-1, :) = f(i-1, :)./(x(i) - x(i-1)) - (x(i) - x(i-1))./6.0.*SCoef(i-1, :);
6263
end
63-
6464
% For eval determine in which interval the point lies, and use the
6565
% according p_i(x) (valid in interval [x_i-1, x_i])
6666

@@ -69,23 +69,20 @@
6969
for i = 1:N
7070
x_i = 2; % The top of the interval t(i) is in
7171
funcNum = 1; % The number of the spline function
72-
if(t(i) > x(end)) || (t(i) < x(1))
73-
% Figure out how to get it back in de base region (its periodic
74-
% after all)
75-
continue;
76-
else
77-
while(x_i < n+1) && (~(t(i) < x(x_i)) && (t(i)>x(x_i-1)))
78-
x_i = x_i + 1;
79-
funcNum = funcNum + 1;
80-
end
81-
end
72+
while(x_i < n+1) && (~(t(i) < x(x_i)) && (t(i)>x(x_i-1)))
73+
x_i = x_i + 1;
74+
funcNum = funcNum + 1;
75+
end
8276
if x_i == n+1
83-
s_i = 2;
77+
s_i = 1;
8478
else
8579
s_i = x_i;
8680
end
8781

88-
y(:, i) = ((t(i) - x(x_i-1)).^3)./(6.0.*(x(x_i) - x(x_i-1))).*SCoef(s_i) - ((t(i) - x(x_i)).^3)./(6.0*(x(x_i) - x(x_i -1))).*SCoef(x_i-1) + C1(funcNum, :)'.*(t(i) - x(x_i-1)) + C2(funcNum, :)'.*(x(x_i) - t(i));
82+
y(i, :) = ((t(i) - x(x_i-1)).^3)./(6.0.*(x(x_i) - x(x_i-1))).*SCoef(s_i, :)...
83+
- ((t(i) - x(x_i)).^3)./(6.0.*(x(x_i) - x(x_i -1))).*SCoef(x_i-1, :)...
84+
+ C1(funcNum, :).*(t(i) - x(x_i-1)) + C2(funcNum, :).*(x(x_i) - t(i));
8985
end
86+
y = y';
9087
end
9188

sidonia.mat

1.76 KB
Binary file not shown.

test.m

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
plotval = linspace(-pi, pi, 1000);
2+
plotval2 = linspace(pi, 3*pi, 1000);
3+
x = linspace(-pi, pi, 10);
4+
x2 = linspace(pi, 3*pi, 10);
5+
t = linspace(-pi, pi, 2000);
6+
t2 = linspace(pi, 3*pi, 2000);
7+
8+
func = @(x) sin(x);
9+
10+
yplotval = func(plotval);
11+
yplotval2 = func(plotval2);
12+
f = func(x(1:end-1));
13+
f2 =func(x2(1:end-1));
14+
yEst = periospline(x,f,t);
15+
yEst2 = periospline(x2,f2,t2);
16+
close;
17+
plot(plotval, yplotval, 'b');
18+
hold on;
19+
plot(plotval2, yplotval2, 'b');
20+
plot(x(1:end-1), f, 'ro')
21+
plot(t, yEst, 'go');
22+
plot(t2, yEst2,'go');
23+
hold off;

testCases/perioSplineTest.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
classdef perioSplineTest < matlab.unittest.TestCase
2+
%PERIOSPLINETEST Summary of this class goes here
3+
% Detailed explanation goes here
4+
methods(TestMethodSetup)
5+
function addPath(testCase)
6+
addpath('../');
7+
end
8+
end
9+
10+
11+
12+
methods(Test)
13+
function testCoefMatrix(Test)
14+
x = [1 3 7 15 31];
15+
n = length(x)-1;
16+
upDiag = zeros(n-1,1);
17+
mainDiag = zeros(n,1);
18+
downDiag = zeros(n-1,1);
19+
20+
for i =2:n+1
21+
if i == 2
22+
% First one's special (used the last value)
23+
upDiag(i-1) = x(i) - x(i-1); % x_1 - x_0
24+
mainDiag(i-1) = 2.*((x(end) - x(end-1)) + (x(i) - x(i-1))); % 2*(x_n - x_n-1 + x_1 - x_0)
25+
downDiag(i-1) = x(i) - x(i-1); % x_1 - x_0
26+
27+
elseif i == n+1
28+
% Other diags don't have values here
29+
mainDiag(i-1) = 2.*(x(i) - x(i-2)); % 2*(x_i-1 - x_i-2 + x_i - x_i-1)
30+
else
31+
upDiag(i-1) = x(i) - x(i-1); %x_i - x_i-1
32+
mainDiag(i-1) = 2.*(x(i) - x(i-2)); % 2*(x_i-1 - x_i-2 + x_i - x_i-1)
33+
downDiag(i-1) = x(i) - x(i-1); % x_i - x_i-1
34+
35+
end
36+
37+
end
38+
Coef = full(gallery('tridiag', downDiag, mainDiag, upDiag));
39+
40+
% Add corner coeff to matrix
41+
Coef(1,end) = x(end) - x(end-1);
42+
Coef(end,1) = x(end) - x(end-1);
43+
44+
Test.verifyEqual(Coef, [36 2 0 16; 2 12 4 0; 0 04 24 8; 16 0 8 48]);
45+
end
46+
47+
function testRHSMatrix(Test)
48+
49+
end
50+
end
51+
end
52+

0 commit comments

Comments
 (0)