Lab Exam Solution 2018S2.ipynb.txt - "cells"cell_type"markdown"metadata"source Lab Exam Solution(2018

Lab Exam Solution 2018S2.ipynb.txt -...

This preview shows page 1 - 6 out of 19 pages.

{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "## Lab Exam Solution (2018 S2)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "$\\hspace{2.4cm}$NAME: \n", "\n", "STUDENT NUMBER: " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q1" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Q1 (a) " ] }, { "cell_type": "code", "execution_count": 16, "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ "\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/Project.toml`\n", " \u001b[90m [1fd47b50]\u001b[39m\u001b[92m + QuadGK v2.0.3\u001b[39m\n", "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m `~/.julia/Manifest.toml`\n", " \u001b[90m [864edb3b]\u001b[39m\u001b[92m + DataStructures v0.15.0\u001b[39m\n", " \u001b[90m [bac558e1]\u001b[39m\u001b[92m + OrderedCollections v1.0.2\u001b[39m\n", " \u001b[90m [1fd47b50]\u001b[39m\u001b[92m + QuadGK v2.0.3\u001b[39m\n" ] } ], "source": [ "import Pkg\n", "Pkg.add(\"QuadGK\")" ] }, { "cell_type": "code", "execution_count": 18, "metadata": {},
Image of page 1

Subscribe to view the full document.

"outputs": [], "source": [ "import QuadGK.quadgk" ] }, { "cell_type": "code", "execution_count": 19, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "(1.5707963267948966, 1.3063771620025477e-8)" ] }, "execution_count": 19, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Area under the curve f(x)=cos^3(x) over [0 pi/2] is the integral of f(x) from 0 to pi/2\n", "function f(x)\n", " sin(x)^2 # input your function in this line\n", "end\n", "quadgk(f,0,pi)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Thus, the area is 1.5707963267948966. NOTE: the accuracy is 1.3063771620025477e-8" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Q1 (b)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The region under the curve $y=\\sin^2(x)$ lies in the box\n", " $$0\\le x\\le \\pi, \\ 0\\le y\\le 1$$" ] }, { "cell_type": "code", "execution_count": 3, "metadata": {}, "outputs": [], "source": [ "N=1000000\n", "n=0.0\n", "for i in 1:N\n", " x=rand(1,2)\n", " if x[1,2]<=sin(pi*x[1,1])^2\n",
Image of page 2
" n=n+1\n", " end\n", "end" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "Using the formula\n", " $$\\mbox{Area under curve }\\approx\\frac{n}{N}\\times\\mbox{(Area of Box)}$$\n", "we have:" ] }, { "cell_type": "code", "execution_count": 4, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "1.570686371052021" ] }, "execution_count": 4, "metadata": {}, "output_type": "execute_result" } ], "source": [ "Area=(n/N)pi" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "So, the estimated area is 1.570686371052021." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "### Q2" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Q2 (a)" ] }, { "cell_type": "code", "execution_count": 8, "metadata": {}, "outputs": [], "source": [ "# Load package \"LinearAlgebra\"\n", "using LinearAlgebra"
Image of page 3

Subscribe to view the full document.

] }, { "cell_type": "code", "execution_count": 6, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7×7 Array{Float64,2}:\n", " 0.0 1.0 2.0 2.0 2.0 1.0 0.0\n", " 0.52 0.0 0.0 0.0 0.0 0.0 0.0\n", " 0.0 0.66 0.0 0.0 0.0 0.0 0.0\n", " 0.0 0.0 0.75 0.0 0.0 0.0 0.0\n", " 0.0 0.0 0.0 0.79 0.0 0.0 0.0\n", " 0.0 0.0 0.0 0.0 0.6 0.0 0.0\n", " 0.0 0.0 0.0 0.0 0.0 0.4 0.0" ] }, "execution_count": 6, "metadata": {}, "output_type": "execute_result" } ], "source": [ "# Input the Leslie matrix here\n", "A=zeros(7,7)\n", "A[1,:]=[0.0 1 2 2 2 1 0]\n", "A[2,1]=0.52\n", "A[3,2]=0.66\n", "A[4,3]=0.75\n", "A[5,4]=0.79\n", "A[6,5]=0.60\n", "A[7,6]=0.40\n", "A" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Q2 (b)" ] }, { "cell_type": "code", "execution_count": 9, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7-element Array{Complex{Float64},1}:\n", " 0.0 + 0.0im \n", " 1.2729866945536794 + 0.0im \n", " 0.09963480098642538 + 0.6799094016423889im \n", " 0.09963480098642538 - 0.6799094016423889im \n", " -0.5226295463527517 + 0.44966966779805895im\n", " -0.5226295463527517 - 0.44966966779805895im\n", " -0.4269972038210267 + 0.0im " ] }, "execution_count": 9,
Image of page 4
"metadata": {}, "output_type": "execute_result" } ], "source": [ "eigvals(A)" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "As the largest eigenvalue ($\\approx$1.27299) is greater than 1,\n", "the population is likely to survive." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Q2 (c)" ] }, { "cell_type": "code", "execution_count": 10, "metadata": {}, "outputs": [ { "data": { "text/plain": [ "7×7 Array{Float64,2}:\n", " 0.0 1.0 2.0 2.0 2.0 1.0 0.0\n", " 0.52 0.0 0.0 0.0 0.0 0.0 0.0\n", " 0.0 0.46 0.0 0.0 0.0 0.0 0.0\n", " 0.0 0.0 0.55 0.0 0.0 0.0 0.0\n", " 0.0 0.0 0.0 0.59 0.0 0.0 0.0\n", " 0.0 0.0 0.0 0.0 0.4 0.0 0.0\n", " 0.0 0.0 0.0 0.0 0.0 0.2 0.0" ] }, "execution_count": 10,
Image of page 5

Subscribe to view the full document.

Image of page 6
  • Spring '12
  • Boman
  • 1984, #, $0, Julia, 001b

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes