/
action.m
65 lines (51 loc) · 1.81 KB
/
action.m
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
function action
% 1. Interpolation Runge against by uniform points
% 2. Interpolation Runge against by Chebyshev's points
%
% 3. Draw plot for 1. and 2. against exact Runge-function (on at least 100 point)
% 4. Interpolate function y = 0.5x^2 + 8x^-1 + 8, a = -4, b = -1
% for n = {11,6}
% 5. Interpolate by Matlab
% 6. Compute errors vectors
% 7. Draw plots
% Runge plot
uniform_source = -1:2/100:1;
cheb_source = cheb(-1,1,100);
x = -1:2/100:1;
uniform_value = runge(uniform_source);
cheb_value = runge(cheb_source);
uniform_y = arrayfun(@(x)( ipol_lagr(uniform_source,uniform_value,x) ),x);
cheb_y = arrayfun(@(x)( ipol_lagr(cheb_source,cheb_value,x) ),x);
runge_y = runge(x);
subplot(3,1,1);
runge_plot = [uniform_y; cheb_y; runge_y];
H = plot(x, runge_plot);
legend('Uniform','Cheb','Exact');
% Task plot
x = -4:5/100:-1;
f = @(x)(0.5*x.^2 + 8*x.^-1 + 8);
source_n6 = -4:5/6:-1;
source_n11 = -4:5/11:-1;
value_n6 = f(source_n6);
value_n11 = f(source_n11);
y_n6 = arrayfun(@(x)( ipol_lagr(source_n6,value_n6,x) ),x);
y_n11 = arrayfun(@(x)( ipol_lagr(source_n11,value_n11,x) ),x);
a_n6 = polyfit(source_n6, value_n6, 6);
a_n11 = polyfit(source_n11, value_n11, 11);
y_n6m = arrayfun(@(x)( polyval(a_n6,x)), x);
y_n11m = arrayfun(@(x)( polyval(a_n11,x)), x);
y_exact = f(x);
y_plot = [y_n6; y_n11; y_n6m; y_n11m; y_exact];
subplot(3,1,2);
plot(x, y_plot);
legend('y_n6','y_n11','y_n6m','y_n11m','exact');
% Error plot
y_n6_e = abs(y_n6 - y_exact);
y_n11_e = abs(y_n11 - y_exact);
y_n6m_e = abs(y_n6m - y_exact);
y_n11m_e = abs(y_n11m - y_exact);
y_error = [y_n6_e; y_n11_e; y_n6m_e; y_n11m_e];
subplot(3,1,3);
plot(x, y_error);
legend('y_n6_error','y_n11_error','y_n6m_error','y_n11m_error');
end