How to ask a user for an input for once ?
Afficher commentaires plus anciens
I am new to MATLAB and I am attempting to make a code that asks the user to input a function and then estimates the integral using Monte Carlo Method.
My problem is when I ask to enter the function, it repeats asking for the function. I only want the user to enter the function only once.
Thanks in advance for any help!
This is my code :
under = 0;
nmax = 10000;
for i=1:nmax
x = rand;
y = rand;
prompt = 'Enter your function';
z = input(prompt);
if y <= z
under = under +1;
end
end
estimated_integration =(under/nmax);
3 commentaires
Adam
le 3 Fév 2020
Why do you have a for loop running 10000 times? That will cause your code to ask the user for input 10000 times.
Osama Al-Shalali
le 3 Fév 2020
Adam
le 4 Fév 2020
But you are asking the user to input a function every time and then running a test against that. Either you want that to happen 10000 times or if you don't then don't put it in the loop.
Réponse acceptée
Plus de réponses (1)
Osama Al-Shalali
le 5 Fév 2020
Catégories
En savoir plus sur Whos dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!