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

Click For Summary
SUMMARY

The discussion focuses on creating a graph of a damped oscillator's position over time using the "philsplot" library in C. The equation utilized is x = A*e^((-b/4)*t)cos(omega*t), with parameters A = 10, m = 2, and user-defined values for b and omega. The user initially faced issues with graphing but successfully resolved them. The code provided includes necessary functions for plotting and calculating the oscillator's position.

PREREQUISITES
  • Understanding of damped oscillation equations
  • Familiarity with C programming language
  • Knowledge of the "philsplot" library for graphing
  • Basic concepts of exponential and trigonometric functions
NEXT STEPS
  • Explore advanced features of the "philsplot" library
  • Learn about numerical methods for solving differential equations
  • Investigate other libraries for graphing in C, such as "gnuplot" or "SDL"
  • Study the physics of oscillations and damping effects in more detail
USEFUL FOR

Students in scientific programming courses, physics enthusiasts, and developers interested in graphing mathematical functions using C.

Immanuel Can
Messages
18
Reaction score
0
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;}
 
Physics news on Phys.org
Nevermind! Figured it out.
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
9
Views
2K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 3 ·
Replies
3
Views
2K
Replies
6
Views
3K
  • · Replies 5 ·
Replies
5
Views
4K