From eafae4f72fb5a880d83c70f94a005b2000ccab72 Mon Sep 17 00:00:00 2001 From: joshinils Date: Fri, 21 Jun 2019 06:07:47 +0200 Subject: [PATCH] foo tschebyschow --- pa2 | 2 +- uebungen/factorio/funcApprox/P.m | 9 ++++ uebungen/factorio/funcApprox/foo.m | 81 ++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 uebungen/factorio/funcApprox/P.m create mode 100644 uebungen/factorio/funcApprox/foo.m diff --git a/pa2 b/pa2 index 0b396b2..6d7b312 160000 --- a/pa2 +++ b/pa2 @@ -1 +1 @@ -Subproject commit 0b396b2e063b526425f8cfb38f774d129066fec1 +Subproject commit 6d7b312bc5883e81c5b554e18a79ba06788995e5 diff --git a/uebungen/factorio/funcApprox/P.m b/uebungen/factorio/funcApprox/P.m new file mode 100644 index 0000000..1e4f88f --- /dev/null +++ b/uebungen/factorio/funcApprox/P.m @@ -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 + diff --git a/uebungen/factorio/funcApprox/foo.m b/uebungen/factorio/funcApprox/foo.m new file mode 100644 index 0000000..73d159c --- /dev/null +++ b/uebungen/factorio/funcApprox/foo.m @@ -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) + + + + + + + + +