Ruby 和求解三角函数的数学模块
Ruby and math module to solve trig functions
所以我正在尝试练习使用 Ruby 求解三角函数,但我在使用数学库时遇到了一些困难。
三角函数是:y = (x^3 sqrt(2x^2)) / (sin(x+5))
其中 x = 51,答案需要以度为单位。这个问题应该在我没有经验的 Matlab 中解决,但我设法将函数的以下输出拼凑在一起:1.1540e + 07
,我想验证我在 Matlab 中所做的是正确,所以我想使用 Ruby 来确保答案是我应该得到的。
我试图用来解决此功能的 Ruby 代码是:puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
但是当 运行 代码出现以下错误时:
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected tCONSTANT, expecting ')'
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
^
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected '(', expecting ')'
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
^
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected ')', expecting end-of-input
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
我该如何评估这个函数并解决错误?
每次相乘都需要*,并尝试将度数改为弧度:
x = 51
p (x ** 3) * Math.sqrt(2 * x ** 2) / (Math::sin((x+5) * Math::PI / 180))
您也不需要那么多括号,因为 Ruby 理解数学运算顺序。
如错误提示,部分 (
和 *
丢失。
由于需要的变量应该是弧度,所以你应该先把度数转换成弧度,然后再计算。
xindeg = 51
xinrad = xindeg*Math::PI/180
puts xinrad ** 3 * Math.sqrt(2*xinrad ** 2) / Math::sin(xinrad+5*Math::PI/180)
1.070855715686936
同样是在MATLAB中,正确的做法是:
x=51
xinrad = x*pi/180;
y = (xinrad^3 * sqrt(2*xinrad^2)) / (sin(xinrad+5*pi/180))
y =
1.070855715686936
所以我正在尝试练习使用 Ruby 求解三角函数,但我在使用数学库时遇到了一些困难。
三角函数是:y = (x^3 sqrt(2x^2)) / (sin(x+5))
其中 x = 51,答案需要以度为单位。这个问题应该在我没有经验的 Matlab 中解决,但我设法将函数的以下输出拼凑在一起:1.1540e + 07
,我想验证我在 Matlab 中所做的是正确,所以我想使用 Ruby 来确保答案是我应该得到的。
我试图用来解决此功能的 Ruby 代码是:puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
但是当 运行 代码出现以下错误时:
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected tCONSTANT, expecting ')'
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
^
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected '(', expecting ')'
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
^
/Users/sam/Desktop/jasons_shit.rb:1: syntax error, unexpected ')', expecting end-of-input
puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
我该如何评估这个函数并解决错误?
每次相乘都需要*,并尝试将度数改为弧度:
x = 51
p (x ** 3) * Math.sqrt(2 * x ** 2) / (Math::sin((x+5) * Math::PI / 180))
您也不需要那么多括号,因为 Ruby 理解数学运算顺序。
如错误提示,部分 (
和 *
丢失。
由于需要的变量应该是弧度,所以你应该先把度数转换成弧度,然后再计算。
xindeg = 51
xinrad = xindeg*Math::PI/180
puts xinrad ** 3 * Math.sqrt(2*xinrad ** 2) / Math::sin(xinrad+5*Math::PI/180)
1.070855715686936
同样是在MATLAB中,正确的做法是:
x=51
xinrad = x*pi/180;
y = (xinrad^3 * sqrt(2*xinrad^2)) / (sin(xinrad+5*pi/180))
y =
1.070855715686936