Go Down

### Topic: Problem with arduino maths (Read 3811 times)previous topic - next topic

#### amruth11

##### May 23, 2012, 06:42 am
Hi everyone, i am using the math.h or the first time and when the arduino does the math the answers of the arduino and the calculator have atleast a difference of 2. Thats a big difference which i dont  want.Below is the code i used for doing the math.
Code: [Select]
`#include <math.h>double u;double t;double a;double f;int m = 4;int v = 0;double g = 9.8;int d = 1;int AOF = 70;void setup(){  Serial.begin(9600);  u = sqrt((g*d)/sin(2*AOF));  Serial.print("Velocity = ");  Serial.println(u);  Serial.println();  t = 2*(u*sin(AOF))/g;  Serial.print("Time = ");  Serial.println(t);  Serial.println();  a = -1*((v-u)/t);  Serial.print("Acceleration = ");  Serial.println(a);  Serial.println();  f = m*a;  Serial.print("Force = ");  Serial.println(f);}void loop(){}`

These are the answers i get form the arduino-
velocity = 3.16
time = 0.5
acceleration = 6.33
force = 25.33

These are the answers from the calc -
velocity = 3.95
time = 0.7
acceleration = 5.somthing (dont remember)
force = 23.something

I coudnt find out wat i was doing wrong, plz give me ur advices on how to get the arduino answer as close as i can to the calculators or is this the best arduino can do. Plz need help.

#### WizenedEE

#1
##### May 23, 2012, 06:53 am
Everything higher level than geometry uses radians. You should, too.

#### marco_c

#2
##### May 23, 2012, 06:54 am
Does the sin() function take degree or radians? Which one are you using?

Arduino Libraries https://github.com/MajicDesigns?tab=Repositories
Parola for Arduino https://github.com/MajicDesigns/Parola
Arduino++ blog https://arduinoplusplus.wordpress.com

#### amruth11

#3
##### May 23, 2012, 07:02 am
The sin function takes degrees. In my case it 70 degrees that is the Angle Of Fire (AOF).

#### WizenedEE

#4
##### May 23, 2012, 07:03 am

The sin function takes degrees. In my case it 70 degrees that is the Angle Of Fire (AOF).

The fact that doing the calculations assuming it takes radians makes them match the ones with an arduino seem to refute your claim.

If you really want to use degrees, there's a radians() function which converts.

#### cmiyc

#5
##### May 23, 2012, 07:05 am

The sin function takes degrees.

You might be interested in the first sentence of this page:
http://arduino.cc/en/Reference/Sin
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

#### amruth11

#6
##### May 23, 2012, 07:06 am
Ok, i will try using radians.

#### amruth11

#7
##### May 23, 2012, 07:17 am
I want to find sin(140) and the calculator returns 0.6, how do i do that in arduino.  :~

#### amruth11

#8
##### May 23, 2012, 07:23 am
When i try using radians(140); the answerr returns as 0.

#### amruth11

#9
##### May 23, 2012, 07:31 am
I want the degrees converted to radians and thats what sin() does but the answer is wrong, why. Need help with this.  :~

#### Arrch

#10
##### May 23, 2012, 07:36 am

I want the degrees converted to radians and thats what sin() does but the answer is wrong, why. Need help with this.  :~

Thats not what sin() does. Sin() takes the angle in Radians as the input. It also takes it as a float, so why not just convert it yourself?

#### cmiyc

#11
##### May 23, 2012, 07:37 am
Your calculator is using Degrees.  sin() uses Radians.  (As is explained in the reference page.)

Code: [Select]
`  float answer = sin(140.0);  Serial.println(answer);`
Returns 0.98.  My calculator, in Radians-mode, returns the same thing.

If I assume you mean "140 degrees" then converting to radian:
Code: [Select]
` float answer = sin(140.0*(3.14/180.0));  Serial.println(answer);`
Returns 0.64.

Also note that my integers are in float/decimal.  "140.0" instead of "140".
Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.com

#### amruth11

#12
##### May 23, 2012, 07:44 am
Thx james for solving my problem, thats the answer i wanted, so sin() takes int radians and give degrees. Thx XD

#### Arrch

#13
##### May 23, 2012, 07:52 am

Thx james for solving my problem, thats the answer i wanted, so sin() takes int radians and give degrees. Thx XD

So close...

It actually takes a float.

#### AWOL

#14
##### May 23, 2012, 10:29 am
Quote
so sin() takes int radians and give degrees.

No, it takes radians, and gives the sine of the supplied angle.
A sine is a ratio.
"Pete, it's a fool looks for logic in the chambers of the human heart." Ulysses Everett McGill.
Do not send technical questions via personal messaging - they will be ignored.
I speak for myself, not Arduino.

Go Up