From: Nils Forssén Date: Tue, 27 Sep 2022 16:11:36 +0000 (+0200) Subject: push X-Git-Url: https://gitweb.forssennils.se/?a=commitdiff_plain;h=4b1e72914f7a9eef306763d27f5d2ca42860b8ce;p=TANA21.git push --- diff --git a/Lagrange_interpolation.m b/Lagrange_interpolation.m index e53a219..34b6f05 100644 --- a/Lagrange_interpolation.m +++ b/Lagrange_interpolation.m @@ -1,4 +1,4 @@ -function [pnx] = Lagrange_interpolation(z,) +function [pnx] = Lagrange_interpolation(z,x,y) %% % Implementation to evaluate an interpolating polynomial p_n(x) % at the point x = z. The polynomial uses the standard Lagrange @@ -7,8 +7,8 @@ function [pnx] = Lagrange_interpolation(z,) % INPUT: % % z - 1x1 value to evaluate -% ??? -% +% x - nx1 vector with x-values +% y - nx1 vector with y-valuse % OUTPUT: % % pnx - value of the polynomial interpolant at x = z @@ -16,17 +16,17 @@ function [pnx] = Lagrange_interpolation(z,) %% % compute the polynomial interpolation sum evaluated at x = z - pnx = - for i = - - - - for j = - - - + pnx = 0; + n = length(x); + l = ones(n,1); + for i=1:n + + for j=1:n + if i ~= j + l(i) = l(i)*(z - x(j))/(x(i) - x(j)); + end end - - pnx = + l(i) + pnx = pnx + (y(i) * l(i)); end end diff --git a/build_interpolation.m b/build_interpolation.m new file mode 100644 index 0000000..7f2e318 --- /dev/null +++ b/build_interpolation.m @@ -0,0 +1,9 @@ +function [X,L] = build_interpolation(M, xVals, yVals) +%UNTITLED Summary of this function goes here +% Detailed explanation goes here + X = linspace(xVals(1), xVals(end), M); + L = zeros(length(M), 1); + for k=1:M + L(k) = Lagrange_interpolation(X(k), xVals, yVals); + end +end \ No newline at end of file diff --git a/command_windwo todat.mat b/command_windwo todat.mat new file mode 100644 index 0000000..b423bd1 Binary files /dev/null and b/command_windwo todat.mat differ diff --git a/diff.m b/diff.m index 64e8bf7..ba02172 100644 --- a/diff.m +++ b/diff.m @@ -57,7 +57,7 @@ bound_up = ((f_x_3(4)) - (4*f_x_3(5)) + (3*f_x_3(6)))/(2*h) %% EXP -N = 100; +N = 1000; D_x_center = diag(0.5*ones(N,1),1) - diag(0.5*ones(N,1),-1); D_x_center(1,1) = 1; @@ -72,16 +72,24 @@ h = 1/N; f = zeros(length(interval), 1); f_prim = zeros(length(interval), 1); for i=1:length(interval) - f(i) = 4*exp(sin(4*interval(i))); + f(i) = exp(sin(4*interval(i))); f_prim(i) = 4*exp(sin(4*interval(i)))*cos(4*interval(i)); end -D_x_center -interval -f -approx = (1/h) * D_x_center * f -f_prim +approx_mid = (1/h) * D_x_center * f; +%approx_edge = (1/h) * D_x_forward * f; +%approx_mid(1) = approx_edge(1); +%approx_mid(end) = approx_edge(end); + +bound_low = ((-3*f(1)) + (4*f(2)) - (f(3)))/(2*h); +bound_up = ((f(N-2)) - (4*f(N-1)) + (3*f(N)))/(2*h); + +approx_mid(1) = bound_low; +approx_mid(end) = bound_up; + +max(abs(f_prim - approx_mid)) + diff --git a/scripting.m b/scripting.m new file mode 100644 index 0000000..0897810 --- /dev/null +++ b/scripting.m @@ -0,0 +1,21 @@ +xVals = [1, 2, 3, 4, 5]; +yVals = [1, 1, 2, 6, 24]; +[X, L] = build_interpolation(150, xVals, yVals); +%plot(X, gamma(X), '-k' , X, L, '--r', xVals, yVals, 'rd', 'MarkerFaceColor', 'r', 'MarkerSize', 7) + +p = Lagrange_interpolation(6, xVals, yVals) +q = gamma(6) +sqrt(pi)/2 + +%% + +xVals = x_joukow; +yVals = y_joukow; +[X, L] = build_interpolation(150, xVals, yVals); + +M = linspace(-1.775, 1.695, 150); +s = spline(x_joukow, y_joukow, M); + + +plot(X, L, '--r', xVals, yVals, 'rd', 'MarkerFaceColor', 'r', 'MarkerSize', 7) +plot(M, s, '-.g', 'LineWidth', 2); \ No newline at end of file