User Tools

Site Tools


gibson:teaching:fall-2013:math445:hw5

Math 445 HW5

This homework is meant as review for the second-chance exam to be held Nov 26. It is due Thursday Nov 14th in lecture.

Review topics

  • fprintf: formatted printing function
  • plotting: plot, contour, surf, mesh, meshgrid
  • loops: for and while
  • anonymous functions: defining and using
  • solving equations: with fsolve or newtonsearch
  • monte carlo simulation
  • log-linear relationships: semilogy, semilogx, loglog
  • index operations

Problem 1: Find all real-valued solutions of the equation $x^4 - 3x^2 + 2x -4 = 0$ by plotting $y = x^4 - 3x^2 + 2x -4$ versus $x$, estimating the values of $x$ where $y(x) = 0$, and then solving the equation numerically using those estimates as initial guesses. Turn in your plot, your matlab code, and the numerical solutions to the equation.

Problem 2: Define a rightshift function using Matlab's anonymous function facility that shifts the elements of a row vector one step to the right, wrapping the last element around to the first position. For example rightshift([1 2 3 4]) should return [4 1 2 3].

Problem 3: Write Matlab code to estimate the probability of drawing four-of-a-kind from a randomly shuffled 52 card deck. Turn in your code and your estimated probability.

Problem 4: Make a 3d plot of $f$ versus $x,y$ for the function $f(x,y) = 3 e^{-(x^2/2 + xy + 2 y^2)}$ for $-3 \le x \le $ and $-3 \le y \le 3$, on a mesh with $Delta x = \Delta y = 0.1$. Use Matlab's meshgrid function.

Problem 5: Print $e$ to five digits accuracy. (Hint: You get $e$ in Matlab from exp(1)).

Problem 6: Write an isPrime(n) function that returns 1 (true) if n is prime and 0 (false) for a composite. Don't worry about efficiency, just loop over 2 and the odd integers less than or equal to $\sqrt{n}$, and return 0 if any divide n evenly, and 1 if not. (You might have already done this as an optional problem for exam 1 prep.)

Problem 7: Write Matlab code that will compute the first 20 prime numbers, using your isPrime function from problem 6.

Problem 8: Deduce the functional relationship between $y$ and $x$ for the data shown in this plot

Bonus: Write an primes(N) function that returns all primes less than or equal to N using the Sieve of Eratosthenes algorithm.

gibson/teaching/fall-2013/math445/hw5.txt · Last modified: 2013/11/14 12:58 by gibson