Advertisement
Guest User

Root_finding

a guest
Jul 10th, 2019
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.68 KB | None | 0 0
  1. % Experimental script to find the roots of the Laplacian spectrum function
  2. % using different methods; for testing purposes only, see the eigfinder
  3. % function for the current working version.
  4.  
  5. clear
  6. clc
  7. %close all
  8. set(0,'defaultlegendFontSize',14)
  9. set(0,'defaulttextInterpreter','latex') %latex axis labels
  10. set(0,'defaultlegendInterpreter','latex') %latex axis labels
  11. set(0,'defaultAxesFontSize',14)
  12. set(0,'defaultAxesTickLabelInterpreter','latex')
  13.  
  14. %% Problem conditions %%
  15. global m kappa D l Kp Km
  16. m = 3;
  17. kappa = ones(1,m)*0.1;
  18. D = ones(1,m);
  19. l = ones(1,m);
  20. Kp = 0;
  21. Km = 0;
  22.  
  23. %% Calculate function %%
  24. N = 100;
  25. maxeig=100;
  26. eta = linspace(10^-3,maxeig,N);
  27. F = zeros(1,N); % Using my function
  28. F_Moutal = zeros(1,N); % Using Moutal's function
  29. for i = 1:N
  30.     F(i) = function_F(eta(i));
  31.     F_Moutal(i) = general_condition(sqrt(eta(i)),sqrt(D),kappa,l,Km,Kp);
  32. end
  33.  
  34. figure
  35. plot(eta,F,'LineWidth',1.5)
  36. hold on
  37. plot(eta,F_Moutal,'LineWidth',1.5,'LineStyle','--')
  38. plot([0 maxeig],[0 0],'k','LineWidth',1,'LineStyle',':')
  39. xlabel('Trial value $\eta$')
  40. ylabel('Function evaluation $F(\eta)$')
  41.  
  42. %% Find preliminary zeros %%
  43. s = spline(eta,F);
  44. z = fnzeros(s,[0 maxeig]); % Finds the eigenvalues within range
  45. spectrum = [0 z(1,:)];
  46.  
  47. % Moutal's result
  48. [spectrum_M,~]=function_modes(D, kappa, l, Km, Kp, 10, maxeig);
  49.  
  50. scatter(spectrum,zeros(1,numel(spectrum)),'LineWidth',1.5)
  51. hold on
  52. scatter(spectrum_M,zeros(1,numel(spectrum_M)),'LineWidth',1.5)
  53. legend('Function','Zero','In-house','Moutal')
  54.  
  55. %% check if zeros are actually zeros %%
  56. Z_Moutal = zeros(1,numel(spectrum_M));
  57. for i = 1:numel(spectrum_M)
  58.     Z_Moutal(i) = general_condition(sqrt(spectrum_M(i)),sqrt(D),kappa,l,Km,Kp);
  59.     %Z_Moutal(i) = function_F(spectrum_M(i));
  60. end
  61. for i = numel(spectrum)
  62.     Z(i) = function_F(spectrum(i));
  63. end
  64. figure
  65. scatter(spectrum_M,spectrum_M)
  66. hold on
  67. scatter(spectrum,spectrum)
  68.  
  69. %% Get zeros fancy %%
  70. clearvars F
  71. maxeig=1000;
  72. N = maxeig*10;
  73. tic
  74. eta = linspace(10^-3,maxeig,N);
  75. for i = 1:N
  76.     F(i) = function_F(eta(i));
  77. end
  78. threshold = 2;
  79. dfine = 10^-2;
  80. % Now we will try to identify the intervals close to or already including a
  81. % zero. Note that using the logical arrays, we will identify the intervals
  82. % to be explored further with ZEROS and the areas to be scrapped with ONES.
  83. range = ((abs(F)<threshold) == 0);
  84. signchange = (zeros(1,N)==0);
  85. signchange(1:N-1) = (diff(sign(F)) == 0);
  86. signchange(2:N) = signchange(2:N).*(diff(sign(F)) == 0);
  87. % Note: two previous lines insure that if a sign change was missed, a new
  88. % INTERVAL (i.e. two consecutive logical zeros) are added and not a single
  89. % point (remember diff uses two consecutive points).
  90. range = range.*signchange; % will add an interval if a signchange occurs outside of bounds
  91. % Add the first zero
  92. IDbounds = [1 find(diff(range))];
  93. if (-1)^numel(IDbounds) ~= 1 % Essentially closes last bound if left open
  94.     IDbounds = [IDbounds N];
  95. end
  96.  
  97. plot(eta,F,'LineWidth',1,'Color','k')
  98. hold on
  99. I = 2;
  100. roots = 0;
  101. for i = 1:2:numel(IDbounds)
  102.     lowb = eta(IDbounds(i));
  103.     upb = eta(IDbounds(i+1));
  104.     etafine = lowb:dfine:upb;
  105.     Ffine = zeros(1,length(etafine));
  106.     for j = 1:numel(etafine)
  107.         Ffine(j) = function_F(etafine(j));
  108.     end
  109.     s = spline(etafine,Ffine);
  110.     z = fnzeros(s,[min(etafine) max(etafine)]);
  111.     roots = [roots z(1,:)];
  112.     plot(etafine,Ffine,'LineWidth',4);
  113. end
  114. toc
  115.  
  116. tic
  117. roots2 = eigfinder(maxeig,2);
  118. toc
  119.  
  120. tic
  121. roots_M=function_eigenvalues(D, kappa, l, Km, Kp, maxeig);
  122. toc
  123.  
  124. plot([0 maxeig],[0 0],'k','LineWidth',1,'LineStyle',':')
  125. scatter(roots2,zeros(1,numel(roots2)),35,'MarkerEdgeColor','black','LineWidth',1.5)
  126. xlabel('$\eta$')
  127. ylabel('$F(\eta)$')
  128.  
  129. disp(sum(abs(roots2-roots_M)))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement