## Attachment 'sheet10.m'

Download```
1 function sheet10
2
3 % generate some data
4 N = 50;
5 X = 2 * pi * rand(N, 1);
6 Y = fct1(X) + randn(N, 1);
7
8 % plot the data
9 hold off;
10 plot(X, Y, '.');
11 grid;
12
13 % plot the linear fit without centering
14 hold on;
15 W = lsr(X, Y)
16 plot_fit(X, Y, W, 0, 'r-');
17
18 % ... and with centering
19 [W, B] = lsr_with_centering(X, Y);
20 plot_fit(X, Y, W, B, 'b-');
21
22 % plot a polynomial fit for 4 different regularization constants
23 for lambda = [1, 10, 100, 1000]
24 W = lsr_poly(4, lambda, X, Y);
25 plot_poly_fit(4, W, X, Y, 'k-');
26 end
27
28 hold off;
29
30 legend('data', 'without centering', 'with centering', 'poly')
31
32 function Y = fct1(X)
33 Y = 0.2 * (X - pi - 1) .* (X - pi - 2.5) .* (X - pi + 1) .* X - 3;
34
35 %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
36 % fill in your solutions below
37
38 % 1. Center the data
39 function X = center(X)
40 % ...
41
42 % 2. Compute the least-squares fit from X and Y
43 function W = lsr(X, Y)
44 % ...
45
46 % 3. Compute the centered version. Compute W and B accordingly.
47 function [W, B] = lsr_with_centering(X, Y)
48 XM = mean(X);
49 YM = mean(Y);
50 X = center(X);
51 Y = center(Y);
52
53 % ...
54
55 function plot_fit(X, Y, W, B, style)
56 X = sort(X);
57 Yh = W'*X + B;
58 plot(X, Yh, style);
59
60 % 4. Compute the least-squares fit using the function
61 % poly_design_matrix.
62 function W = lsr_poly(K, lambda, X, Y)
63 % ...
64
65 % 5. Compute the design matrix for polynomials from degree 0 to K
66 function PSI = poly_design_matrix(K, X)
67 % ...
68
69 % 6. Compute the prediction using for polynomials from degree 0 to K
70 function W = plot_poly_fit(K, W, X, Y, style)
71 X = sort(X);
72 % ...
73 plot(X, Yh, style);
```

## Attached Files

To refer to attachments on a page, use**, as shown below in the list of files. Do**

`attachment:filename`**NOT**use the URL of the

`[get]`link, since this is subject to change and can break easily.

You are not allowed to attach a file to this page.