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]...
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.
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...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads...
Bank Accounts in Java! Design and implement a Java program that does the following: 1) reads in the principle 2) reads in additional money deposited each year (treat this as a constant) 3) reads in years to grow, and 4) reads in interest rate And then finally prints out how much money they would have each year. See below for formatting. Enter the principle: XX Enter the annual addition: XX Enter the number of years to grow: XX Enter the...
Program should be written in Java b) The computer program should prompt the user (You are...
Program should be written in Java b) The computer program should prompt the user (You are the user) to enter the answers to the following questions: What is your city of birth? What is your favorite sport? If you could live anywhere in the world, where would you like to live? What is your dream vacation? Take this information and create a short paragraph about the user and output this paragraph. You may use the Scanner class and the System.out...
Design and implement a Java program that creates a GUI that will allow a customer to...
Design and implement a Java program that creates a GUI that will allow a customer to order pizza and other items from a Pizza Paarlor. The customer should be able to order a variety of items which are listed below. The GUI should allow the customer (viaJavaFX UI Controls - text areas, buttons, checkbox, radio button, etc.) to input the following information: Name of the customer First Name Last Name Phone number of the customer Type of food being order...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT