This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
x = [1 2 3 4]'; % transpose to make a column | |
y = [3 1 4 1]'; | |
n = length(x); % number of elements | |
M = zeros(n,n); % initialize a matrix n by n | |
for k=1:n | |
M(:,k) = x.^(k-1); % fill the matrix with powers of x-values | |
end | |
coeff = M\y; % find the coefficients | |
t = linspace(1,4); | |
poly = coeff(n)*ones(size(t)); % prepare for evaluation |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function plot_newton | |
f = @(x) 1./(1+x.^2); % function to interpolate | |
x = -5:5; % nodes of interpolation | |
y = f(x); | |
c = coeff(x,y); % calculate Newton polynomial coefficients | |
t = linspace(-5,5); | |
poly = newton(c,x,t); % evaluate the polynomial | |
plot(t,[f(t);poly]); % plot function and its polynomial | |
hold on | |
plot(x,y,'ro') % also mark the interpolated values |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function chebyshev_interpolation | |
a = -5; | |
b = 5; | |
t = linspace(a,b); | |
plot(t, [chebyshev(a,b,5,t); chebyshev(a,b,10,t); chebyshev(a,b,15,t); chebyshev(a,b,30,t)]); | |
hold on | |
plot(t, f(t), 'k--'); % original function in dashed black curve | |
legend('degree 5', 'degree 10', 'degree 15', 'degree 30', 'function') % describe the curves | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function adaptivePL | |
t = linspace(0, 7, 1000); % the default number of points, 100, may be not enough here | |
points = refine(t(1), t(end)); % determine the nodes of interpolation | |
plot(points, f(points), 'r-*') % piecewise linear plot with asterisks for data points | |
hold on | |
plot(t, f(t)) % the original function for comparison | |
end | |
function points = refine(x1,x2) | |
xm = (x1+x2)/2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function naturalcubic | |
a = 1995; | |
b = 2000; | |
y = [30 25 20 28 15 31]; | |
n = length(y); | |
h = (b-a)/(n-1); | |
rhs = (-6/h^2)*[0; diff(y',2); 0]; | |
A = diag(4*ones(1,n))+diag(ones(1,n-1),1)+diag(ones(1,n-1),-1); | |
A(1, 1:2) = [1 0]; | |
A(n, n-1:n) = [0 1]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function cubic | |
a = 1992; | |
b = 2000; | |
y = [26 25 20 23 28 21 15 20 18]; | |
n = length(y); | |
h = (b-a)/(n-1); | |
rhs = (-6/h^2)*[0; diff(y',2); 0]; | |
A = diag(4*ones(1,n))+diag(ones(1,n-1),1)+diag(ones(1,n-1),-1); | |
A(1, 1:2) = [1 0]; | |
A(n, n-1:n) = [1 0]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = @(x) 55*cos(x)-x; | |
a = 0; | |
b = 8; | |
fa = f(a); | |
fb = f(b); | |
if sign(fa) == sign(fb) | |
error('No bracket'); | |
end | |
while(b-a > 1e-9) | |
c = (a+b)/2; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
f = @(x,y) 100*(x.^2-y).^2 + (x-1).^2; % Rosenbrock's function | |
x = -2:0.1:2; | |
y = -2:0.1:2; | |
[X,Y] = meshgrid(x,y); | |
contour (x,y, f(X,Y), 30); | |
hold on | |
T = [-2 -1; 2 0; -1 1]; | |
for i = 1:3 | |
z(i) = f(T(i,1),T(i,2)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
c = document.querySelectorAll('.user-details a'); | |
a = []; | |
for (var i=0; i < c.length; i++) { | |
a.push(c[i].href.split('/')[4]); | |
} | |
console.log(a.join()); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
A = [0 1 1 0 1 1; 2 1 1 0 3 0; 0 0 0 4 0 2; 3 2 1 0 1 0; 0 0 1 1 0 3] | |
[U,D,V] = svd(A) | |
D2 = D.*(D>=5) | |
A2 = U*D2*V' | |
for j=1:6 disp(A2(1,j)/norm(A2(:,j))); end |
OlderNewer