Using a Nested For Loop with input from an Array to Create an Array

Hello all,
I am trying to use the code seen below to output an array titled as "Fatigue_Life" when the function of "mancof" changes from positive to negative. The input of j should be for each variable in the "de" array and the output should be an array for the value of i at which the "mancof" function becomes negative. The output array should be the exact same dimension as the input array. For example, de(1,1)=0.00486 should result in Fatigue_Life(1,1)=72 and so on until the array has been fully calculated. The code currently runs but I can not seem to get the output matrix as described above it seems to just be taking the last value? I created the input array "de" of all the same values for simplicity so the output array should also be of the same value, known to be 72. I hope what I would like to happen is clear. Please see the code below, any help will be greatly appreciated.
MATLAB Code:
clc
clear
E=75657;
ef=.057429;
sf=227.365;
b=-0.0787879;
c=-0.6060601;
t=.00486;
de= t.*ones(1,100);
mancof=(sf/E*(2*i)^b)+(ef*(2*i)^c)-de;
Fatigue_Life=0;
for i=1:length(de)
for j=de(:,i)
if mancof>0
mancof=(sf/E*(2*i)^b)+(ef*(2*i)^c)-j;
Fatigue_Life=i
elseif mancof<0
fprintf('The Fatigue Life is: \n', Fatigue_Life)
end
end
end

 채택된 답변

Andrei Bobrov
Andrei Bobrov 2013년 4월 26일
편집: Andrei Bobrov 2013년 4월 26일
ii = (1:100)';
mancof = sf/E*(2*ii).^b + ef*(2*ii).^c - t;
Fatigue_Life = find(mancof > 0);
ADD after Luke's comment
ii = (1:100)';
t = .00485 + .001*rand(30,1); % eg
mancof = bsxfun(@gt, sf/E*(2*ii).^b + ef*(2*ii).^c, t(:)');
[i1,j1] = find(mancof);
Fatigue_Life = [i1,j1];

댓글 수: 3

Hello Andrei,
I appreciate the response but this isn't exactly what I need because the variable "t" needs to be from an array that in the future will be of several different values. The input to the for loop should be for each variable of the array and then the result "Fatigue_Life" should be stored in an output matrix for each iteration.
See ADD part in my answer.
Hello Andrei,
I finally got what I needed with the assistance of you, this seems to be a much simpler approach compared to the nested for loops. Below is the working code of yours that i slightly modified to get exactly what I needed. Thanks alot for all your help.
clc clear
E=75657;
ef=.057429;
sf=227.365;
b=-0.0787879;
c=-0.6060601;
de=[.00486, 0.004,0.0055,.003];
ii= (1:1:10000)'; % Number of cycles
mancof= bsxfun(@gt, sf/E*(2*ii).^b + ef*(2*ii).^c, de(:)');
[i1,j1]= find(mancof);
Fatigue= [i1,j1];
for i= 1:length(de)
Fatigue_Life(i)=find(mancof(:,i),1,'last');
end
Fatigue_Life

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

추가 답변 (0개)

카테고리

도움말 센터File Exchange에서 Loops and Conditional Statements에 대해 자세히 알아보기

제품

질문:

2013년 4월 26일

Community Treasure Hunt

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

Start Hunting!

Translated by