- #1
Immanuel Can
- 18
- 0
This is an assignment for a class titled "Intro to Scientific Programming" and it is a prerequisite for Computational Physics.
Create a graph of the position of a damped oscillator as a function of time.
The equation is x = A*e^((-b/2m)*t)cos(omega*t + phi) where phi = 0 and m = 2.
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,
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;}