Pages: [1] 2   Go Down
Author Topic: Problem with arduino maths  (Read 1797 times)
0 Members and 1 Guest are viewing this topic.
Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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:
#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.
Logged

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Everything higher level than geometry uses radians. You should, too.
Logged

Sydney, Australia
Offline Offline
Edison Member
*
Karma: 33
Posts: 1287
Big things come in large packages
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Does the sin() function take degree or radians? Which one are you using?

Logged

Arduino libraries http://arduinocode.codeplex.com
Parola hardware & library http://parola.codeplex.com

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

Offline Offline
Edison Member
*
Karma: 19
Posts: 1041
Arduino rocks
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

The sin function takes degrees.

You might be interested in the first sentence of this page:
http://arduino.cc/en/Reference/Sin
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Ok, i will try using radians.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want to find sin(140) and the calculator returns 0.6, how do i do that in arduino.  smiley-confuse
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

When i try using radians(140); the answerr returns as 0.
Logged

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

I want the degrees converted to radians and thats what sin() does but the answer is wrong, why. Need help with this.  smiley-confuse
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?
Logged

Fort Lauderdale, FL
Offline Offline
Faraday Member
**
Karma: 71
Posts: 6144
Baldengineer
View Profile
WWW
 Bigger Bigger  Smaller Smaller  Reset Reset

Your calculator is using Degrees.  sin() uses Radians.  (As is explained in the reference page.)

Code:
 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:
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".
Logged

Capacitor Expert By Day, Enginerd by night.  ||  Personal Blog: www.baldengineer.com  || Electronics Tutorials for Beginners:  www.addohms.c

Offline Offline
Newbie
*
Karma: 0
Posts: 17
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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

California
Offline Offline
Faraday Member
**
Karma: 92
Posts: 3447
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

Thx james for solving my problem, thats the answer i wanted, so sin() takes int radians and give degrees. Thx smiley-lol
So close...

It actually takes a float.
Logged

Global Moderator
UK
Offline Offline
Brattain Member
*****
Karma: 310
Posts: 26635
I don't think you connected the grounds, Dave.
View Profile
 Bigger Bigger  Smaller Smaller  Reset Reset

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.
Logged

"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.

Pages: [1] 2   Go Up
Jump to: