《C》C语言编程实现任意阶数的“m序列”并通过gnuplot绘图——在知道本原多项式的前提下

关于m序列的构建方法

在我之前的博客中已详细介绍,请点击 → \rightarrow C语言编程实现指定阶“m序列”并通过gnuplot绘图

一、C语言编程实现任意阶m序列
1. 任意阶m序列的程序设计流程图:

在这里插入图片描述

2. 任意阶m序列源程序
【Msquence.c】
#include<stdio.h>
#include<stdlib.h>
#include<math.h>

int main(int argc, char *argv[])  //命令行参数
{
   	 
	if(argc<2)  //输出提示信息,提示如何使用
    {
   
		printf("\nProduce data of Msquence 2018 ygaairspace Copyright(C)\n");
		printf("Usage: Msquence.exe index1(Max index) index2 ... ('index' is index of primitive polynomial)");
		printf("Example: Msquence.exe 6 1 0");
		return 0;
    }
	
	int a[100] = {
   0};  //定义初始化数组a
	int c[101] = {
   0};  //定义反馈选择接入数组c
	int max = atoi(argv[1]); //将字符型的“最大指数”转化为整型数
	int temp;  //temp用于存放反馈量 
	int p
  • 1
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用伪势模型实现剪切作用下气液相分离的 C 语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <math.h> #include <time.h> #define N 100 // 粒子数 #define L 10 // 盒子大小 #define T 20 // 总时间 #define dt 0.1 // 时间步长 #define gamma 0.1 // 阻尼系数 #define eps 1e-10 // 小数判定精度 typedef struct { double x, y; } Vector2; // 定义伪势模型 double LennardJones(double r) { return 4 * (pow(r, -12) - pow(r, -6)); } // 定义计算力的函数 void force(Vector2 pos[], Vector2 F[], int N) { for (int i = 0; i < N; i++) { for (int j = i+1; j < N; j++) { Vector2 r = {pos[i].x - pos[j].x, pos[i].y - pos[j].y}; r.x -= round(r.x / L) * L; r.y -= round(r.y / L) * L; double d = sqrt(r.x*r.x + r.y*r.y); if (d < 2.5) { double f = LennardJones(d) / d; F[i].x += f * r.x; F[i].y += f * r.y; F[j].x -= f * r.x; F[j].y -= f * r.y; } } } } // 定义运动函数 void integrate(Vector2 pos[], Vector2 vel[], int N) { Vector2 F[N]; for (int i = 0; i < N; i++) { F[i].x = F[i].y = 0; } for (double t = 0; t < T; t += dt) { force(pos, F, N); for (int i = 0; i < N; i++) { vel[i].x = vel[i].x * (1 - gamma*dt) + F[i].x * dt; vel[i].y = vel[i].y * (1 - gamma*dt) + F[i].y * dt; pos[i].x += vel[i].x * dt; pos[i].y += vel[i].y * dt; pos[i].x -= floor(pos[i].x / L) * L; pos[i].y -= floor(pos[i].y / L) * L; } } } // 定义绘图函数 void plot(Vector2 pos[], int N) { FILE *fp = fopen("output.dat", "w"); for (int i = 0; i < N; i++) { fprintf(fp, "%.6lf %.6lf\n", pos[i].x, pos[i].y); } fclose(fp); system("gnuplot -p -e \"plot 'output.dat'\""); } // 定义主函数 int main() { srand(time(NULL)); Vector2 pos[N], vel[N]; for (int i = 0; i < N; i++) { pos[i].x = (double)rand() / RAND_MAX * L; pos[i].y = (double)rand() / RAND_MAX * L; vel[i].x = (double)rand() / RAND_MAX; vel[i].y = (double)rand() / RAND_MAX; } integrate(pos, vel, N); plot(pos, N); return 0; } ``` 该代码使用 Lennard-Jones 势函数作为伪势模型,通过计算每个粒子之间的相互作用力,模拟气液相分离的过程。代码中的 `force` 函数计算力,`integrate` 函数进行运动模拟,`plot` 函数绘制图像。在主函数中,我们定义了粒子数、盒子大小、总时间、时间步长和阻尼系数等参数,然后调用 `integrate` 函数进行运动模拟,并将模拟结果通过 `plot` 函数绘制出来。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值