For Problem ! Please !

F = G*m1*m2/r

I need 1000 values to F.

G, m1, m2 are Constants values, r change in a range to 0-1000

for example: r1 = 147000
r2 = 152000

but i need add 5 unids in 1000 times.

ex:

F = G*m1*m2/147000
F = G*m1*m2/147005
F = G*m1*m2/147010
.
.
.
.
and all that results send to txt file, in this case its "valores_out.dat"

the Quiestion is ¿how i can made a for loop to made this?

this is my script... but it dosn't works to replaces 1000 times the r.

(AND SORRY POR MY ENGLISH!)

#include <iostream>
#include <math.h>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <iomanip>
#include <cmath>
#include <stdlib.h>
#include <stdio.h>

using namespace std;
ofstream outfile;

int main ()
//Declaracion de variables
{

double m1; //Masa de la Tierra
double m2; //Masa del Sol
double G; //Costante de Gravitacion Universal en N
double F; //Fuerza en N
double r1; //Distancia mas corta entre la Tierra y el Sol en KM
double r2; //Distancia mas grande entre la Tierra y el Sol en KM
int r; //Valor del Radio entre 142000 y 157000 en kilometros
double i;
double d;

//Declaracion de contantes:
m1 = 5.9736e24;
m2 = 1,9891e30;
G = 6.674e-11;
r1 = 147000;
r2 = 152000;

outfile.open("valores_out.dat");
if(!outfile)
{
cout << "Error al abrir el archivo." << endl;
return 0;
}

for (r = 147000;r < 152000;r += 5)
{
outfile << r << endl;
}
F = G * m1 * m2 / r*r;
{
outfile << r << endl;
}

{
// outfile << r <<" kms" << "|" << F << " N" << endl;
}
return 0;
}

Thanks

Is this what you want to calculate?


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
     //Declaracion de contantes:
     const double m1 = 5.9736e24;   //Masa de la Tierra
     const double m2 = 1.9891e30;   //Masa del Sol
     const double G = 6.674e-11; //Costante de Gravitacion Universal en N
  
     //Distancia mas corta entre la Tierra y el Sol en KM
     const double r1 = 147000;
     const double r2 = 152000;
  
     ofstream outfile;
     outfile.open("valores_out.dat");
     if(!outfile) {
	  cout << "Error al abrir el archivo." << endl; 
	  return -1; 
     }
     
     //Valor del Radio entre 142000 y 157000 en kilometros
     for (int r = r1; r < r2;r += 5)
     {
	  double F = (G * m1 * m2) / r; //Fuerza en N
	  outfile << r <<" kms" << "|" << F << " N" << endl;
     }
     return 0;
}

Yeah ! Thanks lot man, I Prove it and it works PERFECT, this is what I need. and, if i want change r for m1, for example:


#include <iostream>
#include <fstream>

using namespace std;

int main ()
{
     //Declaracion de contantes:
     const double r = 147000 // Now r is a const value
     const double m2 = 1.9891e30;   //Masa del Sol
     const double G = 6.674e-11; //Costante de Gravitacion Universal en N
     long double m1 = 6e+24;
 
   
     ofstream outfile;
     outfile.open("valores_out.dat");
     if(!outfile) {
	  cout << "Error al abrir el archivo." << endl; 
	  return -1; 
     }

//m1/1000 = 6e021
     
     //Value of mass between 0 - 6e+24
     for (int m = 0; m < m1; m+= 6e021)
     {
	  double F = (G * m1 * m2) / r; //Fuerza en N
	  outfile << m <<" kgs" << "|" << F << " N" << endl;
     }
     return 0;
}


this is way like I'll do it, are you agree pote? Thanks !
Topic archived. No new replies allowed.