Using Python
Version 1. Write a program that uses a "while" loop to print the first 10 positive integers and to compute their sum. Print the sum after it is computed. Do the same with a "for" loop.
Version 2. Write a program to approximate the square root of a number. Recall that the square root of a number x is a number r such that r*r = x. Newton discovered that if one initial estimate of r is z then a better estimate is obtained by taking the average of z and x/z. The estimate can be improved by using this rule again and again until a satisfactory value is obtained (for example, when the difference between x and z*z becomes less than 0.000001 ).
Ask the user to give a value for x. Start with the estimate
z = 1.0 , then update z inside a while loop using the computation
z = ( z + x/z ) /2
until the difference (x - z**2) has an absolute value less than a given tolerance.
Let tolerance = 0.000001. Print to the screen the value found and compare it with the one returned by math.sqrt(x), that is, the square root function from the math module.
Note on Newton's method: If you have taken calculus, this is using Newton's method to find an approximation of the zeros for a function of the type f(z) = x - z*z, where x is fixed. Newton's method consists in following the equation of the tangent line at the current estimate and using the intersection with the x-axis as an improvement of the estimate. The derivative f'(z) = - 2z gives the slope of the tangent at the current z. Replacing f(z_new) by 0 we get the equation of the tangent line of the form
f(z_old) - 0 = f'(z_old) (z_old - z_new).
z_new = z_old - f(z_old)/f'(z_old)
or for the computer code the update is just
z = z - f(z)/ f'(z)
for the specific f(z) = x - z*z, we have z - f(z)/ f'(z) = (z -( x - z*z)/(-2z)) = z/2 + x/(2z)
which leads to the update statement in the form
z = ( z + x/z ) /2 ;
#VERSION 1
i = 1
sum=0
#while loop
while i <= 10:
print(i) #printing new number on each line
sum += i
i+=1
print("Sum of first 10 positive integers is= "+str(sum))
#for loop
i=1
sum=0
for i in range(1,11):
print(i)
sum+=i
i+=1
print("Sum of first 10 positive integers is= "+str(sum))
#VERSION 2
import math #importing the math lib to use math.sqrt()
x=float(input("Enter x ")) #taking input
z=1.0 #starting z with 1.0 acc. to question
tolerance = 0.000001 #fixing tolerance
while abs(x-z**2)>=tolerance: #looping till the difference
doesn't become less than tolerance
z = ( z + x/z ) /2
print("The value computed by our method
is
"+str(z))
print("The value computed by math.sqrt() function is
"+str(math.sqrt(x)))
Using Python Version 1. Write a program that uses a "while" loop to print the first...
a) Write a program that uses a while loop to print all divisors of a number supplied by the user. The program should also print the sum of all the divisors and whether the number the user entered is a prime number or not. Note: The definition of a divisor is a number that divides another evenly (i.e., without a remainder) and the definition of a prime number is a number whose only divisors are 1 and itself. b) Implement...
Using newton's method calculate to the first 3
iterations.
DO NOT WORRY ABOUT THE CODING OR ANYTHING. IHAVE
ALREADY COMPLETED THAT. ONLY HAND WRITTEN CALCULATIONS.
Foject Goals and Tasks Your goal is to implement Newton's Method in Java for various functions, using a for loop. See the last page of this document for help writing the code. Task 1: (a) Apply Newton's Method to the equation x2 - a = 0 to derive the following square-root algorithm (used by the...
clearvars
close all
clc
tol = 0.0001; % this is the tolerance for root identification
xold = 0.5; % this is the initial guess
test = 1; % this simply ensures we have a test value to enter the loop below.
%If we don't preallocate this, MATLAB will error when it trys to start the
%while loop below
k = 1; %this is the iteration counter. Similar to "test" we need to preallocate it
%to allow the while loop to...
Part 1 Encapsulate the following Python code from Section 7.5 in a function named my_sqrt that takes a as a parameter, chooses a starting value for x, and returns an estimate of the square root of a. while True: y = (x + a/x) / 2.0 if y == x: break x = y Part 2 Write a function named test_sqrt that prints a table like the following using a while loop, where "diff" is the absolute value of the...
1. Determine the root of function f(x)= x+2x-2r-1 by using Newton's method with x=0.8 and error, e=0.005. 2. Use Newton's method to approximate the root for f(x) = -x-1. Do calculation in 4 decimal points. Letx=1 and error, E=0.005. 3. Given 7x)=x-2x2+x-3 Use Newton's method to estimate the root at 4 decimal points. Take initial value, Xo4. 4. Find the root of f(x)=x2-9x+1 accurate to 3 decimal points. Use Newton's method with initial value, X=2
IN PYTHON Print out a table of powers. Use a for loop that goes from 101 to 112 inclusive. Print out r, r squared, r cubed, square root of r, and cube root of r. Use two decimal places for the roots, and zero decimal places for the squares and cubes, and 2 decimal places for the averages. Use commas in numbers 1000 and above. After the loop, print out the average of the r squared and r cubed. Use...
Please write in Language c using only the files stdio.h
and math.h
Suppose you wish to find the root r of a function f(x), that is,
the value r where f(r)=0. One method is to make an initial guess,
x0, compute the line tangent to f at x0, and find where the tangent
line intercepts the x-axis, x1. Then, use x1 as the second guess
and repeat this procedure n times until f(xn) approximately equals
0 and report xn as...
I'm learning how to write loops in Python 3+ but need help in solving this function with another function: def babylonian_square_root(N, estimate, precision): new_estimate = (estimate + (N / estimate)) / 2 # I need to complete this using this function close_enough(). def close_enough(x, y, maximum_allowable_difference): ''' Returns True if x and y are within maximum_allowable_difference of each other. ''' # Note: "maximum" implies that we should use <= , but for this to work mathematically we need to use...
Write a Python program (using a nested loop) to print out the following pattern (there is a space between the numbers next to each other). 1 2 1 3 2 1 4 3 2 1 5 4 3 2 1
LAB 2 APROXIMATING ZEROS OF FUNCTIONS USING NEWTON'S METHOD (Refer to section 3.8 of your textbook for details in the derivation of the method and sample problems) (NOTE: You can use Derive, MicrosoftMathematics or Mathematica or any other Computer Algebra System of your choice. Your final report must be clear and concise. You must also provide sufficient comments on your approach and the final results in a manner that will make your report clear and accessible to anyone who is...