adjusting value in the function........

조회 수: 9 (최근 30일)
Muhammad Sulhi
Muhammad Sulhi 2011년 3월 28일
This is my initial:
function V = fitnessFcn(Q)
deltad = -24.268; X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
Answer: V = fitnessFcn(rand(1,30))
V =
0.4335
>> V = fitnessFcn(rand(1,30))
V =
0.4956
>> V = fitnessFcn(rand(1,30))
V =
0.7894
>> V = fitnessFcn(rand(1,30))
V =
0.6970
>> V = fitnessFcn(rand(1,30))
V =
0.5132
>> V = fitnessFcn(rand(1,30))
V =
0.7035
>> V = fitnessFcn(rand(1,30))
V =
0.8027
>> V = fitnessFcn(rand(1,30))
V =
0.7466
>> V = fitnessFcn(rand(1,30))
V =
0.8897
>> V = fitnessFcn(rand(1,30))
V =
0.7190
>> V = fitnessFcn(rand(1,30))
V =
0.8302
>> V = fitnessFcn(rand(1,30))
V =
0.4334
>> V = fitnessFcn(rand(1,30))
V =
0.3439
>> V = fitnessFcn(rand(1,30))
V =
0.8189
>> V = fitnessFcn(rand(1,30))
V =
0.6515
>> V = fitnessFcn(rand(1,30))
V =
0.8410
>> V = fitnessFcn(rand(1,30))
V =
0.8377
>> V = fitnessFcn(rand(1,30))
V =
0.7555
>> V = fitnessFcn(rand(1,30))
V =
0.4869
>> V = fitnessFcn(rand(1,30))
V =
0.2456
>> V = fitnessFcn(rand(1,30))
V =
1.0383
So when I run the program, the answer for V will get a random number until I get the value in range 0.95<V<1.05.
After editing:
function V = fitnessFcn(Q)
deltad = -24.268; X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
if (V <= 1.05 V >= 0.95) true else false end
Answer:
V = fitnessFcn(rand(1,30))
ans =
1
V =
0.6990 (this value still not in range of 0.95 until 1.05)
How about if I want the answer between 0.95<V<1.05 automatically means that it will give the answer correct based on the range (0.95<V<1.05). Can anyone help me on this??

답변 (3개)

Aleksander
Aleksander 2011년 3월 29일
just put a for loop around your while statement for example (and this is quick and dirty - might want to make it more efficient and 'nice':
b=[];
for i=1:30
while
statement
end
V=[V b]
end
  댓글 수: 5
Muhammad Sulhi
Muhammad Sulhi 2011년 4월 2일
sorry...I use R2009a version...this is my coding to get that value...
function V = fitnessFcn(Q)
deltad = -24.268;
X = 0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
b=[];
for i=1:30
while (0.95 > V) || (V > 1.05)
Q = rand(1,30);
deltad = -24.268;
X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
end
V=[V b]
end
end
Is it right..??Can you check it for me...from this coding, I get 30 value but give the same answer...can you help me on this matter..??
Muhammad Sulhi
Muhammad Sulhi 2011년 4월 2일
It does not give different value of V...

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


Jan
Jan 2011년 3월 28일
This is a strange line:
if (V <= 1.05 V >= 0.95) true else false end
What does it mean? Perhaps you want this:
if (0.95 <= V) && (V <= 1.05)
reply = true;
else
reply = false;
end
Or shorter:
reply = (0.95 <= V) && (V <= 1.05);
  댓글 수: 1
Muhammad Sulhi
Muhammad Sulhi 2011년 3월 28일
I have try from yours..
1)
function V = fitnessFcn(Q)
deltad = -24.268;
X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
if (0.95 <= V) && (V <= 1.05)
reply = true;
else
reply = false;
end;
answer:
V = fitnessFcn(rand(1,30))
V =
0.8027
2)
function V = fitnessFcn(Q)
deltad = -24.268;
X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
reply = (0.95 <= V) && (V <= 1.05);
answer:
V = fitnessFcn(rand(1,30))
V =
0.7466
both method I've try by replacing it..but the answer still the same..what i meant is..when i run the program, it will give the value exactly in the range of 0.95 until 1.05...I've try from your answer...but the value still not change..it still read the value out of the range..help me please....

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


Aleksander
Aleksander 2011년 3월 28일
From what I can tell, there's nothing in your statement that tells your program to continue executing until the condition 0.95<=V<=1.05 is true. I'm not sure what you're trying to do, but look copy and run this code for example. It will always produce a number within your range. you can "beautify" it but the point is that there is something in there that changes the value until the condition is true:
Q = rand(1,30);
deltad = -24.268;
X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
while (0.95 > V) || (V > 1.05)
Q = rand(1,30);
deltad = -24.268;
X=0.202;
V = sqrt ((Q(30)*X)/(2*(1-cos(deltad/2))));
end
V
  댓글 수: 1
Muhammad Sulhi
Muhammad Sulhi 2011년 3월 29일
thank you from your responding..I had try edit from my coding by combination from yours and me and it works...from the coding,i just produce one value of V..my next question is...how can I produce many value of V..For example I want to produce V1 until V30...but the value V1 until V30 still in the range of 0.95 until 1.05...can anyone help me on this...??

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

카테고리

Help CenterFile Exchange에서 Matrix Indexing에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by