Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Create variables x 1 and x 2 , setting them equal to 0 and 5 , respectively. We will use a loop to update the
Create variables x and x setting them equal to and respectively. We will use a loop to update the values
of these variables to close in on to the solution.
Create a variable named val setting it equal to the value you would obtain by plugging x into the function f
Create a variable named val setting it equal to the value you would obtain by plugging x into the function f
Notice that we can see from the plot that val should be negative and val should be positive.
Create a variable named n and set it equal to We will use this variable to count the number of iterations
required for the algorithm to converge to a solution.
Use a loop to iteratively update the values of the four variables above as described below. This loop should
continue to execute until the absolute value of val and val are both less than Each time the loop
executes, perform the following steps:
Increment n
Create a variable named newx that is equal to the average of x and x
Create a variable named newval that is equal to fnewx
If newval is negative, then set x to newx and set val to newval. Otherwise, set x to newx
and set val to newval.
When the loop is finished executing, x and x should be very near each other, and near to the solution. Take
the average of the two values to be the approximation for the solution. Print your result with a message as
shown below, with the xxxx strings replaced with the appropriate values. Round the approximate solution to
seven decimal places.
The approximate solution is x xxxx
The algorithm took xxxx iterations to converge
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started