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. See the Mathematica notebook on solving the equations for 4th order RK method. .DO NOT USE a[1] or a[2] = 1/2. 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) = 2 x2 cos(4 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 .

The answer at the end of the integration is about 1.978940602164785990777661.

Hint: It is often helpful to test your program on simple differential equations (such as x' = 1 or x'=t or x'=x) as a part of the debugging process.
Once you have worked these simple cases, then try working the nonlinear differential equation given above for the assignment (with a small stepsize).
Also, check your coefficients to make sure that they satisfy the equations of condition and that you have assigned these correct values to the
variables or constants in your program properly. For example, a common error is to write something like:   a2 = 1/2;   when you meant to write
   a2 = 1.0/2.0;   so please be careful.  

Write down (in your output file or in a text file) any conclusions that you can make from these experiments (e.g., what happens as h is decreased?).

Solutions

Expert Solution

import java.io.*;
//import java.Math;
class differential
{
double dydx(double t, double x)
{
return (2*x*2*Math.cos(4*t));
}
  
// Finds value of y for a given x using step size h
// and initial value y0 at x0.
double rungeKutta(double x0, double y, double x, double h)
{
differential d1 = new differential();
// Count number of iterations using step size or
// step height h
int n = (int)((x - x0) / h);
//System.out.println(n);
double k1, k2, k3, k4, k5;
for (int i = 1; i <= n; i++)
{
// Apply Runge Kutta Formulas to find
// next value of y
k1 = h * (d1.dydx(x0, y));
k2 = h * (d1.dydx(x0 + 0.5 * h, y + 0.5 * k1));
k3 = h * (d1.dydx(x0 + 0.5 * h, y + 0.5 * k2));
k4 = h * (d1.dydx(x0 + h, y + k3));
  
// Update next value of y
y = y + (1.0 / 6.0) * (k1 + 2 * k2 + 2 * k3 + k4);
//System.out.println(y);
// Update next value of x
x0 = x0 + h;
}
return y;
}
}
class Main{
public static void main(String args[])
{
differential d2 = new differential();
double t = 0.0, y = 1, t1 = 2;
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.1));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.05));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.025 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.0125 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.00625 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.003125 ));
System.out.println("\nThe value of x at t is : "
+ d2.rungeKutta(t, y, t1, 0.0015625 ));
double sum=0;
sum+=d2.rungeKutta(t, y, t1, 0.1)+d2.rungeKutta(t, y, t1, 0.05)+d2.rungeKutta(t, y, t1, 0.025 )+d2.rungeKutta(t, y, t1, 0.0125 )+d2.rungeKutta(t, y, t1, 0.00625 )+d2.rungeKutta(t, y, t1, 0.003125 )+d2.rungeKutta(t, y, t1, 0.0015625 );
//System.out.println(sum);
}
}

As the value of h is decrease the value of x(t) will increase therefore they have inversally relationship.

If you found this answer helpful please give a thumbs up.


Related Solutions

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...
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.
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices,...
Computer Science: Data Structures and Algorithms Practice: Construct a C++ program that takes two SQUARE matrices, and multiplies them and produces a new matrix: Example: [Matrix A] * [Matrix B] = [Matrix C] (A 3x3 is the most preferred example to use) (The first two matrices, A and B can be fixed numbers, hard-coded into the program. as opposed to user input) Display Matrix C, also (cout etc.)
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...
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT