Index exceeding number of array elements on second iteration of loop

조회 수: 1 (최근 30일)
Thomas Sibly
Thomas Sibly 2021년 8월 31일
편집: Walter Roberson 2021년 9월 1일
Apologies if this has been answered previously but I could not see an answer specific to my case. Im modelling the damage properties properties over a number of cycles (c). Theres a number of while loops within while loops if that makes any difference.
A single point is itervatively solved (DELTA_plot,P_plot) if:
if(length==Sp)
if(abs(ddelta_dx(y))<tol&&abs(DELTA(y))<tol)
DELTA_plot = DELTA_initial;
P_plot = Prl_in;
end
figure(1)
title('Plot of Load (P) - Global Deformation (Δ) Relationship')
xlabel('Deformation Δ (mm)')
ylabel('Load P (kN)')
plot(DELTA_plot(c),P_plot(c),'.'); hold on;
grid
This works for the first cycle (c=1), which plots a linear relationship. However when I repeat the exact same code for the second cycle, with different initial parameters, the following error appears:
"Index exceeds the number of array elements (1)."
plot(DELTA_plot(c),P_plot(c),'.'); hold on;
What am I doing wrong? If i try to plot as follow:
plot(DELTA_plot,P_plot,'.'); hold on;
It does not plot correctly.
Please let me know if you require any additonal information and thanks for any help.

답변 (1개)

Mathieu NOE
Mathieu NOE 2021년 8월 31일
hello
your variables are not indexed , so this is no surprise that it will fail at the second iteration
code should be , assuming c is the current loop index :
if(abs(ddelta_dx(y))<tol&&abs(DELTA(y))<tol)
DELTA_plot(c) = DELTA_initial;
P_plot(c) = Prl_in;
end
  댓글 수: 2
Thomas Sibly
Thomas Sibly 2021년 8월 31일
Thanks for the response mate.
When I change the code to:
if(abs(ddelta_dx(y))<tol&&abs(DELTA(y))<tol)
DELTA_plot(c) = DELTA_initial;
P_plot(c) = Prl_in;
end
The following error appears:
"Unable to perform assignment because the indices on the left side are not compatible with the size of the right side."
DELTA_plot(c) = DELTA_initial;
This error occurs in the first cycle now.
Additionally, If I were to make the code:
if(abs(ddelta_dx(y))<tol&&abs(DELTA(y))<tol)
DELTA_plot(c) = DELTA_initial(y);
P_plot(c) = Prl_in(y);
end
The original error appears, also for the first cycle:
"Index exceeds the number of array elements (1)."
Any idea what is causing that issue?
Mathieu NOE
Mathieu NOE 2021년 9월 1일
well , I don't know what your code is supposed to do and what size are variables DELTA_initial and Prl_in and if their size is constant or not as you loop ?
what size are you expecting the result DELTA_plot and P_plot to be ?
are you willing to share the entire code ?

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Line Plots에 대해 자세히 알아보기

제품


릴리스

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by