Advertisement
Guest User

Untitled

a guest
Apr 27th, 2017
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 0.83 KB | None | 0 0
  1. a=0.3;
  2.  
  3. b=8.7;
  4.  
  5. n=10;
  6.  
  7. pol=2;
  8.  
  9. h=(b-a)/10;
  10.  
  11. x1=h*(b+a);
  12.  
  13. x2=0.3*(b-a)+2*h;
  14.  
  15. f=@(x) (sin(x)+0.5*cos(2*x)+0.5*x);
  16.  
  17. x = zeros(1,n);
  18.  
  19. y = zeros(1,n);
  20.  
  21. for i=1:n
  22.  
  23. x(i)=a+i*h;
  24.  
  25. y(i)=f(a+i*h);
  26.  
  27. end;
  28.  
  29. for pol_st=1:5
  30.  
  31. xx=linspace(min(x),max(x),pol);
  32.  
  33. pol=pol+1;
  34.  
  35. yy=0;
  36.  
  37. for i=1:n
  38.  
  39. P=1;
  40.  
  41. for k=1:n
  42.  
  43. if(i~=k)
  44.  
  45. P=P.*(xx-x(k))./(x(i)-x(k));
  46.  
  47. %.* - поэлементное умножение
  48.  
  49. %./ - поэлементное деление
  50.  
  51. end
  52.  
  53. end
  54.  
  55. yy=yy+y(i)*P;
  56.  
  57. end
  58.  
  59. plot(xx,yy);
  60.  
  61. hold on;
  62.  
  63. end
  64.  
  65. fplot( '[sin(x)+0.5*cos(2*x)+0.5*x]',[0.3 8.7],0.0001)
  66.  
  67. plot(x,y,'*r');
  68.  
  69. grid on
  70.  
  71. plot(xx,yy,'bo');
  72.  
  73. %legend('sin(5*x)/x','{\itL_n}({\itx})','nodes',-1)
  74.  
  75. legend('pol=1','pol=2','pol=3','pol=4','pol=5','sin(x)+0.5*cos(2*x)+0.5*x','nodes:x,y' ,'nodes:xx,yy');
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement