C++,Write a program that generates the (x,y) pairs for a Euler’s extrapolation having the initial condition (0, 1). The data set should include eleven pairs between x = 0 and x = 5 (spacing is 0.5) Test your program with a slope equation slope=(1+2x) √y Your program should include the following global functions contained in a namespace called “RungeKutta” double step(double, double, int) double *ones(int, double = 1) double *linspace(double, double, int) double slope(double, double) void extrapolate(double *, double *, int) void printXY(double *, double *, int)
时间: 2023-09-22 10:07:38 浏览: 104
Here's the program that generates the (x,y) pairs for Euler's extrapolation with the given initial condition and slope equation:
```c++
#include <iostream>
#include <cmath>
namespace RungeKutta {
double step(double x, double y, int n) {
double h = x / n;
for (int i = 1; i <= n; i++) {
y = y + h * slope(x, y);
x = x + h;
}
return y;
}
double *ones(int n, double v) {
double *arr = new double[n];
for (int i = 0; i < n; i++) {
arr[i] = v;
}
return arr;
}
double *linspace(double start, double end, int n) {
double *arr = new double[n];
double h = (end - start) / (n - 1);
for (int i = 0; i < n; i++) {
arr[i] = start + i * h;
}
return arr;
}
double slope(double x, double y) {
return (1 + 2 * x) * sqrt(y);
}
void extrapolate(double *x, double *y, int n) {
for (int i = 0; i < n - 1; i++) {
double h = x[i + 1] - x[i];
y[i + 1] = y[i] + h * slope(x[i], y[i]);
}
}
void printXY(double *x, double *y, int n) {
for (int i = 0; i < n; i++) {
std::cout << "(" << x[i] << ", " << y[i] << ")" << std::endl;
}
}
}
int main() {
const int n = 11;
double *x = RungeKutta::linspace(0, 5, n);
double *y = RungeKutta::ones(n, 1);
y[n - 1] = RungeKutta::step(x[n - 2], y[n - 2], 2);
RungeKutta::extrapolate(x, y, n);
RungeKutta::printXY(x, y, n);
delete[] x;
delete[] y;
return 0;
}
```
Explanation:
- The `RungeKutta` namespace contains the global functions required for the program.
- The `step` function implements Euler's method to compute the value of `y` at a given `x` using the given slope equation. It takes three parameters: the current `x`, the current `y`, and the number of steps to take (`n`). It returns the final value of `y`.
- The `ones` function creates an array of `n` elements with the same value (`v`). It takes two parameters: the number of elements (`n`) and the value to fill the array with (default is 1).
- The `linspace` function creates an array of `n` equally spaced values between `start` and `end`. It takes three parameters: the starting value (`start`), the ending value (`end`), and the number of elements (`n`).
- The `slope` function computes the slope at a given `(x, y)` pair using the given slope equation. It takes two parameters: the current `x` and the current `y`.
- The `extrapolate` function computes the (x,y) pairs for Euler's extrapolation using the values of `x` and `y` computed so far. It takes three parameters: the array of `x` values, the array of `y` values, and the number of elements (`n`).
- The `printXY` function prints the (x,y) pairs to the console. It takes three parameters: the array of `x` values, the array of `y` values, and the number of elements (`n`).
- In the `main` function, we create an array of `x` values using the `linspace` function and an array of `y` values initialized to 1 using the `ones` function. We set the last element of `y` to the value computed using Euler's method with `n=2`. We then compute the remaining `(x,y)` pairs using Euler's extrapolation with the `extrapolate` function and print the results to the console using the `printXY` function. Finally, we delete the dynamically allocated arrays to free up memory.
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)