Values used to run a for loop
조회 수: 1 (최근 30일)
이전 댓글 표시
Hi, I'm trying to model the strength of a cable that consist of N wires, subjected to strain loading (epsilon). To find the strength of the cable I use 2 for loops and some IF sentences. Since I want to find the strength of the cable based on the strain, I want epsilon to start at zero and calculated at certain steps (a in script) towards a described end value (k in script). If I start with epsilon = 0 then I get: Attempted to access jobb(0); index must be a positive integer or logical. Is there a way that my epsilon value can go from zero and I can have smaler calculation steps than 1? The script works when epsilon = 1 and step = 1..
Here is my script:
clc; clear all; close all;
E = 7.75*1e4;
Area = 38.48;
my = 0.8686;
sy = 0.1318;
mu = 0.4343;
su = 0.3295;
k = 8;
a = 1;
N = 200;
epsilon_0 = 0;
jobb = zeros(k,1);
P = rand(N,1);
epsilon_y = logninv(P,my,sy);
epsilon_u_y = logninv(P,mu,su);
epsilon_u = (epsilon_y + epsilon_u_y);
n = numel(P);
g = 1:a:k;
model = zeros(n,1);
for epsilon = (0+epsilon_0):a:k;
for ii = 1:n;
if epsilon_y(ii) >= epsilon
ep = epsilon;
elseif epsilon_y(ii) < epsilon && epsilon < epsilon_u(ii)
ep = epsilon_y(ii);
elseif epsilon_u(ii) <= epsilon
ep = 0;
end
model(ii) = ((ep/100)*E);
end
p = sum(model)*Area;
jobb(epsilon) = p;
end
Best regards
Trond Oesten
댓글 수: 0
채택된 답변
Michael Haderlein
2015년 2월 12일
편집: Michael Haderlein
2015년 2월 12일
First, you need to distinguish between an index and a value. When you want to assign a value to jobb(epsilon), epsilon is an index. In Matlab, index are integers >=1. Thus, zero is not allowed. In your case, you better create some counter for your outer loop such as you did for the inner loop and work with epsilon(jj) (jj being the counter here). Then, jobb(jj) corresponds to epsilon(jj) and everything is clear.
Apart from that, I think your script can be vectorized. You might want to think about it. If you need help with this respect, please write here again.
댓글 수: 0
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!