C++ code; unforced damped oscillator

In summary: I'm getting the following error:Linking console executable: C:\Program Files\CodeBlocks\MinGW\Untitled1.exeC:\Program Files\CodeBlocks\MinGW\bin\ld.exe: cannot open output file C:\Program Files\CodeBlocks\MinGW\Untitled1.exe: Permission deniedcollect2: ld returned 1 exit statusI'm using:#include <stdio.h>#include <math.h>main(){int time;int lower, upper, step;lower = 0;upper = 300;step = 1;float gamma = 0.01, omega = 100, A
  • #1
mathman44
207
0

Homework Statement



I have an assignment to make a C++ program (I've never seen C++ before, and my professor has never taught it) that makes a set of displacement values corresponding to the motion of a damped oscillator. The function is:

[tex]x = A*e^{(-\gamma*t/2)} * cos(\omega*t)[/tex]

where variables are to "float", except time. Time should be defined as "int" and use it as a counter variable. [tex]\gamma[/tex] and [tex]\omega[/tex] are to be defined arbitrarily. For each time value, print out the time and the displacement.

I have absolutely no background with C++. After googling for some time, I downloaded Code::Blocks and now I have to write the file. I have never seen C++ before... so I feel kind of bad for not showing any work, but I literally have no idea how to do this. This is all I have:

#include <stdio.h>
main()
{
int time;


int lower, upper, step;
lower = 0;
upper = 300;
step = 1;


Would anyone be kind enough to lend me some hints or give me a template?
 
Physics news on Phys.org
  • #2
Code:
float gamma = 1.0, omega = 0.1, A=1.0;
for(int t=lower; t<upper; t+=step)
{
  float x = A * exp(-gamma*t/2) * cos(omega*t);
  printf("%d\t%f\n", t, x);
}

Oh and you also need to #include <math.h> to use exp and cos.
 
  • #3
Thank you so much! One other question though:The file gets compiled, but now I get:

Linking console executable: C:\Program Files\CodeBlocks\MinGW\Untitled1.exe
C:\Program Files\CodeBlocks\MinGW\bin\ld.exe: cannot open output file C:\Program Files\CodeBlocks\MinGW\Untitled1.exe: Permission denied
collect2: ld returned 1 exit status

I'm using:

#include <stdio.h>
#include <math.h>
main()
{
int time;int lower, upper, step;
lower = 0;
upper = 300;
step = 1;
float gamma = 1.0, omega = 0.1, A=1.0;
for(int t=lower; t<upper; t+=step)
{
float x = A * exp(-gamma*t/2) * cos(omega*t);
printf("%d\t%f\n", t, x);
}
}

:S
 
  • #4
Is this on Vista? Most likely you don't have permission to write into C:\Program Files\CodeBlocks\MinGW\. Tinker with your command-line to make it put the executable someplace where you do have write access.
 
  • #5
Thanks, that solved it! One last thing: is it possible to have the program organize the output in a formatted table (with a heading and units)?
 
  • #6
Everything is possible. Add a printf() with the heading you want before the for().
 
  • #7
Thanks so much, you saved me
 
  • #8
:S I have:

#include <stdio.h>
#include <math.h>
main()
{
int time;


int lower, upper, step;
lower = 0;
upper = 300;
step = 1;
float gamma = 0.01, omega = 100, A=10000.0;
printf("Time(s) versus Displacement(m)\n");
for(int t=lower; t<upper; t+=step)
{
float x = A * exp(-gamma*t/2) * cos(omega*t);
printf("%d\t%f\n", t, x);
}
}

Which doesn't seem to be returning anything. Also how can I attach units to the output? Sorry to pester you guys...
 
  • #9
It writes to standard output. Start it from a command line.
You can write anything into the printf format string. use "man printf" on google to know more about the format string.
 
  • #10
I'm doing this on windows using C::B, by the way.
 

What is C++ code for an unforced damped oscillator?

The C++ code for an unforced damped oscillator involves using a mathematical model to simulate the motion of a damped oscillator without any external forces acting on it. This can be achieved by implementing the necessary equations and variables in the C++ programming language.

What is an unforced damped oscillator?

An unforced damped oscillator is a physical system that undergoes oscillatory motion due to an internal restoring force, such as a spring, while also experiencing a damping force that decreases its amplitude over time. This type of oscillator does not have any external forces acting on it, hence the term "unforced."

What is a damped oscillator in C++?

In C++, a damped oscillator is a program that simulates the behavior of a physical system undergoing oscillatory motion with a damping force. This involves implementing the necessary equations and variables in the C++ programming language to accurately model the motion of the oscillator.

What are the key components of a C++ code for an unforced damped oscillator?

The key components of a C++ code for an unforced damped oscillator include defining the necessary variables for the system (such as mass, spring constant, and damping coefficient), implementing the equations for the oscillator's motion, and setting up a loop to iterate through the system's behavior over time.

What is the purpose of using C++ for an unforced damped oscillator?

The purpose of using C++ for an unforced damped oscillator is to create a program that can accurately simulate the motion of a physical system with a damping force. C++ is a powerful and efficient programming language that allows for complex mathematical calculations and can provide visual representations of the oscillator's behavior through graphs or animations.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
15
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
17
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
8K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
873
  • Engineering and Comp Sci Homework Help
Replies
8
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
9
Views
1K
Back
Top