Advertisement
Hamikadze

Untitled

Oct 10th, 2019
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.77 KB | None | 0 0
  1. function [xmin, fmin, count] = mullersearch(f, df,searchpos,tol)
  2.  
  3. %figure(3);
  4. count = 0;
  5. x0 = searchpos(1);
  6. x2 = searchpos(2);
  7. x1 = (x2-x0)/2;
  8. x3 = 0;
  9.  
  10. ea = realmax;
  11. left = x0;
  12. right = x2;
  13. verbose = true;
  14.  
  15. a = 0;
  16. b = 0;
  17. c = 0;
  18. figure(3);
  19. set(gcf,'Position',[7 7 700 1000]);
  20. hold on
  21. [miny, maxy] = drawplot(f,df, a,b,c, x0, x1, x2, count);
  22. placelabel(left,0,count);
  23. placelabel(right,0,count);
  24.  
  25. export_fig(gcf, '1.jpg', '-transparent', '-r300');
  26.  
  27. %iterative computation
  28.  
  29. while(ea > tol)
  30.    
  31.     count = count+1;
  32.     %1. compute for h0,h1,d0,d1
  33.    
  34.     fx0 = df(x0);
  35.     fx1 = df(x1);
  36.     fx2 = df(x2);
  37.    
  38.     h0 = x1-x0;
  39.     h1 = x2-x1;
  40.     d0 = ( fx1-fx0 ) / h0;
  41.     d1 = ( fx2-fx1 ) / h1;
  42.    
  43.     %2. compute for a,b,c
  44.     a = (d1-d0) / (h1-h0);
  45.     b = a*h1 + d1;
  46.     c = fx2;
  47.     %if count == 3
  48.     %end
  49.    
  50.     %3. check for sign
  51.     temp = real(sqrt( b*b - 4*a*c ));
  52.     checkpos = b + temp;%holder for |b+sqrt(b^2-4ac)|
  53.     checkneg = b - temp;%holder for |b-sqrt(b^2-4ac)|
  54.    
  55.     %4. compute for x3, using form according to sign
  56.     if abs(checkneg) < abs(checkpos)
  57.         x3 = x2 - (2*c) / checkpos;
  58.     else
  59.         x3 = x2 - (2*c) / checkneg;
  60.     end
  61.    
  62.     drawplot(f, df,  a, b, c, x0, x1, x2, count);
  63.     if count <= 10
  64.         export_fig(gcf,[num2str(count),'.jpg'],'-transparent','-r300');
  65.     end
  66.    
  67.     %5. compute approximate error
  68.     ea = abs((x3-x2)/x3);
  69.    
  70.     %6. if verbose, print values
  71.     if verbose == true
  72.        
  73.         % print: count, x0, x1, x2, x3, ea
  74.         sprintf('Iteration: %i' , count)
  75.         sprintf('x0 = %f', x0)
  76.         sprintf('x1 = %f', x1)
  77.         sprintf('x2 = %f', x2)
  78.         sprintf('Root Estimate: x3 = %f', x3)
  79.         sprintf('Approximate Relative Error: %f', ea)
  80.     end
  81.    
  82.    
  83.     %7. adjust values for next iteration
  84.     x0 = x1;
  85.     x1 = x2;
  86.     x2 = x3;
  87.    
  88. end
  89. %worst case scenario: count maxed out
  90. sprintf('Maximum iterations exhausted')
  91. xmin = x3;
  92. fmin = df(x3);
  93. end
  94.  
  95. function [miny, maxy] = drawplot(f, df, A, B, C, x1, x2, x3, count)
  96. h = 0.05;
  97. asixLeft = - 20;
  98. asixRight = 20;
  99. left = asixLeft -100;
  100. right = asixLeft +100;
  101.  
  102. x = left:h:right;
  103. y = f(x);
  104. miny = min(y) - 10;
  105. maxy = max(y) + 10;
  106. dy = df(x);
  107. mindy = min(dy) - 10;
  108. maxdy = max(dy) + 10;
  109.  
  110. colp = hsv2rgb([rand(), 1, 0.5+0.5*rand()]);
  111. hyper = @(x) A*x.^2 + B*x + C;
  112. yh = hyper(x);
  113. col = hsv2rgb([rand(), 1, 0.5+0.5*rand()]);
  114.  
  115. newplot;
  116. hold on
  117. xlabel('\itx');
  118. ylabel('\ity');
  119. scatter([left right],[f(left), f(right)],'Marker','o','MarkerFaceColor',colp,'MarkerEdgeColor',colp);
  120.  
  121. newplot;
  122. hold on
  123. col = hsv2rgb([rand(), 1, 0.5+0.5*rand()]);
  124. subplot(2,1,1);
  125. line([left right],[0 0],'Color','k','LineWidth',1); %axis x
  126. plot(x,yh,'Color',col, 'LineWidth',0.5);
  127. y1 = hyper(x1);
  128.  
  129. line([x1 x1],[0 y1],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  130. y2 = hyper(x2);
  131. line([x2 x2],[0 y2],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  132. y3 = hyper(x3);
  133. line([x3 x3],[0 y3],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  134. plot(x,dy,'Color',colp, 'LineWidth',2);
  135. placelabel(x1,y1,count);
  136. placelabel(x2,y2,count);
  137. placelabel(x3,y3,count);
  138. axis([asixLeft asixRight -20000 20000]);
  139.  
  140. newplot;
  141. hold on
  142. subplot(2,1,2);
  143. line([left right],[0 0],'Color','k','LineWidth',1); %axis x
  144. y1 = f(x1);
  145. line([x1 x1],[0 y1],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  146. y2 = f(x2);
  147. line([x2 x2],[0 y2],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  148. y3 = f(x3);
  149. line([x3 x3],[0 y3],'Marker','s','Color',col,'LineWidth',1,'MarkerSize',4);
  150. plot(x,y,'Color',colp, 'LineWidth',2);
  151. axis([asixLeft asixRight -20000 20000]);
  152. end
  153.  
  154.  
  155. function placelabel(x,y,iternumber)
  156. if iternumber <=10
  157.     text(x, 0, num2str(iternumber), 'BackgroundColor', 'white');
  158. end
  159. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement