# Paulche/num_analysis_labs

Lab6 has passed

1 parent ff40a49 commit 3d13943f789dea38720987b6c6d9f2e04c4e4f77 committed Mar 25, 2013
Showing with 76 additions and 8 deletions.
1. +65 −0 lab6/action.asv
2. +11 −8 lab6/action.m
 @@ -0,0 +1,65 @@ +function action + % 1. Interpolation Runge against by uniform points + % 2. Interpolation Runge against by Chebyshev's points + % + % 3. Draw plot for 1. and 2. against exact Runge-function (on at least 100 point) + % 4. Interpolate function y = 0.5x^2 + 8x^-1 + 8, a = -4, b = -1 + % for n = {11,6} + % 5. Interpolate by Matlab + % 6. Compute errors vectors + % 7. Draw plots + + % Runge plot + uniform_source = -1:2/100:1; + cheb_source = cheb(-1,1,100); + x = -1:2/100:1; + + uniform_value = runge(uniform_source); + cheb_value = runge(cheb_source); + + uniform_y = arrayfun(@(x)( ipol_lagr(uniform_source,uniform_value,x) ),x); + cheb_y = arrayfun(@(x)( ipol_lagr(cheb_source,cheb_value,x) ),x); + runge_y = runge(x); + + subplot(3,1,1); + runge_plot = [uniform_y; cheb_y; runge_y]; + H = plot(x, runge_plot); + legend('Uniform','Cheb','Exact'); + + % Task plot + x = -4:5/100:-1; + f = @(x)(0.5*x.^2 + 8*x.^-1 + 8); + source_n6 = -4:5/6:-1; + source_n11 = -4:5/11:-1; + + value_n6 = f(source_n6); + value_n11 = f(source_n11); + + y_n6 = arrayfun(@(x)( ipol_lagr(source_n6,value_n6,x) ),x); + y_n11 = arrayfun(@(x)( ipol_lagr(source_n11,value_n11,x) ),x); + + a_n6 = polyfit(source_n6, value_n6, 6); + a_n11 = polyfit(source_n11, value_n11, 11); + + y_n6m = arrayfun(@(x)( polyval(a_n6,x)), x); + y_n11m = arrayfun(@(x)( polyval(a_n11,x)), x); + + y_exact = f(x); + y_plot = [y_n6; y_n11; y_n6m; y_n11m; y_exact]; + + subplot(3,1,2); + plot(x, y_plot); + legend('y_n6','y_n11','y_n6m','y_n11m','exact'); + + % Error plot + y_n6_e = abs(y_n6 - y_exact); + y_n11_e = abs(y_n11 - y_exact); + y_n6m_e = abs(y_n6m - y_exact); + y_n11m_e = abs(y_n11m - y_exact); + + y_error = [y_n6_e; y_n11_e; y_n6m_e; y_n11m_e] + + subplot(3,1,3); + plot(x, y_error); + legend('y_n6_error','y_n11_error'); +end
 @@ -10,8 +10,8 @@ % 7. Draw plots % Runge plot - uniform_source = -1:2/10:1; - cheb_source = cheb(-1,1,11); + uniform_source = -1:2/100:1; + cheb_source = cheb(-1,1,100); x = -1:2/100:1; uniform_value = runge(uniform_source); @@ -44,19 +44,22 @@ y_n6m = arrayfun(@(x)( polyval(a_n6,x)), x); y_n11m = arrayfun(@(x)( polyval(a_n11,x)), x); - y_plot = [y_n6; y_n11; y_n6m; y_n11m]; + y_exact = f(x); + y_plot = [y_n6; y_n11; y_n6m; y_n11m; y_exact]; subplot(3,1,2); plot(x, y_plot); - legend('y_n6','y_n11','y_n6m','y_n11m'); + legend('y_n6','y_n11','y_n6m','y_n11m','exact'); % Error plot - y_n6_e = abs(y_n6 - y_n6m) - y_n11_e = abs(y_n11 - y_n11m) + y_n6_e = abs(y_n6 - y_exact); + y_n11_e = abs(y_n11 - y_exact); + y_n6m_e = abs(y_n6m - y_exact); + y_n11m_e = abs(y_n11m - y_exact); - y_error = [y_n6_e; y_n11_e] + y_error = [y_n6_e; y_n11_e; y_n6m_e; y_n11m_e]; subplot(3,1,3); plot(x, y_error); - legend('y_n6_error','y_n11_error'); + legend('y_n6_error','y_n11_error','y_n6m_error','y_n11m_error'); end