Introduction to Mathematica SS 2025.

Solve the following three exercises. Write short comments to illustrate the main steps in the code.

Exercise 1. A Wimbledon simulation.

In tennis, a player wins a game if s/he has at least four points and two points more than the opponent. For instance, if the first score refers to player A who is serving and the second to the opponent, at 3-0 the game continues; at 4-1, player A wins; at 5-4 the game continues; at 6-4 player A wins, etc. Imagine now player A is serving, and s/he scores a point with probability x when s/he is serving (i.e. makes a point 100*x% of times). Realize many (eg 1000) random simulations of a game, and estimate the probability  p(x) for player A to win as a function of x. Draw a plot of p(x) for x between 0 and 1.

Exercise 2. MonteCarlo integration.

Define the function f (x) as below . Plot it in the interval (0, 3). Integrate f(x) analytically inside this range. Find numerically the maximum f_max of f(x) within the range (0,3). Write a code that generate 10^N random points uniformly distributed  in the rectangular region defined by x between (0,3) and y betwen 0 and f_max and counts how many points are below f(x). Plot the points below f(x) and the function itself for a couple values of N, e.g. N=2 and N=4 or create a Manipulate command showing the plot when varying N. The ratio of points below f(x) and the total number of random points 10^N, multiplied by the total area of the rectangular region, should approximate the exact integral of f(x) between 0 and 3 (MonteCarlo integration). Plot the result of the MonteCarlo integration as a function of N and show graphically that it converges to the  true value for N increasing from 1 to 5 or more.

Clear[f];f[x_]=Sin[x]^2*Exp[-x];

Exercise 3. Classical physics.

Solve the differential equation for the motion of a projectile launched from the surface of a spherical planet (without atmosphere) of mass M and radius R. The equations should be solved for the coordinates x  and y with the origin at the center of the planet. Stop the integration when the projectile hits the ground (you can use the command WhenEvent inside NDSolve). Assign as initial conditions on the surface  an initial angle θ (wrt the local horizontal), and an initial velocity v0. Plot the planet and the path. Vary θ between 30 and 90 degrees in steps of, e.g., 10 degrees, and use Manipulate to show the various trajectories.  Use the constants defined below.
Hint: if you use WhenEvent, and r[t] is the position vector, you might use this syntax to stop the integration when hitting the ground:
WhenEvent[Norm[r[t]]<R ,{tfin=t,”StopIntegration”}]
then the integration stops and tfin is the stopping time needed to draw the plots.

(*Constants*)
gG=6.67430*10^-11; (*gravitational constant[m^3/kg/s^2]*)
mM=5.972*10^24;     (*mass of planet (e.g.,Earth)[kg]*)
rR=6.371*10^6;      (*radius of planet (e.g.,Earth)[m]*)

(*Initial velocity, angle, and position*)
v0=8000;           (*initial speed[m/s]*)
theta=45 Degree;   (*launch angle from local horizontal*)
r0={R,0}; (* defines a vector in Cartesian coordinates,pointing from center of planet to launch point on the surface*)