当前位置: 首页 > news >正文

倒单摆22Inverted pendulum)c++代码1201

//倒单摆(Inverted pendulum)c++

#include<stdio.h>

#include<iomanip>

#include <iostream>

#include <cmath>

//#define PI 3.1415926

#define PI 0x3.243F6A8885A3p+0

#define PI16 0x3.243F6A8885A308D313198A2E03707344A4093822299F31D0082EFA98EC4E6C89p+0

long double fPi0x=atan(1)*4.0;

using namespace std;

class pendulum

{

public:

    double m;    //摆杆的质量

    double l;    //摆杆的长度

    double g;    //重力加速度

    double t;    //时间

    double theta;//摆杆的角度

    pendulum();

    pendulum(double m, double l, double g, double t, double theta);

    double get_theta();

    double get_theta_dot();

    void set_theta_dot(double theta_dot);

    void calculate_theta(double t);

private:

    double theta_dot;//摆杆的角速度

};

pendulum::pendulum()

{

    m = 1.0;

    l = 1.0;

    g = 9.8;

    t = 0.0;

    theta = 0.0;

    theta_dot = 0.0;

}

pendulum::pendulum(double m, double l, double g, double t, double theta)

{

    this->m = m;

    this->l = l;

    this->g = g;

    this->t = t;

    this->theta = theta;

    theta_dot = 0.0;

}

double pendulum::get_theta()

{

    return theta;

}

double pendulum::get_theta_dot()

{

    return theta_dot;

}

void pendulum::set_theta_dot(double theta_dot)

{

    this->theta_dot = theta_dot;

}

void pendulum::calculate_theta(double t)

{

    double a = g / l;

    theta = theta + theta_dot * t + 0.5 * a * t * t;

    theta_dot = theta_dot + a * t;

    if (theta >= 2 * PI)

    {

        theta = theta - 2 * PI;

    }

    else if (theta < 0)

    {

        theta = theta + 2 * PI;

    }

    this->t = t;

}

int main()

{

   

    std::cout << std::setprecision(100);

    std::cout<< fPi0x<<std::endl;

    std::cout<<PI<<std::endl;

    std::cout<<PI16<<std::endl;

    

    pendulum p(1.0, 1.0, 9.8, 0.0, 0.0);

    long double theta, theta_dot;

    

    std::cout << std::setprecision(12);

    for (int i = 0; i < 10; i++)

    {//220for

        theta = p.get_theta();

        theta_dot = p.get_theta_dot();

        p.calculate_theta(0.1);

        

//        printf("t=%ll",p.t);

        cout << "t = " << p.t << ", theta = " << theta << " rad, theta_dot = " << theta_dot << " rad/s" << endl;

//        printf(",theta=%ll",theta);

    }//220for

    return 0;

}//10main

相关文章:

  • 车载诊断数据库ODX——Flash(刷写)
  • Linux中的进程状态
  • 【C函数】函数详解
  • jdk11新特性——ZGC
  • 【java】stream流
  • 在linux中配置redis去中心化集群
  • 2022圣诞树(C语言摇钱树版本)
  • 【圣诞节】飘雪圣诞树
  • 口罩佩戴监测系统 yolo
  • 卡塔尔世界杯半自动越位识别技术(SAOT)的工作原理
  • 《图解TCP/IP》阅读笔记(第六章 6.5、6.6)——其他代表性的传输层协议与UDP、TCP首部
  • 如何花最少的资源遍历二叉树
  • 【PAT甲级 - C++题解】1125 Chain the Ropes
  • 学Python上班第一天就翻车了?
  • Vue - npm 批量升级依赖包
  • Java编程实战21:构造字典序最大的合并字符串
  • java基于ssm,jsp鞋城源码卖鞋服装男鞋商城女鞋商城项目源码
  • java实现每日给女友微信发送早安等微信信息
  • Android设计模式详解之解释器模式
  • zabbix6.0安装教程(七):从web界面安装
  • 科学时代重读“老子”的意义——对谈《老子智慧八十一讲》
  • 上海潮汕联谊会举行换届大会,陈湖文当选会长
  • 新任浙江省委常委、组织部长杨荫凯到嘉兴南湖瞻仰红船
  • 只在上海!德国剧团新作亚洲首演:一张古典与流行的声音网络
  • 对外投资增长、消费市场持续升温,中国经济砥砺前行
  • 亚振家居控制权将变更:济南域潇集团实控人成新控股股东