フィルターのクリア

Store Values in Array per Loop iteration issues

7 ビュー (過去 30 日間)
Sam Blake
Sam Blake 2023 年 11 月 2 日
コメント済み: Sam Blake 2023 年 11 月 2 日
Hello everyone. I have a script made to make and analyze the stress strain curves for multiple data sets. I want to store the youngs modulus found in an array "youngs". The modulus is calcualted using polyfit, and then stored in the 'linear_fit' variable. In the code below, you can see at the bottom, i attempt to go and load the first value into 'mod' and then assign it to the 'i'th iterations spot in the 'youngs' array. So since I have 8 data sets, I should see 8 youngs modulus's. However, when I run this code and step through it, only after the first iteration, the 'youngs' array is a 1x386, and by the end of the loop, it is 1x402. I will include a sample data .csv but I cant include all datasets. Anyone that can look through this and spot why it isnt just assigning one value for 'mod' to one value for 'youngs' resulting in a 1 value per for loop iteration data set, id really appreciate that. Once again for clarity, I want to store the value assigned to 'mod' in 'youngs' for each iteration, ultimately making a nx1 array for 'youngs' representing each value derived per for loop iteration. Thank you!
% Loading & Plotting Compression Testing Data
clc;clear;close all
% Data Read
number_files = 8;
youngs = zeros(1,8) ;
for i = 1:number_files
filename = sprintf('DAQ%d.txt', i);
Data = importdata(filename);
%Setting time and pounds variables
time = Data.data(:,3);
PoundsForce = Data.data(:,2);
%Use geometry to convert displacment to strain and pounds to stress
inches = Data.data(:,1);
Area = 2.659044022;
Stress = -1 * (PoundsForce ./ -Area);
Strain = -1 * inches / -.85;
%Cleaning values to only include up to .45 engineering strain
[p,~] = find(Strain > .45 );
Strain(p,:) = [] ;
%Setting strain and stress vectors equal length
Stress = Stress(1:length(Strain),1);
figure;
hold on
% finding linear region by Lower Strain Bound < x > Upper Strain Bound
[i, ~] = find(Strain < 0.01 | Strain > 0.15);
Strain(i, :) = [];
% Normalizing vector length
Stress = Stress(1:length(Strain),1);
% Fitting linear region
[linear_fit,Error] = polyfit(Strain,Stress,1);
[y_fit,delta] = polyval(linear_fit,Strain,Error);
% Evaluating R^2
fit = linear_fit(1).*Strain + linear_fit(2);
SSR = sum((Stress - fit).^2) ; SST = sum((Stress - mean(Stress)).^2);
R_squared = abs(1 - (SSR / SST));
%Plotting strain and stress linear region
OG_Stress = -1 * (PoundsForce ./ -Area);
OG_Strain = -1 * inches / -.85;
Vertical = ones(length(Strain),1);
Vertical = (Vertical .* Strain(end));
Horizontal = ones(length(fit),1);
Horizontal = (Horizontal .* fit(1));
Slope_legend = sprintf('Slope = %d PSI',linear_fit(1));
Rsquared_legend = sprintf('R_squared = %d ',R_squared);
Triangle = plot(Vertical,(Stress+.01),'k--',Strain,Horizontal,'k--',Strain,(fit+.02),'r');
OG_plot = plot(OG_Strain,OG_Stress,'b');
ylim([0,3]);
lgd1 = legend([Triangle(3), OG_plot],{Slope_legend,'Experimental Data'},'Location','southeast');
%title(lgd1,Rsquared_legend);
ylabel('Stress (PSI) ','FontSize',24,'FontWeight','bold') ; xlabel('Strain','FontSize',24,'FontWeight','bold') ;
title('Stress versus Strain Plot','FontSize',24,'FontWeight','bold')
dim = [.4 .007 .2 .2] ; str = 'E' ;
A = annotation("textbox",dim,'string',str,'FitBoxToText','on');
A.EdgeColor = 'none'; hold off
%Plotting 95% confidence error bounds
figure; hold on
plot(Strain,fit+2*delta,'m--',Strain,fit-2*delta,'m--')
%Plotting original stress strain over linear region
Linear_region = plot(Strain,Stress);
%plotting linear fit
Linear_plot = plot(Strain,fit,'r');
ylabel('Stress (PSI) ') ; xlabel('Strain')
title('Stress versus Strain Plot','Linear Region With Line of Best Fit in 95% Confidence')
lgd2 = legend([Linear_plot Linear_region],{Slope_legend,'Experimental Data'},'Location','southeast');
ylim([0,fit(end)]);
title(lgd2,Rsquared_legend)
hold off
% Setting mod to linear_fit(1) - mod should be a 1x1
mod = linear_fit(1) ;
youngs(i) = mod ;
end

採用された回答

Torsten
Torsten 2023 年 11 月 2 日
Replace
[i, ~] = find(Strain < 0.01 | Strain > 0.15);
Strain(i, :) = [];
by
[j, ~] = find(Strain < 0.01 | Strain > 0.15);
Strain(j, :) = [];
And rename the variable "mod" ; "mod" is a built-in MATLAB function.
  1 件のコメント
Sam Blake
Sam Blake 2023 年 11 月 2 日
Good eye! thank you so much, its amazing what you can miss looking over your own code.

サインインしてコメントする。

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeStress and Strain についてさらに検索

製品


リリース

R2023a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by