Java简单实现单摆模拟

我听说写代码的同时也要发博客,于是就随便发一篇

这些代码并没有体现出java的优越性,大家看着乐呵就好(指不定还会高血压)

import java.lang.Math;

public class run{
    public static void init(Box box){
        box.setStickLength(10);
        box.setDx(0.001);
    }
    public static void main(String[] args) {
        Thyta thyta = new Thyta();
        Box box = new Box();
        thyta.init(box);
        init(box);
        calculator Calculator = new calculator();
        while (true){
            Calculator.Task01(box);
            System.out.println(Math.toDegrees(thyta.getThyta()));
        }
    }
}

这段代码是执行用的,run类,会一直输出单摆的角度,StickLength是单摆摆长,Dx是微分变量

public class Thyta extends Box{
    private static boolean init = false;
    public static void init(Box Box){
        init(45, false, Box);
    }
    public static void init(double Orin, boolean Radian, Box Box){
        Box.setThyta(Orin, Radian);
        init = true;
    }
    public static double getThyta(){
        if (init) {
            return Box.Thyta();
        }else{
            return 114514;
        }
    }
    public static void setThyta(double newThyta){
        if (init){
            Box.setThyta(newThyta, true);
        }else{
            System.out.println(1145141919);
        }
    }
    public static void setThyta(double newThyta, boolean Radian){
        Box.setThyta(newThyta, Radian);
    }
}

这是Thyta类,专门用来init的

import java.lang.Math;

public class Box{
    private static double Thyta;
    private static double doubleForce;  // 这个是加速度的加速度
    private static double aForce;  // 正如其名,这个是单摆的加速度
    private static double arcLength;  // 弧长的意思
    private static double stickLength;  // 单摆的长度
    private static double dx$;
    private static final double g = 9.8065;  // 重力加速度
    private static boolean lengthAlrSet = false;  // 单摆的长度有没有被设置
    private static boolean dxAlrSet = false;
    public static double Thyta(){
        return Thyta;
    }
    public static double doubleForce(){
        return doubleForce;
    }
    public static double aForce(){
        return aForce;
    }
    public static double arcLength(){
        return arcLength;
    }
    public static double stickLength(){
        return stickLength;
    }
    public static double g(){
        return g;
    }
    public static double dx(){
        return dx$;
    }
    public static void setThyta(double newThyta, boolean Radian){
        if (Radian){
            Thyta = newThyta;
        }else{
            Thyta = Math.toRadians(newThyta);
        }
    }
    public static void setDoubleForce(double newForce){
        doubleForce = newForce;
    }
    public static void setaForce(double newForce){
        aForce = newForce;
    }
    public static void setArcLength(double newLength){
        arcLength = newLength;
    }
    public static void setStickLength(double newLength){
        if (!lengthAlrSet) {
            stickLength = newLength;
            lengthAlrSet = true;
        }
    }
    public static void setDx(double dx){
        if (!dxAlrSet){
            dx$ = dx;
            dxAlrSet = true;
        }else{
            System.out.println(114514);
        }
    }
}

这是一个集成了所有的变量的Box类,具体怎么做的随便看看就好

import java.lang.Math;

public class calculator {
    public static void Task01(Box Box){
        Box.setArcLength(Box.stickLength() * Box.Thyta());
        Box.setDoubleForce(-1 * Box.g() * Math.cos(Box.Thyta()) / Box.stickLength());
        Box.setaForce(Box.aForce() + Box.doubleForce() * Box.dx());
        Box.setThyta(Math.toDegrees(Box.Thyta()) + Box.aForce() * 1, false);
    }
}

这是单摆算法本体

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值