06.09.2021 Views

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

First Semester in Numerical Analysis with Julia, 2020a

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

CHAPTER 1. INTRODUCTION 28<br />

Sometimes we are <strong>in</strong>terested <strong>in</strong> relative frequency histograms where the height of<br />

each b<strong>in</strong> is the relative frequency of the numbers <strong>in</strong> the b<strong>in</strong>. Add<strong>in</strong>g the option "density=true"<br />

outputs a relative frequency histogram:<br />

In [67]: y = randn(10^5);<br />

hist(y,50,density=true);<br />

Exercise 1.2-1: In <strong>Julia</strong> you can compute the factorial of a positive <strong>in</strong>teger n by the<br />

built-<strong>in</strong> function factorial(n). Write your own version of this function, called factorial2,<br />

us<strong>in</strong>g a for loop. Use the @time function to compare the execution time of your version and<br />

the built-<strong>in</strong> version of the factorial function.<br />

Exercise 1.2-2: Write a <strong>Julia</strong> code to estimate the value of π us<strong>in</strong>g the follow<strong>in</strong>g<br />

procedure: Place a circle of diameter one <strong>in</strong> the unit square. Generate 10,000 pairs of<br />

random numbers (u, v) from the unit square. Count the number of pairs (u, v) that fall <strong>in</strong>to

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!