简体   繁体   English

如何在java中编写sin平方

[英]how to write sin squared in java

double maxHeight = 0.0;

velocity = v;
angle = theta;

maxHeight = Math.pow(velocity, 2.0);
maxHeight = Math.pow(sin, 2.0);
maxHeight *= Math.sin(angle) / (2 * gravity);

I'm trying to get the Math.pow(sin, 2.0);我正在尝试获取Math.pow(sin, 2.0); . . it's asking me to declare sin .它要我宣告sin What am I supposed to put down for it?我应该为此放下什么? I'm just trying to get sin squared.我只是想把罪平方。

First of all - Math.pow(x, 2.0) is equal to sin * sin, and the second approach should be much more efficient.首先 - Math.pow(x, 2.0) 等于 sin * sin,第二种方法应该更有效。

When it comes to answer to your question - does the variable "sin" you want to use in second Math.pow() exists?在回答您的问题时 - 您想在第二个 Math.pow() 中使用的变量“sin”是否存在?

Example code:示例代码:

// assuming that "v", "theta" and "gravity" are known variables

double sin = Math.sin(theta);
double maxHeight = (v * v * sin * sin) / (gravity * 2);

Corrected original code:更正的原始代码:

double maxHeight = 0.0;

velocity = v;
angle = theta;

maxHeight = Math.pow(velocity, 2.0);
double sin = Math.sin(angle);
maxHeight *= Math.pow(sin, 2.0) / (2 * gravity);

The problem with your current code is that you are trying to access a variable named sin but that variable doesn't exist.您当前代码的问题在于您试图访问名为sin的变量,但该变量不存在。 Since you're starting with Java, let's start over.既然您是从 Java 开始的,那么让我们重新开始。

You want to calculate h = v² sin²( θ ) / ( 2 g ) .你想计算h = v² sin²( θ ) / ( 2 g ) We already have v and theta .我们已经有了vtheta

First, the numerator is v² sin²(θ) .首先,分子是v² sin²(θ) This means we need to calculate .这意味着我们需要计算 Then we need to calculate sin(θ) and square the result.然后我们需要计算sin(θ)并对结果进行平方。 Then we need to multiply those two number together.然后我们需要将这两个数字相乘。 Put to code, we have:放入代码,我们有:

double vSquared = Math.pow(v, 2);
double sinTheta = Math.sin(theta);
double sinThetaSquared = Math.pow(sinTheta, 2);
double numerator = vSquared * sinThetaSquared;

The denominator is simply 2*g so we have:分母只是2*g所以我们有:

double maxHeight = numerator / (2 * gravity);

If we simplify this into a single expression, we come up with:如果我们将其简化为单个表达式,我们会得出:

//test data
double v = 1.0;
double theta = 1.5;
double gravity = 9.8;

double maxHeight = Math.pow(v, 2) * Math.pow(Math.sin(theta), 2) / (2 * gravity);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM