why is the code considering only one value when the variable is in an iteration loop

조회 수: 1 (최근 30일)
hi. i am trying to run a loop taking the variable value from x=500:50:5000. it is considering each number starting from 1 and for the final assign it is considering only 1 value

채택된 답변

Walter Roberson
Walter Roberson 2013년 10월 21일
I recommend you rewrite your code, using something similar to
xvals = 500:50:5000;
numx = length(xvals);
for K = 1 : numx
x = xvals(K);
%lots of code here
end
Inside the "lots of code here", continue to use "x" in places where the specific value of x makes a difference, but use "K" where you want just want to indicate a vector accessed at a location corresponding to which x you are using.
For example, when you are assigning to f1(x) you don't really want to be assigning to f1(3500), the 3500'th location in f1: you want to be assigning to f1(7), corresponding to the fact that 3500 is the 7th x value.
I suspect you could also build an Re table and index it inside a for loop instead of using all of those awkward "for Re4 = 9000" constructs. Accumulate all the f*() results for a given x value into a vector and sum() the vector for your final f(x) = fi(x) - (f1(x)+f2(x)+....], which would become something like
f(K) = fi(K) - sum(fvals);
  댓글 수: 2
trilochan
trilochan 2013년 10월 21일
can you please tell me how to write the same code in genetic algorithm solver
Walter Roberson
Walter Roberson 2013년 10월 21일
Sorry, I am not familiar with coding for the genetic algorithm solver.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by