필터 지우기
필터 지우기

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 CenterFile Exchange에서 Stress and Strain에 대해 자세히 알아보기

제품


릴리스

R2023a

Community Treasure Hunt

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

Start Hunting!

Translated by