So I'm trying to practice using Ruby to solve trig functions and I'm having some difficulty getting the Math library to work.
The trig function is: y = (x^3 sqrt(2x^2)) / (sin(x+5))
where x = 51, and the answer needs to be in degrees. The problem is supposed to be solved in Matlab, which I have no experience with, but I managed to hack together the following output for the function: 1.1540e + 07
, and I want to verify that what I'm doing in Matlab is correct, so I want to use Ruby to make sure the answer is what I'm supposed to be getting.
The Ruby code I'm trying to use to solve this function is: puts ((51 ** 3)Math.sqrt(2(51 ** 2)) / (Math::sin(56.degrees)))
however when running the code I get the following errors:
/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)))
How can I go about evaluating this function and resolving the errors?
答案 0 :(得分:2)
You need to have * each time you multiply, and try changing degrees to radians:
x = 51
p (x ** 3) * Math.sqrt(2 * x ** 2) / (Math::sin((x+5) * Math::PI / 180))
You also don't need so many paranthesis because Ruby understands mathematical order of operations.
答案 1 :(得分:1)
As the error is telling you, some (
and *
are missing.
Since the intended variable should be in Radians, so you should convert first to from degrees to radians and then do the calculation.
xindeg = 51
xinrad = xindeg*Math::PI/180
puts xinrad ** 3 * Math.sqrt(2*xinrad ** 2) / Math::sin(xinrad+5*Math::PI/180)
1.070855715686936
Also in MATLAB, the correct way is as follows:
x=51
xinrad = x*pi/180;
y = (xinrad^3 * sqrt(2*xinrad^2)) / (sin(xinrad+5*pi/180))
y =
1.070855715686936