Advertisement
sandeep03edu

Anni Matlab Fitted Curve - 1

Nov 7th, 2023 (edited)
1,173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 4.97 KB | None | 0 0
  1. %M=[44.71756,2554.679,2265.398,2576.034,2867.556,2667.752,2632.009,2600.986,2599.868;
  2. %    44.14105,4206.833,4681.949,5677.823,6185.949,5101.299,6596.67,6414.134,1;
  3. %    43.75766,4298.244,5801.197,2930.065,5323.585,2079.171,2078.888,2747.259,1;
  4. %    43.18222,81941.96,89244.75,78504.41,63392.84,63388.52,6319.933,4119.334,1;
  5. %    42.60572,12237.45,11236.08,12505.07,13619.54,13054.42,16876.52,12511.04,1;
  6.  %   41.45342,149102.6,155620.9,101475.3,110534,192717.7,233582.2,228677,1;
  7.  %   40.49352,21808.74,20459.26,17619.12,22288.7,348237.8,14841.43,12242.46,1];
  8. %S=[44.71756,44.14105,43.75766,43.18222,42.60572,41.45342,40.49352]; % stress array
  9. N=26; %number of stress
  10. arr=[2262,2320,2321,2365,2400,2450,2451,2452,2465,2500,2501,2511,2515,2555,2556,2566,2567,2568,2615,2616,2617,2666,2667,2715,2715,2765,2820];
  11. length(arr)
  12. for i=1:1  % for different stress
  13.     %arr=M(i,:);
  14.     arr=sort(arr);
  15.     %int p=0;
  16.     x=(arr(2)-10):(arr(N+1)+10);
  17.     %int s1=int32(arr(2));
  18.     %int s2=mod(s1,10);
  19.     s=2260;
  20.     %int e1=int32(arr(N+1));
  21.     %int e2=mod(e1,10);
  22.     e=2870;
  23.     %p=(e-s)/10;
  24.     p=0;
  25.     s_temp=s;
  26.     while s_temp<e
  27.         p=p+1;
  28.         s_temp=s_temp+50;
  29.     end
  30.     x_co=zeros(1,p);
  31.     y_co=zeros(1,p);
  32.     ct=1;
  33.     w=0;
  34.     while s<=e
  35.         mn=s;
  36.         mx=s+50;
  37.         count=0;
  38.         for j=1:27
  39.             ele=arr(j);
  40.             if ele>=mn && ele<=mx
  41.                 count=count+1;
  42.             %else
  43.             %    break;
  44.             end
  45.         end
  46.         x_co(ct)=(mn+mx)/2;
  47.         y_co(ct)=count;
  48.         if count>0
  49.             w=w+1;
  50.         end
  51.         ct=ct+1;
  52.         s=s+50;
  53.        % if s>=e
  54.        %     break;
  55.         %end
  56.     end
  57.     x_coo=zeros(1,w);
  58.     y_coo=zeros(1,w);
  59.     l=1;
  60.     for k=1:p
  61.         if y_co(k)>0
  62.             y_coo(l)=y_co(k);
  63.             x_coo(l)=x_co(k);
  64.             l=l+1;
  65.         end
  66.     end
  67.     %plot(x_coo,y_coo);
  68.     % ... (previous code)
  69.  
  70. % Fit a log-normal distribution to your data
  71. % Define the distribution object using the 'lognormal' distribution type
  72.    % ... (previous code)
  73.  
  74. % Fit a normal distribution to your data
  75. % Define the distribution object using the 'Normal' distribution type
  76.     % ... (previous code)
  77.  
  78. % Fit a normal distribution to your data
  79. % Define the distribution object using the 'Normal' distribution type
  80.     % ... (previous code)
  81.  
  82. % Fit a normal distribution to your data
  83. % Define the distribution object using the 'Normal' distribution type
  84.     % ... (previous code)
  85.  
  86. % Fit a normal distribution to your data
  87. % Define the distribution object using the 'Normal' distribution type
  88.     % ... (previous code)
  89.  
  90. % Fit a normal distribution to your data
  91. % Define the distribution object using the 'Normal' distribution type
  92.     % ... (previous code)
  93.  
  94. % Fit a normal distribution to your data
  95. % Define the distribution object using the 'Normal' distribution type
  96.     % ... (previous code)
  97.  
  98. % Create a bar plot to connect the data points
  99.     pd = fitdist(x_coo', 'Normal');  % Note the transpose of x_coo
  100.  
  101.     % Extract the parameters of the fitted distribution
  102.     mu = pd.mu;
  103.     sigma = pd.sigma;
  104.  
  105.     % Generate x-values for the curve with smaller increments
  106.     x_values = min(x_coo):10:max(x_coo);  % Use smaller increments for a smoother curve
  107.  
  108.     % Calculate the corresponding y-values for the normal distribution using the fitted parameters
  109.     y_fit = pdf(pd, x_values);
  110.  
  111.     % Create a bar plot to connect the data points
  112.     bar(x_coo, y_coo, 'yellow', 'BarWidth', 1);
  113.     hold on;
  114.  
  115.     % Overlay the fitted Normal (Gaussian) curve
  116.     plot(x_values, y_fit, 'r-', 'LineWidth', 2);
  117.     legend('Data Points', 'LogNormal');
  118.  
  119.     % ... (rest of your code)
  120.  
  121.  
  122.  
  123.  
  124.     %%%%%%%%%%%%%%%%%%%%%%%%%%%% Filtering Data and Plotting Smooth Curve %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  125.     filterSize = 2;
  126.     x_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  127.     y_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  128.  
  129.     x_mov_fil(1) = x_coo(1);
  130.     y_mov_fil(1) = y_coo(1);
  131.  
  132.     for ii=1:length(x_coo) - filterSize+1
  133.         x_mov_fil(ii+1) = (x_coo(ii) + x_coo(ii+1))/2;
  134.         y_mov_fil(ii+1) = (y_coo(ii) + y_coo(ii+1))/2;
  135.     end
  136.    
  137.  
  138.     % Plotting approx plot
  139.     [xx,is] = sort(x_mov_fil(:));
  140.     yy = y_mov_fil(is);
  141.  
  142.     xx = smoothdata(xx, "sgolay");
  143.     yy = smoothdata(yy, "sgolay");
  144.  
  145.     yy = yy(:);
  146.     dx = diff(xx);
  147.     dy = diff(yy);
  148.     y0 = yy(1:end-1);
  149.     n = numel(xx)-1;
  150.     coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
  151.     pp = struct('form', 'pp',...
  152.         'breaks', xx(:)',...
  153.         'coefs', coefs,...
  154.         'pieces', n, ...
  155.         'order', 4,...
  156.         'dim', 1);
  157.     % figure
  158.     xi = linspace(min(x_mov_fil),max(x_mov_fil));
  159.     yi = ppval(pp,xi);
  160.        
  161.     plot(xi,yi,'r');
  162.     xlim([min(x),max(x)])
  163.     grid on
  164.  
  165.  
  166.     % Max curve value x and y point
  167.     [~, maxIdx] = max(yi);
  168.     maxiX = xi(maxIdx);
  169.     maxiY = yi(maxIdx);
  170.     disp("Maximum value y= "+ maxiY+ " at x="+ maxiX)
  171.  
  172. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement