Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
Hello,

I've got a Harmonic Oscillator define by :
f=-k/r + b/r^5 (central force // r the position // and I will call v the celerity)
if I did well the celerity is: v_n+1 = v_n + Delta_t *( - k/m*r+ b/m*1/r^5)
I call v_nr = Delta_t *( - k/m*r+ b/m*1/r^5)

And so the position r is: r_n+1 = r_n + Delta_t * v_nr +1/2.(r_n+1 - r_n-1)

I need to find the v(t) and r(t).
I first try to solve this without the b term (which is a perturbation term)

What I have tried:

C++
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#define M 3 // in kg
#define k 1.0  //  kg/sec^2
#define N 50   // interation

double T = 2*pi*(m/k)^0.5;

double main (void) {  // initiation
  double r=0.0; double rr=0.0;
  double v=10.0; double vr=0.0;
}

int main() {

    FILE *fichier;
    fichier = fopen("without b perturbation", "w+");
    int t=0 ;
    for(t = 0; t <= N; t++) {
        rr = r + T * v ;
        vr = v + T * k/m *r ;
        v = v + T * ((k * x / m) + (k * xint / m)) / 2.0;
        x = x + (h) * (v + vint) / 2.0;
	r = rr + T * (v+vr);
	v = v
	r=rr;
	fprintf(fichier, "%d\t%lf\n", t, yy);
	printf("%d\t%lf\n", t, yy);
    }
    fclose(fichier);
    return 0;
}
Posted
Updated 8-Jul-18 2:03am
v2
Comments
Patrice T 3-Jul-18 17:40pm    
And you have a question ?
Rick York 3-Jul-18 19:49pm    
As mentioned, there was no question asked.

I noticed an issue with the code - the ^ doesn't mean what it appears that think it does. It does not mean raised to a power in the C language. It means the bitwise exclusive or operator in the C language.

1 solution

You are trying to solve a 2nd-order differential equation, which is not as simple as it looks. One basic family of solvers is the Runge-Kutta family. Google for more information on derivation, how to use, etc.

Many other solvers exist as well, each optimized for a different set of equation characteristics.

BTW, your function double main(void) {...} is not legal C, and (as someone else has pointed out) '^' is not the exponentiation operator in C.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900