I'm trying to use a while loop to delete the final row in an array every time it loops, however, it's not working

3 visualizaciones (últimos 30 días)
Basically, using the file TensileData.xlsx that I have attached to the question, I have to use a loop, work backwards through the entire data set reducing the number of points to find the equation of the elastic region. This can be done by calculating the r^2 value until the r^2 value is larger than 0.9997. However, when I try to create write the code for this I am unable to do it. I'm not completely sure where to begin, however, I have made a start. When I run the code I receive the error: Index in position 1 is invalid. Array indices must be positive integers or logical values.
Error in lab8t5 (line 26)
strain(numRows_strain, :) = [];
The code that my question relates to is part b onwards, however, I have put all the code here in case it is helpful.
clear all; close all; clc
% Part a.
data = importdata('TensileData.xlsx');
strain = data.data(:, 1);
stress = data.data(:, 2);
figure(1)
plot(strain, stress, 'r*')
% Part b.
numRows_strain = length(strain);
numRows_stress = size(stress, 1);
[a0, a1, r2] = linreg(strain, stress);
slope = a1;
constant = a0;
error = r2;
while error <= 0.9997
[a0, a1, r2] = linreg(strain, stress);
numRows_strain = numRows_strain - 1;
numRows_stress = numRows_stress - 1;
strain(numRows_strain, :) = [];
stress(numRows_stress, :) = [];
end

Respuesta aceptada

Dyuman Joshi
Dyuman Joshi el 14 de Sept. de 2023
Firstly, use readmatrix or readtable instead of importdata.
Secondly, you can define variables from function calls, instead of reassinging them. And as mentioned before, do not use error as a variable name, because it is a built-in MATLAB function.
%Directly use this
[slope, constant, err] = linreg(strain, stress);
Thirdly, you are not updating the variable error in the while loop, so depending upon its value, the while loop will not run or run forever.
% Part a.
data = readmatrix('TensileData.xlsx');
strain = data(:, 1);
stress = data(:, 2);
figure(1)
plot(strain, stress, 'r*')
% Part b.
[slope, constant, err] = linreg(strain, stress);
while err <= 0.9997
%Removing the last row
strain(end, :) = [];
stress(end, :) = [];
%Computing the r_square value again
[~, ~, err] = linreg(strain, stress);
end
Since I do not have the linreg() function, I can not run the above code, but it should do what you asked for - delete the last row till the condition is met.
  2 comentarios
Image Analyst
Image Analyst el 14 de Sept. de 2023
You can make it more robust like this
while err <= 0.9997 && ~isempty(strain) && ~isempty(stress)
% Remove the last row from each matrix:
strain(end, :) = [];
stress(end, :) = [];
% Bail out if either matrix is empty.
if isempty(strain) || isempty(stress)
break;
end
% Compute the r_square value again.
[~, ~, err] = linreg(strain, stress);
end

Iniciar sesión para comentar.

Más respuestas (1)

Richard Burnside
Richard Burnside el 14 de Sept. de 2023
Editada: Richard Burnside el 14 de Sept. de 2023
I think you could just do something vectorized like this:
idx = find(r2>0.9997);
stress(idx) = [];
strain(idx) = [];
Also I think "error" is a Matlab function so you should avoid using it as a variable.

Categorías

Más información sobre Stress and Strain en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by