Trial and Error

17 ビュー (過去 30 日間)
Nasir Qazi
Nasir Qazi 2012 年 2 月 12 日
Can someone give a example of Matlab codes perform 'trial and error' it will assume certain value , get the answer , error check , in case of suitable result stop the loop ?

回答 (3 件)

the cyclist
the cyclist 2012 年 2 月 12 日
Here is a simple example. Is this what you mean by "trial and error"?
x = 100;
while x > 1
x = x-1;
end
x

Image Analyst
Image Analyst 2012 年 2 月 12 日
Make an array with your "certain values" like, for example, choices=[1,2,3,4,100,200,1000] or whatever. Then use randi() to pick an index from that at random. Break out if you selected the value to stop at.
% Define the "certain values"
choices=[1,2,3,4,100,200,1000]
% Tell it to stop if choice #4 is chosen.
choiceToStopAt = choices(4);
for k = 1 : 200
randomIndex = randi(length(choices), 1);
selectedChoice = choices(randomIndex);
fprintf('At experiment %d, randomIndex = %d, selected choice = %d\n',...
k, randomIndex, selectedChoice);
if selectedChoice == choiceToStopAt
fprintf('Stopping because we selected %d\n', choiceToStopAt)
break;
end
end
In the command window:
choices =
1 2 3 4 100 200 1000
At experiment 1, randomIndex = 5, selected choice = 100
At experiment 2, randomIndex = 1, selected choice = 1
At experiment 3, randomIndex = 6, selected choice = 200
At experiment 4, randomIndex = 4, selected choice = 4
Stopping because we selected 4

Nasir Qazi
Nasir Qazi 2012 年 2 月 12 日
Pn = 0.18; << Assume Value
Phil, Phiv ( is calualted with some equation using above Pn value
Fv=Pn*phiv;
Fl=Pn*phil; (Fl-Fv) < 0.00001
Trail and error command are starting here
(I am seeking help looks like this ) if (Fl-Fv) < 0.00001 <<<< error check so above assume pressure is right and display it if not caluclate another pressure with following relation Pnew = Pn*(phil/phiv); this new pressure will take the place of above assume pressure and again check error check if we got the our error is less then 0.00001 , stop and display the pressure, this is like trial and error and might run in a loop but i dont know how.. need help

カテゴリ

Help Center および File ExchangeLoops and Conditional Statements についてさらに検索

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by