adjusting value in the function........
Mostrar comentarios más antiguos
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??
Respuestas (3)
Aleksander
el 29 de Mzo. de 2011
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 comentarios
Muhammad Sulhi
el 30 de Mzo. de 2011
Aleksander
el 1 de Abr. de 2011
I get all different values so that sounds strange - after all you are generating random numbers and there is no reason they should produce the same value. What version of Matlab do you have? It might be a problem with your random seed, that is, how Matlab generates a random number.
In older versions (before R2008b I think), if you generate some random numbers in a program, and I run the same program as you, we would get identical results due to the manner in which Matlab generated the random numbers. Newer versions use a different algorithm to avoid this (although, for training purposes it might be useful to use the old algorithm if you want to compare results when solving a problem).
Anyway, try this:
Create a number stream:
1)
s = RandStream('mt19937ar')
Make it the default stream:
2)
RandStream.setDefaultStream(s)
I don't remember off the top of my head how to see what your current seed is but I'm guessing someting like getDefaultStrem or RandStream.getDefaultstream
Aleksander
el 1 de Abr. de 2011
Just to be clear, simply copy the lines directly and run them.
You don't need to 'create' anything yourself per se. That is, don't let the 'mt19937ar' statement confuse you. It is just the name Matlab uses for that particular random number generator.
Muhammad Sulhi
el 2 de Abr. de 2011
Muhammad Sulhi
el 2 de Abr. de 2011
Jan
el 28 de Mzo. de 2011
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 comentario
Muhammad Sulhi
el 28 de Mzo. de 2011
Aleksander
el 28 de Mzo. de 2011
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 comentario
Muhammad Sulhi
el 29 de Mzo. de 2011
Categorías
Más información sobre Matrix Indexing en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!