Question

In: Computer Science

Design and construct a computer program in one of the approved languages (C, C++, C#, Java,...

Design and construct a computer program in one of the approved languages (C, C++, C#, Java, Pascal, Python, etc.) that will illustrate the use of a fourth-order
explicit Runge-Kutta method of your own design. In other words, you will first have to solve the Runge-Kutta equations of condition for the coefficients
of a fourth-order Runge-Kutta method.


Then, you will use these coefficients in a computer program to solve the ordinary differential equation below.
Be sure to follow the documentation and programming style policies of the Computer Science Department.

The initial value problem to be solved is the following:

                          x'(t) = 3 x2 cos(5 t)

subject to the initial condition:  x(0) = 1.0   

Obtain a numerical solution to this problem over the range from t=0.0 to t=2.0

for seven different values of the stepsize,

h=0.1, 0.05 , 0.025 , 0.0125 , 0.00625 , 0.003125 , and 0.0015625 .

In other words, make seven runs with 20, 40, 80, 160, 320, 640, and 1280 steps, respectively.
For each run, print out the value of h, then a table of t and x, and then the error at t=2. You may use the following
very precise value for your "true answer" in order to compute the error at t=2:   0.753913186469598763502963347.

Solutions

Expert Solution

Python code:

import math

def dxdt(t, x): 
        return (3*(x**2)*math.cos(5*t))

# Finds value of x for a given t using step size h 
# and initial value x0 at t0. 
def rungeKutta(t0, x0, t, h): 
        # Count number of iterations using step size or 
        # step height h 
        n = (int)((t - t0)/h) 
        # Iterate for number of iterations 
        x = x0 
        for i in range(1, n + 1): 
                "Applx Runge Kutta Formulas to find nett value of x"
                k1 = h * dxdt(t0, x) 
                k2 = h * dxdt(t0 + 0.5 * h, x + 0.5 * k1) 
                k3 = h * dxdt(t0 + 0.5 * h, x + 0.5 * k2) 
                k4 = h * dxdt(t0 + h, x + k3) 

                # Update nett value of x 
                x = x + (1.0 / 6.0)*(k1 + 2 * k2 + 2 * k3 + k4) 

                # Update nett value of t 
                t0 = t0 + h 
        return x 

# Driver method 
t0 = 0.0
x = 1.0
t = 2.0
a = [0.1, 0.05 , 0.025 , 0.0125 , 0.00625 , 0.003125 , 0.0015625]
i = 1
for i in range(len(a)):
  print("The value of x at with step-size %.7f is: %.27f"%(a[i], rungeKutta(t0, x, t, a[i])))

print("Error at t=2 is: ",0.753913186469598763502963347-rungeKutta(t0, x, 2, 0.0015625))

The value of x for given step-sizes and the error at t = 2 is given in the program output below:

(Feel free to ask if you have any doubts and give an upvote if you got it. Thanks)


Related Solutions

Design and construct a computer program in one of the approved languages (C++) that will illustrate...
Design and construct a computer program in one of the approved languages (C++) that will illustrate the use of a fourth-order explicit Runge-Kutta method of your own design. In other words, you will first have to solve the Runge-Kutta equations of condition for the coefficients of a fourth-order Runge-Kutta method. See the Mathematica notebook on solving the equations for 4th order RK method. That notebook can be found at rk4Solution.nb . PLEASE DO NOT USE a[1] = 1/2 or a[2]...
Design, plan, test, and write a computer program in Java that asks the user to enter...
Design, plan, test, and write a computer program in Java that asks the user to enter 1 number and a String. You will display the first n characters of the string where n is the number entered. For example, if the user enters 3 and java then you will print jav. If they enter 5 and Halloween then you print Hallo. If the user enters a number less than 0 then set the number to 0. Assume the user will...
1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with...
1.) Many languages (e.g., C and Java) distinguish the character ’c’ from the string “c” with separate sets of quotation marks. Others (e.g., Python) use “c” for both single characters and strings of length one. Provide (and justify) one advantage and one disadvantage of Python’s approach. 2.The designers of Java distinguish the primitive types (scalars) from the reference types (all other types of values) in the language. Discuss the costs and benefits to the programmer of this decision 3.In Ruby,...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the...
Java, Python, and C++ are three of the most useful programming languages to learn. Compare the functionalities of all three programming languages. Why would you choose one language over another? Provide code examples demonstrating their usefulness in a real-world scenario.
1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages  ...
1.In C++, C#, Java. Discuss string operations functions, with examples. Then make Comparison of programming languages   2.String and StringBuffer (C#, Java) with examples.
Computer Science (C and Assembly Languages)    •   Assume there are two 32-bit variables in RAM...
Computer Science (C and Assembly Languages)    •   Assume there are two 32-bit variables in RAM memory called In and Out. Write C code that sets Out equal to In plus 2.    •   Assume there are two 32-bit variables in RAM memory called In and Out. Write assembly code that sets Out equal to In plus 2.    •   What are the three stack rules?    •   Assume B1 is a 32-bit unsigned global variable. We wish to write...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project...
Write a program in Java Design and implement simple matrix manipulation techniques program in java. Project Details: Your program should use 2D arrays to implement simple matrix operations. Your program should do the following: • Read the number of rows and columns of a matrix M1 from the user. Use an input validation loop to make sure the values are greater than 0. • Read the elements of M1 in row major order • Print M1 to the console; make...
Create a program in java with the following information: Design a program that uses an array...
Create a program in java with the following information: Design a program that uses an array with specified values to display the following: The lowest number in the array The highest number in the array The total of the numbers in the array The average of the numbers in the array Initialize an array with these specific 20 numbers: 26 45 56 12 78 74 39 22 5 90 87 32 28 11 93 62 79 53 22 51 example...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the...
(JAVA) Implementing a Program Design a program that prompts the user for twenty numbers. If the number is positive, then add the number to the current sum. If the number is negative, then subtract the sum by one. Implement just the main method. Assume that all libraries are imported, and class has been declared.
Language is Java Design and write a Java console program to estimate the number of syllables...
Language is Java Design and write a Java console program to estimate the number of syllables in an English word. Assume that the number of syllables is determined by vowels as follows. Each sequence of adjacent vowels (a, e, i, o, u, or y), except for a terminal e, is a syllable. However, the minimum number of syllables in an English word is one. The program should prompt for a word and respond with the estimated number of syllables in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT