Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- %S=[44.71756,44.14105,43.75766,43.18222,42.60572,41.45342,40.49352]; % stress array
- N = 6; % number of stress
- %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];
- arr=[43,121475.3,110534,192717.7,203582.2,168677,125874.8]
- for i = 1:1 % for different stress
- %arr=M(2,:);
- arr = sort(arr);
- % int p=0;
- %x = (arr(2) - 10):(arr(N + 1) + 10);
- % int s1=int32(arr(2));
- % int s2=mod(s1,10);
- s = 109870;
- % int e1=int32(arr(N+1));
- % int e2=mod(e1,10);
- e = 203590;
- % p=(e-s)/10;
- p = 0;
- s_temp = s;
- while s_temp < e
- p = p + 1;
- s_temp = s_temp + 10000;
- end
- x_co = zeros(1, p);
- y_co = zeros(1, p);
- ct = 1;
- w = 0;
- while s <= e
- mn = s;
- mx = s + 10000;
- count = 0;
- for j = 1:N
- ele = arr(j);
- if ele >= mn && ele <= mx
- count = count + 1;
- % else
- % break;
- end
- end
- x_co(ct) = (mn + mx) / 2;
- y_co(ct) = count;
- if count > 0
- w = w + 1;
- end
- ct = ct + 1;
- s = s + 10000;
- % if s>=e
- % break;
- % end
- end
- x_coo = zeros(1, w);
- y_coo = zeros(1, w);
- l = 1;
- for k = 1:p
- if y_co(k) > 0
- y_coo(l) = y_co(k);
- x_coo(l) = x_co(k);
- l = l + 1;
- end
- end
- % plot(x_coo,y_coo);
- % ... (previous code)
- % Fit a normal distribution to your data
- pd = fitdist(x_coo', 'Normal'); % Note the transpose of x_coo
- % Extract the parameters of the fitted distribution
- mu = pd.mu;
- sigma = pd.sigma;
- % Generate x-values for the curve with smaller increments
- x_values = min(x_coo):10:max(x_coo); % Use smaller increments for a smoother curve
- % Calculate the corresponding y-values for the normal distribution using the fitted parameters
- y_fit = pdf(pd, x_values);
- % Create a bar plot to connect the data points
- bar(x_coo, y_coo, 'b', 'BarWidth', 0.8);
- hold on;
- % Overlay the fitted Normal (Gaussian) curve
- plot(x_values, y_fit, 'r-', 'LineWidth', 2);
- legend('Data Points', 'Gaussian Fit (Bell Curve)');
- % ... (rest of your code)
- %%%%%%%%%%%%%%%%%%%%%%%%%%%% Filtering Data and Plotting Smooth Curve %%%%%%%%%%%%%%%%%%%%%%%%%%%%
- filterSize = 1;
- x_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
- y_mov_fil = zeros(length(x_coo)-filterSize+2, 1);
- x_mov_fil(1) = x_coo(1);
- y_mov_fil(1) = y_coo(1);
- for ii=1:length(x_coo) - filterSize+1
- % x_mov_fil(ii+1) = (x_coo(ii) + x_coo(ii+1))/2;
- % y_mov_fil(ii+1) = (y_coo(ii) + y_coo(ii+1))/2;
- sx = 0;
- sy=0;
- for jj=1:filterSize
- sx = sx + x_coo(ii+jj-1);
- sy = sy + y_coo(ii+jj-1);
- end
- sx = sx / (filterSize);
- sy = sy / (filterSize);
- x_mov_fil(ii+1) = sx;
- y_mov_fil(ii+1) = sy;
- end
- % Plotting approx plot
- [xx,is] = sort(x_mov_fil(:));
- yy = y_mov_fil(is);
- xx = smoothdata(xx, "sgolay");
- yy = smoothdata(yy, "sgolay");
- yy = yy(:);
- dx = diff(xx);
- dy = diff(yy);
- y0 = yy(1:end-1);
- n = numel(xx)-1;
- coefs = [-2*dy./(dx.^3), 3*dy./(dx.^2), 0*dy, y0];
- pp = struct('form', 'pp',...
- 'breaks', xx(:)',...
- 'coefs', coefs,...
- 'pieces', n, ...
- 'order', 4,...
- 'dim', 1);
- figure
- xi = linspace(min(x_mov_fil),max(x_mov_fil));
- yi = ppval(pp,xi);
- plot(xi,yi,'r');
- xlim([min(x_coo),max(x_coo)])
- grid on
- % Max curve value x and y point
- [~, maxIdx] = max(yi);
- maxiX = xi(maxIdx);
- maxiY = yi(maxIdx);
- disp("Maximum value y= "+ maxiY+ " at x="+ maxiX)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement