Numerical analysis in-class experiment of Wuhan University of Technology


Preface

Numerical analysis in-class experiment uses the idea of ​​programming to realize the computational thinking of numerical analysis, which not only deepens the consolidation of knowledge in the class, but also exercises one's own programming ability.

Main equipment and consumables

  1. PC
  2. VS 2019 development environment

The following is the content of this article, the following cases are for reference

1. Use C language to implement several polynomial interpolation programs. (Lagrange interpolation, Newton interpolation)

Experiment description

Lagrange interpolation method uses multiple interpolation points to approximate the root number 115.

Basic principles and design of the experiment

Insert picture description here

Analysis and design

#include<iostream>
using namespace std;
double lagrange(int n,double x[],double y[],double xx)
{
    
    
	double t;
	double yy = 0;
	for (int k = 0; k < n; k++)
	{
    
     
		t= 1;
		for (int j = 0; j <n ; j++)
		{
    
    
			if (j != k)
				t *= (xx - x[j]) / (x[k] - x[j]);
		}
		yy += t*y[k];
	}
	return yy;
}
void main()
{
    
    
	int n;
	double x[100], y[100];
	double xx, yy;
	cout << "Please enter n:";
	cin >> n;
	for (int i = 0; i < n; i++)
	{
    
    
		cout << "x"<<i<<":";
		cin >> x[i];
	}
	cout << endl;
	for (int i = 0; i < n; i++)
	{
    
    
		cout << "y" << i << ":";
		cin >> y[i];
	}
	cout << "Please enter xx:";
	cin >> xx;
	yy = lagrange(n, x, y, xx);
	cout << "Result is:" << yy << endl;}

Experimental results

Insert picture description here

2. Use C language to realize several programs for solving initial value problems of ordinary differential equations. (Euler method and its improvement, Runge-Kutta method)

Experiment description

Use the fourth-order Runge-Kutta method to calculate the initial value of the ordinary differential equation f(x, y)=y-2*x/y, y(0)=1.

Basic principles and design of the experiment

Insert picture description here

Analysis and design

#include<iostream>
using namespace std;
void RK()
{
    
    
	double h = 0.2;
	int n;
	double k1, k2, k3, k4;
	double x[100], y[100];
	x[0] = 0;
	y[0] = 1;
	cout << "请输入计算的次数n:(步长为0.2)";
	cin >> n;
	for (int i = 1; i <= n; i++)
	{
    
    
		k1 = y[i - 1] - 2 * x[i - 1] / y[i - 1];
		k2 = (y[i - 1] + h / 2 * k1) - 2 * (x[i - 1] + h / 2) / (y[i - 1] + h / 2 * k1);
		k3=(y[i - 1] + h / 2 * k2)- 2 * (x[i - 1] + h / 2) / (y[i - 1] + h / 2 * k2);
		k4= (y[i - 1] + h * k3) - 2 * (x[i - 1] + h ) / (y[i - 1] + h  * k3);
		y[i] = y[i - 1] + h /6* (k1+2*k2+2*k3+k4);
		x[i] = x[i - 1] + h;
		cout << x[i] << "\t" << y[i] << endl;
	}
}
int main()
{
    
    
	RK();
	return 0;
}

Experimental results

Insert picture description here

3. Use C language to realize the program of finding roots of several nonlinear equations. (Dichotomy, iteration method, acceleration of iterative process)

Experiment description

The realization of Atkin's algorithm (x=f(x), f(x)=sin(x)+0.5).

Basic principles and design of the experiment

Insert picture description here

Analysis and design

#include<iostream>
#include<math.h>
using namespace std;
double f(double x)
{
    
    
	double f;
	f = sin(x) + 0.5;
	return f;
}
void aitejin()
{
    
    
	double x[100],xx[100],xxx[100];
	x[0] = 1.0;
	double t = 0;
	for (int i = 1; i < 100; i++) {
    
    
		xx[i] = f(x[i - 1]);
		xxx[i] = f(xx[i]);
		x[i] = xxx[i] - pow((xxx[i] - xx[i]), 2) / (xxx[i] - 2 * xx[i] + x[i - 1]);
		cout << "x" << i << ":" << "\t" << x[i] << endl;
		if (x[i] - x[i - 1] < pow(10, -6)) {
    
    
			t = x[i];
			break;
		}
	}
	cout << "近似根为" << t << endl;
}
int main() {
    
    
	aitejin();
	return 0;
}

Experimental results

Insert picture description here

4. Use C language to realize several programs for solving linear equations. (Gauss elimination method)

Experiment description

Use the Gaussian elimination method to calculate the 10-5 x1 + x2 = 1 x1 + x2 = 2 equation system problem.

Basic principles and design of the experiment

Insert picture description here

Analysis and design

#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
bool gauss(int n, double a[][10])
{
    
    
	int k, i, j;
	for (k = 0; k < n - 1; k++)
	{
    
    
		// 判断是否非奇异矩阵
		if (a[k][k] == 0)
			return false;
		for (i = k + 1; i < n; i++)
		{
    
    
			// 直接用 a[i][k] 来存储下面的表达式的值
			a[i][k] = a[i][k] / a[k][k];
			for (j = k + 1; j < n + 1; j++)
				a[i][j] -= a[i][k] * a[k][j]; // 对第 i 行元素进行一次消元
		}
	}
	// 判断 Xn 是否为有唯一解,否则返回错误
	if (a[n - 1][n - 1] == 0)
		return false;
	// 回代求解
	for (k = n - 1; k >= 0; k--)
	{
    
    
		for (j = k + 1; j < n; j++)
			a[k][n] -= a[k][j] * a[j][n];
		a[k][n] /= a[k][k];
	}
	return true;
}
int main(){
    
    
	int i, j, n;
	double a[10][10];
	cout << " 请输入方程组的未知数的个数 n:" << endl;
	cin >> n;
	cout << " 请输入方程组的系数和和: " << endl;
	for (i = 0; i < n; i++)
	{
    
    
		for (j = 0; j <= n; j++)
			cin >> a[i][j];
	}
	if (gauss(n, a))
	{
    
    
		cout << " 方程组的解是: " << endl;
		for (i = 0; i < n; i++)
			cout << setw(5) << a[i][n] << " ";
		cout << endl;
	}
	else
	cout << " 方程不能用此方法解出 " << endl;
}

Experimental results

Insert picture description here


to sum up

The numerical analysis experiment report only requires one method to be written, but I wrote other methods. For details, please refer to the following link.
Link: Chong Chong Chong ~
Extraction code: 1kvi
copy this content and open the Baidu Netdisk mobile phone App, the operation is more convenient.

Guess you like

Origin blog.csdn.net/mo_zhe/article/details/112801842