-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRungeKutta.m
57 lines (49 loc) · 1.26 KB
/
RungeKutta.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
clc
clear all
close all
% Set the Runge function
f = @(x) 1./(1 + x.^2);
% Set the point to build the plot of the Rung function
xx = linspace(-5, 5, 1000);
fxx = f(xx);
% Plot the Runge function
figure
subplot(1,2,1)
hold on, box on
plot(xx, fxx, 'k-', 'LineWidth',2)
axis([-5.1 5.1 -0.4 1.2])
set(gca,'FontSize',16)
set(gca,'LineWidth',1.5)
xlabel('x','FontSize',16)
ylabel('f(x)','FontSize',16)
% Plot the Linear interpolant polynomial
for k = [2:2:10]
x = linspace(-5, 5, k);
fx = f(x);
coef = polyfit(x, fx, k-1);
yy = polyval(coef, xx);
plot(xx, yy, 'r-', 'LineWidth',2)
norm((fxx-yy),'inf')
pause
end
% Plot the Runge function
subplot(1,2,2)%impila grafici
hold on, box on%aggiungi plot a figura esistente creando un riquadro
plot(xx, fxx, 'k-', 'LineWidth',2)
axis([-5.1 5.1 -1.2 2.4])
set(gca,'FontSize',16)
set(gca,'LineWidth',1.5)
xlabel('x','FontSize',16)
ylabel('f(x)','FontSize',16)
% Plot the Linear interpolant polynomial
for k = [12:2:16]
x = linspace(-5, 5, k);
fx = f(x);
coef = polyfit(x, fx, k-1);
yy = polyval(coef, xx);
plot(xx, yy, 'r-', 'LineWidth',2)
norm((fxx-yy),'inf')
pause
end
% The interpolation polynomials approach the function in the middle
% of the interval, but close to the boundaries increasing oscillations appear.