Advertisement
shamiul93

newton.m

May 6th, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 1.72 KB | None | 0 0
  1. X1=[-.5 -.4111 -.3222 -.2333 -.1444 -.0556 0.0333 .1222 .2111 .3 .3889 .4778 .5667 .6556 .7444 .8333 .9222 1.0111 1.1];
  2. Y1=[-1.8791161 -1.4863026 -1.2340791 -1.0889126 -1.0210957 -1.0012003 -.9997404 -.9872194 -.9341338 -.08109635 -.5884439 -.2398145 0.2504693 0.8644380 1.5092225 1.9638191 1.8573017 0.8153058 -.08237622];
  3. xr=[-0.4556 -0.2778 -0.1444 0.0333 0.2111 0.3889 0.6111 0.7889 0.9667 1.1444];
  4. yr=[-1.6626043 -1.1499897 -1.0210957 -.9997407 -.9341338 -.5884439 0.5454266 1.7803107 1.4644281 -1.0369071];
  5. plot(X1,Y1,'b*');
  6. number=19;
  7. for count=1:1:18
  8.     diff=round(18/count);
  9.     %disp(diff);
  10.     j=1;
  11.     % i=0;
  12.     x=zeros(1,19);
  13.     y=zeros(1,19);
  14.     for i=1:diff:19
  15.        
  16.        
  17.         x(1,j)= X1(1,i);
  18.         y(1,j)=Y1(1,i);
  19.         j=j+1;
  20.         %i=i+diff;
  21.        
  22.     end
  23.     if(x(1,j-1)~=X1(1,19))
  24.        
  25.         x(1,j)=X1(1,19);
  26.         y(1,j)=Y1(1,19);
  27.        
  28.        
  29.     else
  30.         j=j-1;
  31.     end
  32.     disp(x);
  33.     length=j;
  34.    
  35.     ar(1:length,1) = x(1,1:length);
  36.     ar(1:length,2) = y(1,1:length);
  37.    
  38.    
  39.     for a = 1:length-1
  40.         for b = 1:length-a
  41.             ar(b,a+2) = (ar(b+1,a+1)-ar(b,a+1))/(ar(b+a,1)-ar(b,1));
  42.         end
  43.     end
  44.    
  45.     xv(1,1:length)=x(1,1:length);
  46.     yv(1,1:length)=y(1,1:length);
  47.     mul = zeros(length-1,length);
  48.     p = conv(1,[1,-xv(1)]);
  49.    
  50.     for c = 1:length-1
  51.         mul(c,(length-c):length) = ar(1,c+2).*p;
  52.         p = conv(p,[1,-x(c+1)]);
  53.     end
  54.    
  55.     P = sum(mul,1);
  56.     P(1,length)=P(1,length)+y(1);
  57.    
  58.    
  59.     x_eval = -.5:0.1:1.1;
  60.     y_eval = polyval(P,x_eval);
  61.     plot(x_eval,y_eval);
  62.     hold on;
  63.     plot(xr,yr,'b*');
  64.     hold on;
  65.     pause(1);
  66.     hold off;
  67.    
  68.    
  69. end
  70.  
  71. clear ;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement