Skip to content

Commit

Permalink
foo tschebyschow
Browse files Browse the repository at this point in the history
  • Loading branch information
joshinils committed Jun 21, 2019
1 parent 25c9f44 commit eafae4f
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pa2
Submodule pa2 updated from 0b396b to 6d7b31
9 changes: 9 additions & 0 deletions uebungen/factorio/funcApprox/P.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
function [ p ] = P( b, xk, x )
b = b(1, :);
n = length(b);
p = b(1, n);
for k = (n-1) : -1 : 1
p = b(k) + p * (x - xk(k));
end
end

81 changes: 81 additions & 0 deletions uebungen/factorio/funcApprox/foo.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
clear all;
close all;
format compact;
format shortg;

% intervall in which to approximate
limit_start = 1;
limit_end = 3e5;
base = (limit_end)^(1/100); % of the log, meaning logb(limit_end) := 100
logb = @(x) log(x)/log(base);

n = 10; % amount of nodes

% tschebyschow nodes:
k = linspace(1, n, n)';
x_i = sort( (limit_start + limit_end)/2 + (limit_end -limit_start)/2 * cos( (2*k -1)/(2*n)*pi ) );
f_i = logb(x_i);

%% berechne koeffizienten

%x_i = [1,2,3,4];
%f_i = [1,2,7,9];

f = zeros(length(f_i));
f(:,1) = f_i;
for s = 2:length(f_i)
for z = 1:(length(f_i)-s+1)
% fprintf('\nz%d, s%d ', z, s);
f(z,s) = (f(z+1,s-1) - f(z,s-1) ) / ( x_i(z+s-1) - x_i(z) );
% x_i(z)
% x_i(z+s-1)
% x_i(z+s-1) - x_i(z)
end
% fprintf('\n');
end

%% Plot P and points

subplot(2,1,1);
plot(x_i, f_i, 'ob', 'HandleVisibility', 'off');
hold on;
xP = logspace(log10(1),log10(limit_end), 1e4);
yP = zeros(length(xP),1);
for fehlerIndex=1:length(xP)
yP(fehlerIndex) = P(f, x_i, xP(fehlerIndex));
end
plot(xP, yP, 'b-', 'DisplayName', 'P(f)(x)');
hold on;
%f_interp = interp1(x_i, f_i, xP)';
%plot(xP, f_interp, '.r-');

%%original function
funcX = logspace(log10(min(xP)), log10(max(xP)), length(xP));
funcY = logb(funcX)';
plot(funcX, funcY, 'g-', 'DisplayName', 'f(x) = logb');

legend('Location','SouthEast');
xlabel('x');
ylabel('y');


%% plot error

subplot(2,1,2);
err = abs(funcY - yP);
plot(xP, err, '.r-');
xlabel('x');
ylabel('fehler');
legend('|f-P(f)| = error');

[max_fehler, fehlerIndex] = max(err)
worstApproxX = xP(fehlerIndex)









0 comments on commit eafae4f

Please sign in to comment.