-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
Submodule pa2
updated
from 0b396b to 6d7b31
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
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 | ||
|
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
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) | ||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|
||
|