Advertisement
sandeep03edu

Anni Matlab Fitted Curve - 6

Nov 7th, 2023
1,366
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.93 KB | None | 0 0
  1. %S=[44.71756,44.14105,43.75766,43.18222,42.60572,41.45342,40.49352]; % stress array
  2. N = 8; % number of stress
  3. %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];
  4. arr=[43,218087.4,204592.6,176191.2,186192,158414.3,158415,122424.6]
  5. for i = 1:1 % for different stress
  6.     %arr=M(2,:);
  7.     arr = sort(arr);
  8.     % int p=0;
  9.     %x = (arr(2) - 10):(arr(N + 1) + 10);
  10.     % int s1=int32(arr(2));
  11.     % int s2=mod(s1,10);
  12.     s = 122420;
  13.     % int e1=int32(arr(N+1));
  14.     % int e2=mod(e1,10);
  15.     e = 348240;
  16.     % p=(e-s)/10;
  17.     p = 0;
  18.     s_temp = s;
  19.     while s_temp < e
  20.         p = p + 1;
  21.         s_temp = s_temp + 30000;
  22.     end
  23.     x_co = zeros(1, p);
  24.     y_co = zeros(1, p);
  25.     ct = 1;
  26.     w = 0;
  27.     while s <= e
  28.         mn = s;
  29.         mx = s + 30000;
  30.         count = 0;
  31.         for j = 1:N
  32.             ele = arr(j);
  33.             if ele >= mn && ele <= mx
  34.                 count = count + 1;
  35.                 % else
  36.                 %    break;
  37.             end
  38.         end
  39.         x_co(ct) = (mn + mx) / 2;
  40.         y_co(ct) = count;
  41.         if count > 0
  42.             w = w + 1;
  43.         end
  44.         ct = ct + 1;
  45.         s = s + 30000;
  46.         % if s>=e
  47.         %    break;
  48.         % end
  49.     end
  50.     x_coo = zeros(1, w);
  51.     y_coo = zeros(1, w);
  52.     l = 1;
  53.     for k = 1:p
  54.         if y_co(k) > 0
  55.             y_coo(l) = y_co(k);
  56.             x_coo(l) = x_co(k);
  57.             l = l + 1;
  58.         end
  59.     end
  60.     % plot(x_coo,y_coo);
  61.     % ... (previous code)
  62.  
  63.     % Fit a normal distribution to your data
  64.     pd = fitdist(x_coo', 'Normal');  % Note the transpose of x_coo
  65.  
  66.     % Extract the parameters of the fitted distribution
  67.     mu = pd.mu;
  68.     sigma = pd.sigma;
  69.  
  70.     % Generate x-values for the curve with smaller increments
  71.     x_values = min(x_coo):10:max(x_coo);  % Use smaller increments for a smoother curve
  72.  
  73.     % Calculate the corresponding y-values for the normal distribution using the fitted parameters
  74.     y_fit = pdf(pd, x_values);
  75.  
  76.     % Create a bar plot to connect the data points
  77.     bar(x_coo, y_coo, 'b', 'BarWidth', 0.8);
  78.     hold on;
  79.  
  80.     % Overlay the fitted Normal (Gaussian) curve
  81.     plot(x_values, y_fit, 'r-', 'LineWidth', 2);
  82.     legend('Data Points', 'Gaussian Fit (Bell Curve)');
  83.  
  84.     % ... (rest of your code)
  85.  
  86.  
  87.  
  88.  
  89.     %%%%%%%%%%%%%%%%%%%%%%%%%%%% Filtering Data and Plotting Smooth Curve %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  90.     filterSize = 1;
  91.     x_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  92.     y_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  93.  
  94.     x_mov_fil(1) = x_coo(1);
  95.     y_mov_fil(1) = y_coo(1);
  96.  
  97.     for ii=1:length(x_coo) - filterSize+1
  98.         % x_mov_fil(ii+1) = (x_coo(ii) + x_coo(ii+1))/2;
  99.         % y_mov_fil(ii+1) = (y_coo(ii) + y_coo(ii+1))/2;
  100.  
  101.         sx = 0;
  102.         sy=0;
  103.         for jj=1:filterSize
  104.             sx = sx + x_coo(ii+jj-1);
  105.             sy = sy + y_coo(ii+jj-1);
  106.         end
  107.         sx = sx / (filterSize);
  108.         sy = sy / (filterSize);
  109.  
  110.         x_mov_fil(ii+1) = sx;
  111.         y_mov_fil(ii+1) = sy;
  112.     end
  113.    
  114.    
  115.  
  116.     % Plotting approx plot
  117.     [xx,is] = sort(x_mov_fil(:));
  118.     yy = y_mov_fil(is);
  119.  
  120.     xx = smoothdata(xx, "sgolay");
  121.     yy = smoothdata(yy, "sgolay");
  122.  
  123.     yy = yy(:);
  124.     dx = diff(xx);
  125.     dy = diff(yy);
  126.     y0 = yy(1:end-1);
  127.     n = numel(xx)-1;
  128.     coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
  129.     pp = struct('form', 'pp',...
  130.         'breaks', xx(:)',...
  131.         'coefs', coefs,...
  132.         'pieces', n, ...
  133.         'order', 4,...
  134.         'dim', 1);
  135.     figure
  136.     xi = linspace(min(x_mov_fil),max(x_mov_fil));
  137.     yi = ppval(pp,xi);
  138.        
  139.     plot(xi,yi,'r');
  140.     xlim([min(x_coo),max(x_coo)])
  141.     grid on
  142.  
  143.  
  144.     % Max curve value x and y point
  145.     [~, maxIdx] = max(yi);
  146.     maxiX = xi(maxIdx);
  147.     maxiY = yi(maxIdx);
  148.     disp("Maximum value y= "+ maxiY+ " at x="+ maxiX)
  149.  
  150. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement