Advertisement
Guest User

Untitled

a guest
Oct 19th, 2017
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.99 KB | None | 0 0
  1. function [ ] = chmethodslagranga (a,b,n)
  2. syms x;
  3. %F(x)=3*x-cos(x)-1;
  4. ezplot('3*x-cos(x)-1'),hold on
  5. axis([0 5 -0.1 0.1])
  6. xx=linspace(a,b,n+1);
  7. yy=zeros(1,n+1);
  8. res=0;
  9. x1=xx(1);
  10. x2=xx(1);
  11. for i=1:n+1
  12. yy(i)=3*xx(i)-cos(xx(i))-1;
  13. end
  14. y1=lagrange (xx,yy,n,xx(1));
  15. y2=y1;
  16. for l=1:n+1
  17. res=lagrange(xx,yy,n,xx(l));
  18. s=3*xx(l)-cos(xx(l))-1;
  19. x2=xx(l);
  20. y2=res;
  21. % рисую график полинома
  22. plot([x1,x2],[y1,y2],'g');
  23. disp([s res s-res xx(l)]);
  24. x1=x2;
  25. y1=y2;
  26. end
  27. ry1=3*1-cos(1)-1-lagrange(xx,yy,n,1);
  28. ry2=ry1;
  29. rx1=1;
  30. rx2=1;
  31. for i=1:0.001:10
  32. res=lagrange(xx,yy,n,i);
  33. s=3*i-cos(i)-1;
  34. rx2=i;
  35. ry2=s-res;
  36. if ry1~=ry2
  37. % рисую график разницы
  38. plot([rx1,rx2],[ry1,ry2],'r');
  39. rx1=rx2;
  40. ry1=ry2;
  41. end
  42. end
  43. end
  44.  
  45. function [res] =lagrange (xx,yy,n,t)
  46. res=0;
  47. for i=1:n+1
  48. pn=1;
  49. for j=1:n+1
  50. if i~=j
  51. pn=pn*(t-xx(j))/(xx(i)-xx(j));
  52. end
  53. end
  54. res=res+pn*yy(i);
  55. end
  56. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement