Activity 8.7.
Write a Matlab program in a script file that prompts the user to enter a positive integer, n, and displays all prime numbers less than or equal to n.
Challenge: A twin prime is a pair of prime numbers such that the difference between them is 2 (for example, 17 and 19). Write a Matlab program in a script file that finds all twin primes between 10 and 500. The program should display the results in a two-column table in which each row is a twin prime.
Please enter your solution (ONE per group - please identify group number) into the textbox.
Activity 8.8.
Write a Matlab program that finds the solution of the equation
using the bisection method.
To do so, plot the function
first to determine an interval
within which the solution is located. You thus need to choose
and
such that
and
have different signs (this can be done outside of your program).
Your program should now calculate a first estimate of a numerical solutions via
Determine whether the true solution is between
and
or between
and
by looking at
and
then replace
or
with
to obtain a new (shorter) interval that contains the true solution and start over. Come up with a reasonable way to stop this process once your approximation is โgood enoughโ and display the solution.
f = @(x) 8 - 4.5*(x-sin(x));
defines the function
so that you can easily evaluate
for different values of
Please enter your solution (ONE per group - please identify group number) into the textbox.
Activity 8.9.
The Sierpinski triangle can be displayed in Matlab by plotting points iteratively according to one of the following three rules which are selected randomly with equal probability:
Write a Matlab program in a script file that calculates the x and y vectors and then plots y versus x as individual points (use
plot(x,y,โ^โ)
). Start with
and
Run the program four times with 10, 100, 1000 and 10000 iterations.
Please enter your solution (ONE per group - please identify group number) into the textbox.
Activity 8.10.
Challenge: Plot the Mandelbrot set.
The Mandelbrot set is a subset of the complex plane (or the xy-plane) determined as follows: To check whether a point (x,y) (corresponding to the complex number
) belongs to the Mandelbrot set, one carries out the following calculation repeatedly (this is called iteration), starting with
Thus, the following calculations are performed:
To find out whether the point c belongs to the Mandelbrot set one checks whether the sequence of z-values generated above stays inside the circle of radius 2 or not. Of course weโd have to look at all (infinitely many) z-values to be sure, but performing the first 100 iterations gives us a pretty good idea. After having calculated
we thus ask the following:
Is
inside the circle of radius 2, i.e. is its absolute value less than 2? If the answer is:
yes, then color the point c black (these are the points c that belong to the Mandelbrot set)
no, then color the point c white (or another color of your choice)
A good rectangle of c-values to look at is the one with real parts between -2.25 and 1.75 and imaginary parts between -1.5 and 1.5. You want to divide this viewing rectangle up into individual โpixelsโ, something like 800 by 600. The best way to do so is to have two 800x600 arrays - one for the x-values and one for the y-values. You can then simply create all c-values that you are interested in via
(Matlab knows complex numbers!). You can โfillโ these arrays by hand (using a loop), or by using the Matlab
meshgrid
command.
Next you carry out the iteration: You can initialize z as an array of zeros of the same size as your array of c-values, then use a for loop to update
100 times.
Now assign the value 1 to those points that have escaped the circle of radius 2, and the value 2 to those that have remained within (these are the points in the Mandelbrot set). You can display this image using the Matlab
image
command. Youโll have to define a colormap (one with exactly two colors in it), for example via
blackwhite = [1 1 1;0 0 0];
colormap(blackwhite)
if you do want to use the two colors white (rgb 1 1 1) and black (rgb 0 0 0).
Please enter your solution (ONE per group - please identify group number) into the textbox.