Go Down

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

amruth11

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

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

marco_c

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

Arduino libraries http://arduinocode.codeplex.com
Parola for Arduino http://parola.codeplex.com

amruth11

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

WizenedEE


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.

James C4S


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

Ok, i will try using radians.

amruth11

I want to find sin(140) and the calculator returns 0.6, how do i do that in arduino.  :~

amruth11

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

amruth11

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

Arrch


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?

James C4S

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

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

Arrch


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

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.

Go Up