Question

In: Computer Science

In class we learned that, if terms added in a series continually decrease in size, the...

In class we learned that, if terms added in a series continually decrease in size, the terms will eventually be too small to be stored by MATLAB. When a number is too small to be stored, we learned that such a situation is called underflow; in this case, MATLAB will just store it as 0. We also learned that a program computing such a series can terminate the loop when the next term to be added is equal to 0 (because of underflow).

It also turns out that, when each term is added to the running sum in a series, the next term to be added will eventually be so small in size relative to the running sum, that this next term will not even be picked up by the running sum- even before the terms reach underflow. In other words, even though additional terms are added, the running sum will eventually stop changing - even before underflow for the next term is reached.

In the script below, where indicated, write a segment of script that uses one while loop (not vectors) to approximate sin(3.76) using the series, sin(x) approximately equals Sum i = 0 to n ((-1)^i x^(2i+1))/(2i+1)!, for the largest n possible. In other words, add terms to the series just until running sum stops changing. Furthermore,

i) Store the running sum into variable, runsum.

ii) Note that you will need to keep track of a current running sum (runsum) and the previous sum ( call it sumold), which contains one less term. You will need to compare these in the condition of a while loop, which will terminate once they are the same The final value of runsum will contain up to and include the first term that doesn't change runsum, i.e. when runsum = sumold. Note that you will need to initialize runsum and sumold with different values to start the loop.

iii) Also, define variable, isum, to be a counter that will keep track of the number of terms added. The final value of isum will be the number of terms in the final runsum.

Use only arithmetic operations and no built in functions (nor ^). Use only one for loop.

format long

% write the code for part(a) here

%display final variables

runsum

isum

Solutions

Expert Solution

The MATLAB script for the above problem is:

x = 3.76; % x initialised to 3.76
runsum = 0; % all variables initialised to 0
isum = 0; 
sumold = -1; % initialised to -1 so as to pass the while loop
f = 1; % factorial value is initialised to 1
while runsum ~= sumold
    sumold = runsum; % store new value in old value
    runsum = runsum + ((-1)^isum * x^(2*isum + 1))/f; % update runsum
    f = f * (2*isum + 2) * (2*isum + 3); % update factorial value
    isum = isum + 1; % isum updated after each iteration
end
disp(runsum)
disp(isum)

The sample of the code is:

The output sample of the code is:

The file name is hidden due to the guidelines. However, if you have any queries in the above code comment down. I will help you out as soon as possible.


Related Solutions

Chose the right answer a) If we decrease the size of a quantum dot that contains...
Chose the right answer a) If we decrease the size of a quantum dot that contains an electron, what happens to the energy levels? 1-Decrease 2-remain the same 3-increase b) If two quantum states are degenerate, which is true? 1- They have identical momenta. 2- They have identical wave functions 3- They have identical energies C) Which is true about the electron in a hydrogen atom? 1-Its total energy and potential energy are negative 2-Its kinetic energy and potential energy...
part a. In class we learned that the debugger, and _________ are effective in debugging C++...
part a. In class we learned that the debugger, and _________ are effective in debugging C++ programs. a. a logic analyzer b. print statements c. code reviews d. network analyzers part b. A ‘side-effect’ is a. code written in a memory-efficient way b. a style of writing C++ statements that is risky. c. code written so that more than one thing is being done in one statement. d. (a) and (b) e. (b) and (c) f. none of the above...
4. Using the terms you have learned in this class, explain what is meant by the...
4. Using the terms you have learned in this class, explain what is meant by the following statement: (8 points) “All viruses mutate.  However, a mutated virus is not necessarily more dangerous.” Include answers to the following questions: What is a mutation? Are most mutations helpful or harmful to a virus?  Why? What would make a virus “more dangerous” to humans? 5. Based on all the topics you have discussed Biol002 this quarter, what is one way the Corona virus pandemic will...
We have learned about conditioning in class. There are countless ways that we encounter condition in...
We have learned about conditioning in class. There are countless ways that we encounter condition in everyday life. This assignment requires that you examine conditioning (either classical or operant) as you’ve experienced it. Provide five examples of conditioning from your own life with a detailed description of stimulus/response and reinforcement patterns.​
In class, we learned that ”most” of the sun’s radiation happens in the band 0.2 ?...
In class, we learned that ”most” of the sun’s radiation happens in the band 0.2 ? ? ? 3µm, and Stephen asked what ”most” means. What proportion of solar radiation (for a black body sun) falls in the above wavelength range. Similarly, what proportion of the Earth’s radiation (black body Earth at 290K) occurs in the wavelength band 4 ? ? ? 40µm? What wavelength range contains the middle 95% of the sun’s radiation?
In class we learned that investors should always hold a portfolio that is on the efficient...
In class we learned that investors should always hold a portfolio that is on the efficient frontier. In reality investors often hold a portfolio that is not on the efficient frontier. One reason for this is called home bias. Home bias refers to the fact that investors tend to have larger portfolio weights on assets that are located in the country in which they live. So, American investors tend to have a larger portfolio weight on American firms than is...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the...
In class, we learned that the running time of Karatsuba’s algorithm can be expressed as the recurrence T (n) = 3T (n/2) + n. We then used the substitution method to solve the recurrence. When using the substitution method, it seems difficult to make a guess about the upper bound on the running time. However, if we use the recursion tree method, it would be a lot easier. In this question, you are asked to solve this recurrence using the...
1. We have learned in this class that the steady condition is given as: sf (k)...
1. We have learned in this class that the steady condition is given as: sf (k) = (n + S + g)k . Use this condition for steady state to find the steady state (long run) equilibrium values of capital per effective worker, k* , output per effective worker, y* ; consumption per effective worker, c* ; and investment per effective worker, i* , for this economy. 2. If the United States adopts policies that permanently decrease savings and investment,...
Insurance. As we learned this term, healthcare insurance is one example of an entire class of...
Insurance. As we learned this term, healthcare insurance is one example of an entire class of insurance products that is provided by the government. The government tends to underwrite insurance programs when they are at a high risk of failure, for whatever the reason. Carefully explain four reasons why the government underwrites healthcare insurance in the U.S. In your answer, use as many examples from the US healthcare industry as you can.
One of the lessons we have learned in class is that raising the minimum wage would...
One of the lessons we have learned in class is that raising the minimum wage would put more employment pressure on competitively disadvantaged, unskilled workers. Furthermore, many older Americans do not have enough savings or pension for them to retire. To help resolve this problem, the government has proposed that it will provide subsidies to companies hiring workers aged 65+. Given what you have learned about subsidies, who (employer or employee) in reality is receiving the subsidies? Start your analysis...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT