日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

高斯定理的证明(三重积分的C/C++实现)(C++)(大学物理)

發(fā)布時(shí)間:2023/12/18 c/c++ 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 高斯定理的证明(三重积分的C/C++实现)(C++)(大学物理) 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

特別鳴謝:無(wú)私幫助我的ycc同學(xué)

#include <iostream> #include <cstdlib> #include <ctime> #include <corecrt_math_defines.h>const int max_eletrons = 256;struct Eletrons {double x, y, z, q; } eletrons[max_eletrons]; int num_eletrons = 0; double rand_double() {return double(rand()) / 65536.0; } void init_eletrons(int range_min, int range_max) {srand((unsigned)time(0));num_eletrons = range_min + rand() % (range_max - range_min);for (int i = 0; i < num_eletrons; i++) {eletrons[i] = { rand_double(), rand_double(), rand_double(), rand_double() * 100.0 };} } struct Vec {double x, y, z;inline Vec operator + (Vec const& r) const { return { x + r.x, y + r.y, z + r.z }; }inline Vec operator - (Vec const& r) const { return { x - r.x, y - r.y, z - r.z }; }inline Vec operator * (double const& s) const { return { x * s, y * s, z * s }; }inline double dot(Vec const& r) const { return x * r.x + y * r.y + z * r.z; }inline double length2() const { return dot(*this); }inline double length() const { return std::sqrt(length2()); }inline double distance(Vec const& r) const { return (*this - r).length(); }inline Vec normalize() const { auto len = length(); return { x / len, y / len, z / len }; } };const double pi = M_PI; //const double eps0 = 8.854187817 * 1e-12f const double eps0 = 1; const double k = 1 / (4 * pi * eps0);double integral_dS(Vec const& pos, Vec const& normal) {double sum = 0.0;for (int i = 0; i < num_eletrons; ++i) {Eletrons e = eletrons[i];Vec dir = pos - Vec{ e.x, e.y, e.z };sum += dir.normalize().dot(normal) * k * e.q / dir.length2();}return sum; } struct Sphere {Vec p;double r; }; bool sphere_inside(const Sphere& s, const Eletrons& e) {return s.p.distance(Vec{ e.x, e.y, e.z }) <= s.r; } double sphere_gauss(const Sphere& s) {double Q = 0;for (int i = 0; i < num_eletrons; ++i) {if (sphere_inside(s, eletrons[i])) Q += eletrons[i].q;}return Q / eps0; } double sphere_integral(const Sphere& s, int step) {double dphi = pi / step, dtheta = 2 * pi / step;double sum = 0.0;for (double phi = 0; phi < pi; phi += dphi)for (double theta = 0; theta < 2 * pi; theta += dtheta) {double dS = s.r * s.r * sin(phi) * dtheta * dphi;Vec norm = { sin(phi) * cos(theta), sin(phi) * sin(theta), cos(phi) };sum += integral_dS(s.p + norm * s.r, norm) * dS;}return sum; } struct Rectangle {Vec p, l; }; bool rectangle_inside(const Rectangle& rec, const Eletrons& e) {return e.x >= rec.p.x - rec.l.x && e.x <= rec.p.x + rec.l.x &&e.y >= rec.p.y - rec.l.y && e.y <= rec.p.y + rec.l.y &&e.z >= rec.p.z - rec.l.z && e.z <= rec.p.z + rec.l.z; } double rectangle_gauss(const Rectangle& rec) {double Q = 0;for (int i = 0; i < num_eletrons; ++i) {if (rectangle_inside(rec, eletrons[i])) Q += eletrons[i].q;}return Q / eps0; } double rectangle_integral(const Rectangle& rec, int step) {double sum = 0.0, sum_xy = 0.0, sum_yz = 0.0, sum_xz = 0.0;double dx, dy, dz, dS;dx = 2 * rec.l.x / step, dy = 2 * rec.l.y / step, dS = dx * dy;sum_xy = 0.0;for (double x = rec.p.x - rec.l.x; x < rec.p.x + rec.l.x; x += dx)for (double y = rec.p.y - rec.l.y; y < rec.p.y + rec.l.y; y += dy) {sum_xy += integral_dS({ x + dx / 2, y + dy / 2, rec.p.z - rec.l.z }, { 0, 0, -1 });sum_xy += integral_dS({ x + dx / 2, y + dy / 2, rec.p.z + rec.l.z }, { 0, 0, 1 });}sum += sum_xy * dS;dy = 2 * rec.l.y / step, dz = 2 * rec.l.z / step, dS = dy * dz;sum_yz = 0.0;for (double y = rec.p.y - rec.l.y; y < rec.p.y + rec.l.y; y += dy)for (double z = rec.p.z - rec.l.z; z < rec.p.z + rec.l.z; z += dz) {sum_yz += integral_dS({ rec.p.x - rec.l.x, y + dy / 2, z + dz / 2 }, { -1, 0, 0 });sum_yz += integral_dS({ rec.p.x + rec.l.x, y + dy / 2, z + dz / 2 }, { 1, 0, 0 });}sum += sum_yz * dS;dx = 2 * rec.l.x / step, dz = 2 * rec.l.z / step, dS = dx * dz;sum_xz = 0.0;for (double x = rec.p.x - rec.l.x; x < rec.p.x + rec.l.x; x += dx)for (double z = rec.p.z - rec.l.z; z < rec.p.z + rec.l.z; z += dz) {sum_xz += integral_dS({ x + dx / 2, rec.p.y - rec.l.y, z + dz / 2 }, { 0, -1, 0 });sum_xz += integral_dS({ x + dx / 2, rec.p.y + rec.l.y, z + dz / 2 }, { 0, 1, 0 });}sum += sum_xz * dS;return sum; } struct Cylinder {Vec p;double r, h; }; bool cylinder_inside(const Cylinder& clin, const Eletrons& e) {return e.z >= clin.p.z - clin.h / 2 && e.z <= clin.p.z + clin.h / 2 && clin.p.distance(Vec{ e.x,e.y,clin.p.z }) <= clin.r;//miaomiaomiao } double cylinder_gauss(const Cylinder& clin) {double Q = 0;for (int i = 0; i < num_eletrons; ++i) {if (cylinder_inside(clin, eletrons[i])) Q += eletrons[i].q;}return Q / eps0; } double cylinder_integral(const Cylinder& clin, int step) {double dz = clin.h / step, dtheta = 2 * pi / step, dr = clin.r / step;double sum = 0.0;for (double theta = 0; theta < 2 * pi; theta += dtheta){for (double r = 0; r < clin.r; r += dr){double ds = dtheta * dr * r;Vec norm1 = { 0, 0, 1 }, norm2 = { 0, 0, -1 };sum += integral_dS(clin.p + Vec{ r * cos(theta), r * sin(theta), clin.h / 2 }, norm1) * ds;sum += integral_dS(clin.p + Vec{ r * cos(theta),r * sin(theta), -clin.h / 2 }, norm2) * ds;}}for (double theta = 0; theta < 2 * pi; theta += dtheta) {for (double z = -clin.h / 2; z < clin.h / 2; z += dz) {double ds = clin.r * dtheta * dz;Vec norm = { cos(theta),sin(theta), 0 };sum += integral_dS(clin.p + Vec{ clin.r * cos(theta), clin.r * sin(theta), z }, norm) * ds;}}return sum; } struct Circular_cone {Vec p; //默認(rèn)圓錐的頂點(diǎn)朝z軸正方向double r, h; }; double circular_cone_inside(const Circular_cone& cir, const Eletrons& e) {double r0 = (cir.r / cir.h) * (cir.h - (e.z - cir.p.z));return e.z >= cir.p.z && e.z <= cir.p.z + cir.h && r0 >= 0 &&(e.x - cir.p.x) * (e.x - cir.p.x) + (e.y - cir.p.y) * (e.y - cir.p.y) <= r0 * r0; } double circular_cone_gauss(const Circular_cone& cir) {double Q = 0;for (int i = 0; i < num_eletrons; ++i) {if (circular_cone_inside(cir, eletrons[i])) Q += eletrons[i].q;}return Q / eps0; } double circular_cone_integral(const Circular_cone& cir, int step) {double dr = cir.r / step, dtheta = 2 * pi / step, dy = cir.h / step;double sum = 0.0;for (double r = 0; r < cir.r; r += dr) {for (double theta = 0; theta < 2 * pi; theta += dtheta) {double ds = r * dr * dtheta;Vec norm = { 0, 0, -1 };sum += integral_dS(cir.p + Vec{ r * cos(theta), r * sin(theta), 0 }, norm) * ds;}}double alpha = atan(cir.r / cir.h);for (double y = 0; y < cir.h; y += dy)for (double theta = 0; theta < 2 * pi; theta += dtheta) {double r0 = (cir.r / cir.h) * (cir.h - y - dy);double ds = dtheta * r0 * dy / cos(alpha);Vec norm = { cos(theta), sin(theta), cir.r / cir.h };sum += integral_dS(cir.p + Vec{ r0 * cos(theta), r0 * sin(theta), y }, norm.normalize()) * ds;}return sum; }int main() {/*init_eletrons(5, 7);Sphere sphere = { {0, 0, 0}, 1 };printf("Sphere use Gauss: %lf\n", sphere_gauss(sphere));printf("Sphere use Integral: %lf\n", sphere_integral(sphere, 1000));Rectangle rectangle = { {0, 0, 0}, {0.9, 0.9, 0.9} };printf("Rectangle use Gauss: %lf\n", rectangle_gauss(rectangle));printf("Rectangle use Integral: %lf\n", rectangle_integral(rectangle, 1000));Cylinder cylinder = { {0,0,0},1000,2000 };printf("Cylinder use Gauss: %lf\n", cylinder_gauss(cylinder));printf("Cylinder use Integral: %lf\n", cylinder_integral(cylinder, 1000));Circular_cone cir = { {0, 0, -1}, 1, 2 };printf("Circular_cone use Gauss: %lf\n", circular_cone_gauss(cir));printf("Circular_cone use Integral: %lf\n", circular_cone_integral(cir, 2000));system("pause");return 0;*/printf("%d,%d,%d,",267.377556 - 267.030334, 237.123285 - 236.817932, 138.437950 - 138.261414); }

學(xué)習(xí)經(jīng)驗(yàn)(按照代碼順序)

  • struct Vec{}“面向?qū)ο蟆?br /> 這個(gè)結(jié)構(gòu)幫助實(shí)現(xiàn)多元向量值積分中的一些常用操作
  • 1)inline函數(shù)&內(nèi)聯(lián)函數(shù)
    關(guān)鍵字 inline 必須與函數(shù)定義體放在一起才能使函數(shù)成為內(nèi)聯(lián),僅將 inline 放在函數(shù)聲明前面不起任何作用。
    tip:當(dāng)函數(shù)只有10行或者更少時(shí)將其定義為內(nèi)聯(lián)函數(shù)
    內(nèi)聯(lián)函數(shù)讓代碼更簡(jiǎn)單,好用get;)

    2)std::
    是標(biāo)準(zhǔn)庫(kù)的前綴,C++要用標(biāo)準(zhǔn)庫(kù)函數(shù)就要用std::

    主要是起到了資源管理的作用,下面是一個(gè)例子:

    有兩個(gè)軟件公司A公司和B公司,他們都是用C++語(yǔ)言開(kāi)發(fā)他們的產(chǎn)品。那么,他們分別編寫了a.h和b.h兩個(gè)自己的頭文件,這兩個(gè)文件中都有一個(gè)叫func()的函數(shù)。他們各自使用也沒(méi)什么問(wèn)題。

    假設(shè)你公司也是一個(gè)軟件公司,你現(xiàn)在要開(kāi)發(fā)一個(gè)軟件,必須同時(shí)用到A公司和B公司的頭文件,同時(shí)會(huì)調(diào)用他們的func()函數(shù)。這個(gè)時(shí)候問(wèn)題就來(lái)了,你調(diào)用的func()函數(shù),編譯器不知道應(yīng)該選用A公司的還是B公司的。為解決這個(gè)問(wèn)題,C++采用了命名空間,這樣,你調(diào)用A公司的func()函數(shù),就使用A::func(),B公司亦然。

    原文鏈接:https://blog.csdn.net/u013152895/article/details/44490923

  • init_eletrons()函數(shù)
  • 總結(jié)

    以上是生活随笔為你收集整理的高斯定理的证明(三重积分的C/C++实现)(C++)(大学物理)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。