Create a graph of the position of a damped oscillator as a function of time.

  • #1
This is an assignment for a class titled "Intro to Scientific Programming" and it is a prerequisite for Computational Physics.

Homework Statement



Create a graph of the position of a damped oscillator as a function of time.


Homework Equations



The equation is x = A*e^((-b/2m)*t)cos(omega*t + phi) where phi = 0 and m = 2.

The Attempt at a Solution



I'm having a hard time debugging this. There are no compiler errors, I just can't get it to graph the function. The graph is made using some library called "philsplot". Here is the code,
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include "philsplot.h"

double function(double A, double b, double omega, double t);


main() {

double position;
double A;
double b;
double t;
double omega;
A = 10;



//asking for input
printf("Enter the dampening term: \n");
scanf("%lf", &b);
printf("Enter the angular frequency: \n");
scanf("%lf", &omega);


//converting degrees to radians
omega = (omega/180) * (4.0*atan(1.0));


//plotting & opening

double xmin, xmax, ymin, ymax, tsize;
int color;
double x,y;

open_plot("600x600");

xmin=0;
xmax=50;
ymin=-50;
ymax=50;
color=4;
tsize=1.5;
int icolor2 = 3;
int istyle = 1;
int iwidth = 1;

box_plot(xmin, xmax, ymin, ymax, tsize, color, "X", "T", "", "");


x = 0;
y = 0;

locate_plot(x,y);

int p;
while(p) {
//drawing graph
for (t=0; t<50; t+=.01) {
x = t;
//finding value
y = function(A, b, omega, t);

delay_plot(.5);
flush_plot();
drawto_plot(x,y, icolor2, istyle, iwidth);
flush_plot();

}

}

flush_plot();

}

//equation=> x = Ae^((-b/2m)t)cos(omegat + phi), phi = 0, m = 2
double function(double A, double b, double omega, double t) {

double x;

x = A * exp( (-b/4)*t ) * cos(omega*t);

return x;


}
 

Answers and Replies

  • #2
Nevermind! Figured it out.
 

Related Threads on Create a graph of the position of a damped oscillator as a function of time.

Replies
3
Views
2K
Replies
1
Views
3K
Replies
6
Views
2K
Replies
9
Views
2K
  • Last Post
Replies
4
Views
723
  • Last Post
2
Replies
33
Views
6K
Replies
0
Views
2K
Replies
5
Views
2K
Replies
1
Views
2K
Top