Advertisement
Guest User

Untitled

a guest
May 21st, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.03 KB | None | 0 0
  1. approx_func_plot(5,3);
  2. approx_func_plot(5,5);
  3. approx_func_plot(5,10);
  4. approx_func_plot(5,20);
  5.  
  6. function [Fi] = approx_func_plot(n,k)
  7. Fi = zeros(n,k);
  8. f = @(x) sqrt(1-x^2)*exp(x);
  9.    for i=1:n
  10.        x(i) = -1 + 2*(i-1)/(n-1);
  11.        y(i) = f(x(i));
  12.    end
  13.    
  14.    for i=1:k
  15.        xk(i) = -1 + 2*(i-1)/(k-1);
  16.    end
  17.  
  18.  
  19.    for j=1:k
  20.        for i=1:n
  21.            Fi(i,j) = sinc(x(i) - xk(j));
  22.        end
  23.    end
  24.    
  25.    
  26.    y = y';
  27.    A = Fi'*Fi;
  28.    b = Fi'*y;
  29.    
  30.    p = A\b;
  31.    
  32.    x = linspace(-1,1,100);
  33.    for i=1:100
  34.        y_100(i) = f(x(i));
  35.    end
  36.    
  37.    tmp2 = 0;
  38.    
  39.    for i=1:100                 % CREATING APPROX FUNCTION OUTOUT
  40.        for j=1:k
  41.            tmp = p(j)*sinc(x(i)-xk(j));
  42.            tmp2 = tmp2 + tmp;
  43.        end
  44.        fapp(i) = tmp2;
  45.        tmp2 = 0;
  46.    end
  47.    
  48.    f4 = figure;
  49.    figure(f4);
  50.    plot(x,fapp, x, y_100);
  51.    legend('approximated', 'real function');
  52.    title(['Approximated function for n= ' num2str(n) ' k= ' num2str(k)]);
  53.    xlabel('x');
  54.    ylabel('y');
  55. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement