Skip to content
Permalink
Browse files

Serie 2

  • Loading branch information
steblyve committed Mar 4, 2020
1 parent d83a7d5 commit bea26a9858e87672086daca131dfd5e11a1f2233
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
@@ -0,0 +1,28 @@
format long g
x0 = 2;
f = @(x)log(x^2);
steps = 4;

% get real value
f_diff = Ableiten(f);
y_real = f_diff(x0);

h = 0.1;
[D1] = Extrapolation_h(h, x0, f, steps);

% print error and value
fprintf("h = 0.01\n");
for i=1:4
for x=1:5-i
fprintf("D%d%d %f.6 ",i-1,x-1, D1(i,x));
end
fprintf("\n");
end
fprintf("error\n");
for i=1:4
for x=1:5-i
fprintf("D%d%d %f.6 ",i-1,x-1, abs(D1(i,x)-y_real));
end
fprintf("\n");
end

@@ -0,0 +1,27 @@
format long g
x0 = 2;
f = @(x)log(x^2);
steps = 4;

% get real value
f_diff = Ableiten(f);
y_real = f_diff(x0);

h = 0.1;
[D1] = Extrapolation_h2(h, x0, f, steps);

% print error and value
fprintf("h = 0.01\n");
for i=1:4
for x=1:5-i
fprintf("D%d%d %f ",i-1,x-1, D1(i,x));
end
fprintf("\n");
end
fprintf("error\n");
for i=1:4
for x=1:5-i
fprintf("D%d%d %f ",i-1,x-1, abs(D1(i,x)-y_real));
end
fprintf("\n");
end
@@ -0,0 +1,17 @@
% testing functions.
f = @(x)1/x;
a = 2;
b = 4;
n = 100;
[T] = Trapez_sum(f, a, b, n);
[R] = Recht_sum(f, a, b, n);

m = 10;
v0 = 20;
Rf = @(v) -v*sqrt(v);
vn = 5;
f = @(x)m/Rf(x);

%Task a:
[R] = Recht_sum(f, v0, vn, 1000)
[T] = Trapez_sum(f, v0, vn, 1000)
@@ -0,0 +1,22 @@
function [D] = Extrapolation_h(h, x0, f, steps)
D = zeros(steps,steps);

%create Dx0 fields.
for i=1 : steps
h1 = h/(2^(i-1));
D(i,1) = D1f(f,x0,h1);
end

index = 2;
% create inner fields
% when starting with 4, then 3 2 and 1
for i = 1 : steps - 1
% z will be 1,2,3 when starting with 4 then 1,2 etc.
for z = 1 : steps-i
div = ((2^(index-1))-1);
D(z,index) = (2^(index-1) * D(z+1,index-1)-D(z,index-1)) / div;
end
index = index + 1;
end
end

@@ -0,0 +1,21 @@
function [D] = Extrapolation_h2(h, x0, f, steps)
D = zeros(steps,steps);

%create Dx0 fields.
for i=1 : steps
h1 = h/(2^(i-1));
D(i,1) = D1f(f,x0,h1);
end

index = 2;
% create inner fields
% when starting with 4, then 3 2 and 1
for i = 1 : steps - 1
% z will be 1,2,3 when starting with 4 then 1,2 etc.
for z = 1 : steps-i
div = ((4^(index-1))-1);
D(z,index) = (4^(index-1) * D(z+1,index-1)-D(z,index-1)) / div;
end
index = index + 1;
end
end
@@ -0,0 +1,9 @@
function [R] = Recht_sum(f, a, b, n)
h = (b-a)/n;
sum = 0;
for i=1:n
xi = a + (i-1)*h;
sum = sum + (f(xi) + (h/2));
end
R = h*(sum);
end
@@ -0,0 +1,11 @@
function [T] = Trapez_sum(f, a, b, n)
h = (b-a)/n;
s = (f(a) + f(b))/2;
sum = 0;
for i=1:n
xi = a + (i-1)*h;
sum = sum +f(xi);
end
T = h*(s+sum);
end

0 comments on commit bea26a9

Please sign in to comment.
You can’t perform that action at this time.