Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 6

Preface

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.

Laboratory work #1 Generating a discrete random variable


Tasks of work
1. Get acquainted with the main mathematical software. 2. Using different software generate a discrete random variable. 3. Find the tendency of the difference between given probability and computed frequency as the number of experiments increases.

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.

MathCAD by MathSoft Inc.


MathCAD is a software for symbolic and numerical mathematical calculations. Main features of this system can be found in a MathCADs Help Index: Built-in Variables Computational features Contour plots Differential Equations Document layout Document management Electronic Books Equations, creating Equations, editing FFT Files Fonts

Mathematica by Wolfram Research Inc.


Probably most complicated mathematical software tool among similar programs. To learn more about Mathematica you can use Getting Started with Mathematica in the Help option, which include: Evaluating Expressions; Using Cell Styles; Formatting and Unformatting Cells; Animating Graphics; Using Cell Groups; Choosing a New Viewpoint for 3D Graphics. The graphical possibilities in Mathematica are extremely wide. For example, it is easy to plot a 3D graph, as a knot, which is shown on right side:

MATLAB by MathWorks Inc


What Is MATLAB? MATLAB is a high-performance language for technical computing. It integrates computation, visualization, and programming in an easy-to-use environment where problems and solutions are expressed in familiar mathematical notation. Typical uses include: Math and computation Algorithm development Modelling, simulation, and prototyping Data analysis, exploration, and visualization Scientific and engineering graphics Application development, including Graphical User Interface building MATLAB is an interactive system whose basic data element is an array that does not require dimensioning. This allows you to solve many technical computing problems, especially those with matrix and vector formulations, in a fraction of the time it would take to write a program in a scalar non-interactive language such as C or Fortran. The name MATLAB stands for matrix laboratory. MATLAB was originally written to provide easy access to matrix software developed by the LINPACK and EISPACK projects, which together represent the state-of-the-art in software for matrix computation. MATLAB has evolved over a period of years with input from many users. In university environments, it is the standard instructional tool for introductory and advanced courses in mathematics, engineering, and science. In industry, MATLAB is the tool of choice for highproductivity research, development, and analysis. MATLAB features a family of application-specific solutions called toolboxes. Very important to most users of MATLAB, toolboxes allow you to learn and apply specialized technology. Toolboxes are comprehensive collections of MATLAB functions (M-files) that extend the MATLAB environment to solve particular classes of problems. Areas in which toolboxes are available include signal processing, control systems, neural networks, fuzzy logic, wavelets, simulation, and many others.

Data types and Mathematical functions in Microsoft Visual C+ +.


Type Float Double long double 4 8 10 Bytes Range of Values 3.4E +/- 38 1.7E +/- 308 1.2E +/- 4932 Number of digits 7 digits 15 digits 19 digits

Mathematical Functions in Microsoft Visual C++


Float acosf( float ); asinf( float ); atanf( float ); tan2f( float , float ); cosf( float ); sinf( float ); tanf( float ); coshf( float ); sinhf( float ); tanhf( float ); expf( float ); logf( float ); log10f( float ); powf( float , float ); sqrtf( float ); Double Trigonometric functions acos(double); asin(double); atan(double); atan2(double, double); cos(double); sin(double); tan(double); Hyperbolic functions cosh(double); sinh(double ); tanh(double); Long double acosl(long double); asinl(long double); atanl(long double); atan2l(long double, long double); cosl(long double); sinl(long double); tanl(long double); coshl(long double); sinhl(long double); tanhl(long double);

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.

The Task of the Work


Let four horses (A, B, C, and D) have raced many times. It is estimated that A wins 30 percent of the time, B 40 percent of the time, C 20 percent of the time, and D 10 percent of the time. We can have our computer carry out one race as follows: Choose a random number x. If x < 0.3 then we say that A won. If 0.3 x < 0.7 then B wins. If 0.7 x < 0.9 then C wins. Finally, if 0.9 x then D wins. The program GeneralSimulation uses this method to simulate repetitions of an arbitrary experiment with a finite number of outcomes occurring with known probabilities. Make and test the program in BASIC first ( file GenSim.bas for Qbasic ).

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 ?

11. ALT F X - to exit BASIC for the next step.

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

Input and Output in C++


C++ language contains special classes, called streams, for input and output operations. Only two operators needed for most tasks to use streams, that is: << put something into a stream >> get something from a stream For most tasks Standart C++ library contains a lot of predefined streams. most used ones are console and file input-output. Cin Predefined object for input from standart console (keyboard by default) Cout Predefined object for output to standart console (by default display) ofstream Stream class for input from file ifstream Stream class for output to file
Example 1 - using streams in C++ program //this program generates tabulated values of function //and writes them in "file_inp.dat" file #include<iostream.h> #include<fstream.h> #include<math.h> //the analytical function inline double ref_func(double x) { return x+exp(sin(5*x));} void main() { int N,i; double x, y, x_s, x_f, step_x;

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.

You might also like