Skip to main content
Logo image

Section 8.11 Practice Your Programming

Activity 8.6.

What Matlab programming language commands have you learned about?

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 8โˆ’4.5(xโˆ’sinโก(x))=0 using the bisection method.
To do so, plot the function f(x)=8โˆ’4.5(xโˆ’sinโก(x)) first to determine an interval [a,b] within which the solution is located. You thus need to choose a and b such that f(a) and f(b) have different signs (this can be done outside of your program).
Your program should now calculate a first estimate of a numerical solutions via x1=a+b2. Determine whether the true solution is between a and x1 or between x1 and b by looking at f(a), f(x1) and f(b), then replace a or b with x1 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.
Note: The Matlab command
f = @(x) 8 - 4.5*(x-sin(x));
defines the function f so that you can easily evaluate f(x) for different values of x.
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:
Rule 1: xn+1=xn2,yn+1=yn2
Rule 2: xn+1=xn2+14,yn+1=yn2+34
Rule 3: xn+1=xn2+12,yn+1=yn2
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 x1=0 and y1=0. 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 c=x+iy) belongs to the Mandelbrot set, one carries out the following calculation repeatedly (this is called iteration), starting with z=0:
znew=z2+c
Thus, the following calculations are performed:
z1=c;z2=z12+c=c2+c;z3=z22+c=(c2+c)2+c;...
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 z100 we thus ask the following:
Is z100 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 c=x+iโˆ—y (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 z=z2+c 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.