Advertisement
sandeep03edu

Anni Matlab Fitted Curve - 4

Nov 7th, 2023
1,191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
MatLab 3.90 KB | None | 0 0
  1. %S=[44.71756,44.14105,43.75766,43.18222,42.60572,41.45342,40.49352]; % stress array
  2. N = 5; % 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,5600.23,6101.45,6450.23,6789.23,7123.56]
  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 = 4000;
  13.     % int e1=int32(arr(N+1));
  14.     % int e2=mod(e1,10);
  15.     e = 7500;
  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 + 500;
  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 + 500;
  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 + 500;
  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.     %%%%%%%%%%%%%%%%%%%%%%%%%%%% Filtering Data and Plotting Smooth Curve %%%%%%%%%%%%%%%%%%%%%%%%%%%%
  88.     filterSize = 1;
  89.     x_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  90.     y_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
  91.  
  92.     x_mov_fil(1) = x_coo(1);
  93.     y_mov_fil(1) = y_coo(1);
  94.  
  95.     for ii=1:length(x_coo) - filterSize+1
  96.         % x_mov_fil(ii+1) = (x_coo(ii) + x_coo(ii+1))/2;
  97.         % y_mov_fil(ii+1) = (y_coo(ii) + y_coo(ii+1))/2;
  98.  
  99.         sx = 0;
  100.         sy=0;
  101.         for jj=1:filterSize
  102.             sx = sx + x_coo(ii+jj-1);
  103.             sy = sy + y_coo(ii+jj-1);
  104.         end
  105.         sx = sx / (filterSize);
  106.         sy = sy / (filterSize);
  107.  
  108.         x_mov_fil(ii+1) = sx;
  109.         y_mov_fil(ii+1) = sy;
  110.     end
  111.    
  112.    
  113.  
  114.     % Plotting approx plot
  115.     [xx,is] = sort(x_mov_fil(:));
  116.     yy = y_mov_fil(is);
  117.  
  118.     xx = smoothdata(xx, "sgolay");
  119.     yy = smoothdata(yy, "sgolay");
  120.  
  121.     yy = yy(:);
  122.     dx = diff(xx);
  123.     dy = diff(yy);
  124.     y0 = yy(1:end-1);
  125.     n = numel(xx)-1;
  126.     coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
  127.     pp = struct('form', 'pp',...
  128.         'breaks', xx(:)',...
  129.         'coefs', coefs,...
  130.         'pieces', n, ...
  131.         'order', 4,...
  132.         'dim', 1);
  133.     figure
  134.     xi = linspace(min(x_mov_fil),max(x_mov_fil));
  135.     yi = ppval(pp,xi);
  136.        
  137.     plot(xi,yi,'r');
  138.     xlim([min(x_coo),max(x_coo)])
  139.     grid on
  140.  
  141.  
  142.     % Max curve value x and y point
  143.     [~, maxIdx] = max(yi);
  144.     maxiX = xi(maxIdx);
  145.     maxiY = yi(maxIdx);
  146.     disp("Maximum value y= "+ maxiY+ " at x="+ maxiX)
  147.  
  148. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement