Professional Documents
Culture Documents
Laboratory Work Generating A Discrete Random Variable
Laboratory Work Generating A Discrete Random Variable
Every student should do his/her own option of the work. The data for each option are given inside the notes for the appropriate lab. To choose your option number, divide your number in the group register by 5 and find the remainder, which will be your option number. If it is 0, then your option is 5.
Execution order
1. Read the introductory remarks about main mathematic software. Maple V by Waterloo Maple Inc.
Maple is a language for symbolic mathematical calculation. Maple statements are input after the > prompt. Every Maple statement must end with a semicolon; (or a colon: if the result is not to be printed). Until Maple receives a (semi)colon it will neither process the statement nor accept a new statement. If the (semi)colon is forgotten, no further commands will work until a (semi)colon is typed. To leave Maple, type the word quit. Some important Maple functions are expand, simplify, and normal, which are useful for simplifying expressions; evalf, for evaluating to floating-point; solve, for solving equations; int and diff, for integration and differentiation; series, for Taylor or Laurent series; and plot, for plotting functions. Other useful Maple functions are: array, coeff, collect, convert, degree, denom, evalc, ifactor, limit, map, normal numer, op, product, simplify, subs, sum, table, and type. These commands are the more commonly used ones; this is by no means a complete list of Maple functions. Maple also has many mathematical functions built-in, such as sin, cos, tan, exp, ln, GAMMA, Zeta, and binomial. The help function can be used to find out more about any Maple function. For instance, to find out about the series command, type series. The Maple assignment operator is :=. The symbols ", "", and """ indicate the previous, second previous, and third previous expressions in a Maple session. Maple procedures are created using the proc and end delimiters. Control structures include for and if.
Exponential and logarithmic functions exp(double); expl(long double); log(double); logl(long double); log10(double); log10l(long double); Power functions pow(double, double); sqrt(double); powl(long double, double); sqrtl(long double); long
Nearest integer, absolute value, and remainder functions ceilf( float ); nearest ceil(double ); ceill(long double ); integer, greater than argument floorf( float ); floor(double); floorl(long double); fmodf( float , float ); fmod(double, double); fmodl(long double, long double); hypotf(float, float); hypot(double, double); hypotl(long double, long double);
2. Open the available mathematical software ( Qbasic, C++, MathCad ) and test it. 3. Find the archive in the local network disk suppose, X:\BRIGADES\Br1\Prob\4_labProb 4. Copy it to your work disk C:\temp\Prob ( in any case, dont work from any remote disk ! ) 5. UnRAR all the archives in the given directory (folder). Drag the windows to put them one above the other in the screen. 6. Read the problem stated below.
7. To make BASIC program work click bas1lab.bat Then F5 to run it. Answer questions and see the results. 8. Press ENTER for return to BASIC environment. 9. Use this program to simulate the outcomes of n races.
Running for n = 10 we find the probabilities and frequencies of winning races for all four horses. A larger number of races would be necessary to have better agreement with the past experience. Therefore we must run the program to simulate 1000 races with our four horses. The possible outcome may be: A won 29.8 percent of the time, B 39.4 percent, C 19.5 percent, and D 11.3 percent of the time. Chose your own option and get data for the probabilities of horse-races from the table: Horse / opt# P(A wins) P(B wins) P(C wins) P(D wins) 1 0.3 0.4 0.1 0.2 2 0.2 0.2 0.4 0.2 3 0.1 0.2 0.3 0.4 4 0.5 0.2 0.2 0.1 5 0.6 0.1 0.2 0.1
Accordingly change the data for probability in GenSim.bas, and make 10, 100, 1000 and 10000 races. Note the modulo-differences and relative differences (%) between frequencies and probabilities.
10. Explain the tendency as the number of experiments increases and answer the following questions.
a) What is the sum taken along each column of the above table equal to? Why? b) How many % is the difference between frequencies and probabilities ? %=100*abs(freq-prob)/prob. c) Make the table: N=10 N=100 N=1000 N=10000 Horse prob freq % freq % freq % freq % A (1) B (2) C (3) D (4) for all the horses A (1), B (2), and say, will this % decrease with the increasing of the number of experiments ?
12. Find MS Visual Studio and run MS Visual C++ v6 in it. Click File / New. Give Project name and click Win32 Console Application. Choose: "Empty Project", Finish, Ok. 13. Try to convert GenSim.bas into C++ and make it run ( Ctrl-F7 than Ctrl-F5 ). 14. To teach yourself C++ unrar : "CPPtutorial.rar" ( use "intro.htm" ) and / or "visual_CPP_6_in_21d.rar" ( use "INDEX.HTM" ). (Not all the Figures are available, sorry !). To get online help press F1 in MS C++ environment.
How to start your own MS VC++ 6.0 program short instruction
Run the MS VC++ 1. File -> New -> Projects -> Choose Win 32 Application -> Enter the project name in the window for example proj_1 <OK> 2. File -> New -> Files -> C++ Source File -> File name: proj_1 <OK> 3. Enter your C++ program; 4. File -> Save ( Ctrl-S ); 5. Build -> Compile proj_1 ( Ctrl-F7 ); 6. The compiler checks your program and informs about errors, for example proj_1.cpp(17) : error C2065: coutr : undeclared identifier, Double click on error message to get to wrong line; revise the wrong line. 7. If compiler reports: proj_1.obj - 0 error(s), 0 warning(s) , Build -> Build proj_1.exe ( F7 ) Successful linking message is : Linking... proj_1.exe - 0 error(s), 0 warning(s) 8. Execute your file: Build -> Execute proj_1.exe ( F5 ) or click ! button
ofstream outFILE; //definition of stream object "outFILE" for output //initialization of the output file stream object outFILE.open("file_inp.dat");//open a file "file_inp.dat" for writing cout << "Enter the x coordinate of the first point x_s = ";//display a query to user cin >> x_s; //input parameters from keyboard cout << "Enter the x coordinate of the last point x_f = "; cin >> x_f; cout << "Enter the number of intervals N = "; cin >> N; step_x=(x_f-x_s)/N; //calc length of the segment outFILE << N <<" \n";//write number of points to file in separate line outFILE << fixed; //set output of real numbers to "fixed" format for(i=0; i<=N; i++) { x=x_s+step_x*i; y=ref_func(x); outFILE << x << "\t" << y << "\n"; //where \t is the tabulation, \n is new line (similar to C programs) }
Example 2 of C++ program for extraction the real roots of a quadratic equation ax2+bx+c=0 with real coefficients a, b and c. #include<iostream.h> #include<math.h> #pragma hdrstop // The extraction of the real roots of a quadratic equation with real coefficients // illustrates the point. The equation ax^2+bx+c=0 possesses two solutions, x1 and // x2, calculate value of the ax^2+bx+c from given x. double Poly_2(double a, double b, double c, double x) { return a*x*x+b*x+c; } void main() { double a,b,c,d,x1,x2; //input initial data do { cout << Enter numbers a, b and c for polynom ax^2+bx+c: \n; cout << a= ; cin >> a; cout << b= ; cin >> b; cout << c= ; cin >> c; d=b*b-4.0*a*c; //calculate discriminant if (d<0) cout << \n d is negative !, try again\n; }while (d<0); //id d<0 there is no solution, try next init data x1=0.5*(-b+sqrt(d))/a; //calculate roots of the equation x2=0.5*(-b-sqrt(d))/a; cout<< Roots of equation << a << x^2+ << b << x+<< c <<\n; cout<< Roots of equation are: x1= << x1 << x2= << x2 <<\n; cout<< f(x1)= << Poly_2(a,b,c,x1) << f(x2)= << Poly_2(a,b,c,x2) <<\n; }
15. If you find the point 13 too hard for you, click "Open file" icon. Find and click "lab1!!.CPP" and run this full complete example in C++, or at least run the executable file "lab1!!.exe" and see the results. 16. Make the table as in step#10. Compare with the results of Basic program run. 17. Run and read the MathCAD file LabProb1.MCD. Perform Lab #1 with that file.