Question

Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...

Self-check exercise: While-loops

The value of (π^2)/8 can be approximated by the series

sum of reciprocals of odd numbers

Write a script that evaluates this expression, ignoring all terms that are strictly smaller than .000001. Your script should display the number of terms summed and the sum. Use a while-loop. Think about the initialization of your variables and the order of computation carefully! In the loop body you need to calculate the value of a term only once.

We use the above series for approximating (π^2)/8 again but with a different stopping criterion. Let the sum of the first n terms be Tn. As n increases one expects the ratio Tn/Tn+1 to approach 1. Write a script to find the smallest n such that the ratio this ratio is greater than 0.9999. Display n and Tn.
Hint: Now you need to keep track of a current sum and the next sum in the loop.

Complete the script stepPyramidSkeleton.m to draw a step pyramid, as the one shown below.

a step pyramid

The base rectangle is L-by-H where H < L. Each step has the same height H. The next rectangle up is 2/3 the length of the rectangle below, and so forth. The top step must have a length no less than H.
Note: To draw a single rectangle, you can make use of the function DrawRect that we have defined for you in file DrawRect.m . To use this function, save this file to your MATLAB working directory and call it from your script with the syntax DrawRect(a,b,L,H,c) to draw a rectangle with lower-left corner at (a,b) a (horizontal) length L, a height H and color c which should be one of 'r', 'g', 'y'.How many rolls of a fair die does one need to accumulate at least 20 points? Of course one needs at least 4 rolls (as in 6 + 6 + 6 + 2 or 6 + 5 + 4 + 5) and at most 20 rolls (getting 20 1's in a row). However these sittuations are rather unlikely! Hence, we ask ourselves: what is the most likely number of times that one has to roll a die to get 20 points? Write a program to answer this question.

First write a fragment of code that simulates one experiment run, i.e. rolling the die several times until 20 points have been accumulated and counting how many times the die was rolled. Store this count in variable n

Test it.

Then insert this fragment in a loop to do many runs (say 10000). Do not forget to reset n to 0 at the beginning of the loop

Use an array counts to summarize the results, as follows. Start with counts being a vector of zeros of length 20. After each run increment component counts(n) by one if the run took n rolls of the die.

After running your program, sum(counts) should equal 10000 (the total number of runs) and[maxcount, maxn] = max(counts) will yield the maximum count and the value of n for which this count is obtained. maxn will be the most likely number of rolls of the die to get 20 points.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

k = 0 % number of terms summed so far

s = 0 %sum so far

term=1;

while term >=.000001

s=s+term;

end

disp(sprintf('sum is %f',s)) %printing the value

disp(sprintf(%d terms summed',k))


Easy way to do this:

term = 1; % value of the current term

k=1; % term number

s=0 % sum so far

while term>=0.000001

s=s+term;

k=k+1;

term= 1/(2*k-1)2

end

disp(sprintf('sum is %f',s)) %printing the value

disp(sprintf(%d terms summed',k-1))

Add a comment
Know the answer?
Add Answer to:
Self-check exercise: While-loops The value of (π^2)/8 can be approximated by the series Write a script...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a....

    Copy/Paste your script from 5b here. 4. Let's look at a more complicated script executable. a. Write the following as script6.sh and change its permissions to count for num in d do if num eq 100 then count (count+1) done echo The number 10 was found $count times What is the output? Run the script as ./script6.sh 10 20 30 10 40 20 50 10 60 <enter Summarize what the script does r using the read command. The following script...

  • Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values...

    Write VBA functions to calculate sin (x) using the Maclaurin arcsine series, and compare the values for sin-1(x) from your program to those given by the Excel spreadsheet function ASIN(x). The Maclaurin arcsine expansion is given by x 3x 6 40 (2n)! sin1(x)-2((2n+1) Note: This function by definition is only defined for-1 SxS1. When you write the code for calculating it, you will need to include code that assigns a value to it that reflects it is undefined for values...

  • Part 1: Using Idle Write a Python Script that Does the Following 1. At the top...

    Part 1: Using Idle Write a Python Script that Does the Following 1. At the top of your program, import the math library as in from math import * to make the functions in the math module available. Create a variable and assign into it a constant positive integer number of your choice. The number should be at most 10. 1 Suppose we call this variable x for this writeup document Try something like x = 4 2. Create another...

  • For this project, each part will be in its oun matlab script. You will be uploading a total 3 m f...

    For this project, each part will be in its oun matlab script. You will be uploading a total 3 m files. Be sure to make your variable names descriptive, and add comments regularly to describe what your code is doing and hou your code aligns with the assignment 1 Iterative Methods: Conjugate Gradient In most software applications, row reduction is rarely used to solve a linear system Ar-b instead, an iterative algorithm like the one presented below is used. 1.1...

  • This code must be in SQL - Run the script provided with the assignment. This will...

    This code must be in SQL - Run the script provided with the assignment. This will create the tables used for this assignment. Write the necessary SQL commands to perform the required actions. Run the queries to obtain results from the database. (Make sure all columns returned have proper headings.) 3 2 In a single row show the following values: how many unique manufacturers are in the products table. What is the most expensive price per unit in the products...

  • Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment...

    Please write a BASH Script in LINUX that... 1. is actually executable 2. has a comment to tell us what you did, why and how. 3. allows a user to enter their name and a number between 1 than 100 (this must be prompted so the user knows what to do) 4. creates a random number between 1 and 100 for you to guess. The command to create a random number is shown below. (if you find a better one...use...

  • Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which...

    Q1. Write a recursive function in C++ void printNumberedChars(string str, int i=0) { ... } which prints each character of the given string str on a new line, with each line numbered 1, 2, 3, …. For example calling the function with printNumberedChars("hello"); should output the following: 1. h 2. e 3. l 4. l 5. o Q2. Write a recursive function in C++ int sumArray(const int* arr, unsigned int size) { ... } which takes an array of integers,...

  • Objective : Write a C Shell script which copies all files(*.java and *.class) from your home dire...

    Objective : Write a C Shell script which copies all files(*.java and *.class) from your home directory to a new one, and Analyze the new directory information such as number of files, user permissions, and disk usage. Sample Output:                                                    << CS Directory Analysis >>      Date:   ============================================================ Current Directory: /home/tomss New Directory Created : /home/tomss/pgm01 File information Total Number of files : 22 files Directory files:   0 files Plain text files:   10 files File have read permissions: 3 files File have...

  • LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals clear all c...

    LMS project Using the notes discussed in class: Implementing the LMS Algorithm First generate some signals clear all close al1: Generate signals for testing the LMS Algorithm 1000 Fs Sampling frequency Sample time 1/Fs 10000: = L Length of signal S Time vector (0:L-1) *T ; Sum of a 50 Hz sinusoid and a 120 Hz sinusoid 0.7 sin (2*pi*50*t); inuside X d+ 10 randn (size (t)); Sinusoids 5O0000000L plus noise fiqure (1) plot (Fs*t (1:150),x (1:1500)) title('Signal Corrupted with...

  • For this lab you will write a Java program that plays the dice game High-Low. In...

    For this lab you will write a Java program that plays the dice game High-Low. In this game a player places a bet on whether the sum of two dice will come up High (totaling 8 or higher), Low (totaling 6 or less) or Sevens (totaling exactly 7). If the player wins, they receive a payout based on the schedule given in the table below: Choice Payout ------ ------ High 1 x Wager Low 1 x Wager Sevens 4 x...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT