Advertisement
Hamikadze

Untitled

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