NTNUJAVA Virtual Physics LaboratoryEnjoy the fun of physics with simulations! Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
December 18, 2017, 09:52:05 pm

"There is nothing either good or bad, but thinking makes it so." ..."Shakespeare (154-1616, English dramatist and poet) "

 Pages: [1]   Go Down
 Author Topic: How to make a triangle wave?  (Read 16667 times) 0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message on: October 01, 2010, 06:57:04 pm » posted from:CornellÃ¡,Catalonia,Spain

Hello friends, I have a problem, I make a sine wave with "A*Math.sin(2*Math.PI*fr*t);", but I don't know how to do a triangle wave. Anyone can help me? thanks! Sorry for me English.
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #1 on: October 01, 2010, 10:54:35 pm » posted from:,,Taiwan

A triangle wave can be generated from sum of many sinwave.

 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #2 on: October 02, 2010, 01:56:12 am » posted from:CornellÃ¡,Catalonia,Spain

I'm noob in EJS. There is no other way to make a triangle wave?
Thanks.
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #3 on: October 02, 2010, 02:17:15 am » posted from:,,Taiwan

There are several ways to generate a triangle wave in EJS.

I would suggest you draw a triangle wave in a piece of paper.
Then draw similar segments with java code.

If you want to do it in EJS:
You can define x,y array to represent (x,y) coordinate for triangle wave.
Use a loop to assign correct value for triangle wave (several segment of line)
then assign x,y to polygon GUI element to draw it.

 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #4 on: October 02, 2010, 03:28:06 am » posted from:CornellÃ¡,Catalonia,Spain

I'm very lost, thanks anyway.
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #5 on: October 02, 2010, 09:28:51 am » posted from:,,Taiwan

I hope the following is what you are looking for.

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
• Please feel free to post your ideas about how to use the simulation for better teaching and learning.
• Post questions to be asked to help students to think, to explore.
• Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!
 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #6 on: October 02, 2010, 05:47:22 pm » posted from:Barcelona,Catalonia,Spain

Thank you very much for your time, that is the triangular wave I want to create, but in my simulation, I have x = t and y = i = sin(t), the graph is a trace, in their simulation is a polygon where x = x, y = y. I'm sending you my EJS's file, could you look it? Thank you.
I'm studying their simulation.
 *** There are 1 more attached files. You need to login to acces it! Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #7 on: October 02, 2010, 08:36:25 pm » posted from:,,Taiwan

There are too many file. I do not know which one to look for.

You can use trace GUI element to add one point at a time for each time step.
However, you can use polygon GUI elements to draw pre-defined number of connected points.
You just need to define x,y as array with n elements in EJS variable page.
then assign (x,y) with a for loop in EJS "fixed relation" page.
Add ploygin GUI element under drawingPanel2D, assign x,y as "x/y data" proper,
and EJS will show the polygons when you run it.

The following code is added at initialize page
Code:
k=2*pi/L; w=2*pi/T;
for(int i=0;i<n;i++){
x[i]=i*dx;
}

And the following code is added at fixed relation page
Code:
for(int i=0;i<n;i++){
cta=(k*x[i]+w*t)%pi2;
if(cta>pi)y[i]=A*(1-(cta-pi)/pi);
else y[i]=cta/pi*A;
}
 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #8 on: October 05, 2010, 05:45:57 pm » posted from:Barcelona,Catalonia,Spain

Hello gentleman Fu-Kwun Hwang, I send my EJS's source code,you will see my low level in java. For me it is very difficult to understand his triangular wave.
You could help put the triangular wave in my project?
 *** There are 1 more attached files. You need to login to acces it! Logged
lookang
Hero Member

Offline

Posts: 1784

http://weelookang.blogspot.com

 « Embed this message Reply #9 on: October 05, 2010, 09:02:17 pm »

i help u

do post up your applet here at NTNUJAVA Virtual Physics Laboratory say http://www.phy.ntnu.edu.tw/ntnujava/index.php?board=28.0 or any relevant board?
your applet looks like an electronic topic maybe http://www.phy.ntnu.edu.tw/ntnujava/index.php?board=16.0

do share your work (applet and worksheets etc) here! we are nurturing a learning community of Ejs users.
 *** There are 1 more attached files. You need to login to acces it! « Last Edit: October 05, 2010, 09:09:21 pm by lookang » Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #10 on: October 05, 2010, 10:33:41 pm » posted from:Barcelona,Catalonia,Spain

Thanks for the simulation! I have to study the triangular wave, because I don't understand the parameters. Thanks for you time!
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #11 on: October 05, 2010, 10:48:31 pm » posted from:,,Taiwan

But I can not figure out what you were doing and what kind of help you need.

Please describe in detail what you want to do and how can we help!
Check out the previous example if you just need to make a triangle wave.
y=f(x,t) increasing when (t%T) < T/2 and decreasing when (t%T)>T/2
 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #12 on: October 06, 2010, 10:19:42 pm » posted from:Barcelona,Catalonia,Spain

Hello sir, I have a problem with her simulation.
I need to know how to modify the wave.
I put a picture.

The first image is the wave that I want do.
The second is her simulation.
 Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #13 on: October 06, 2010, 11:56:11 pm » posted from:,,Taiwan

You need to provide relation between x,y coordinates to tell EJS or any program to draw it.
You can treat the lsft image as segment of three different lines.
A line can be represented by y=m*x +b
Try to find out those three equations for three drifferent segments.
 Logged
DaZyS
Newbie

Offline

Posts: 14

 « Embed this message Reply #14 on: October 08, 2010, 03:05:02 am » posted from:Barcelona,Catalonia,Spain

Thanks to Fu-Kwun Hwang and lookang !!!
Triangular wave is here!!

the code is:

Quote
if(triangularWave)
{
for(int i=0;i<n;i++){

cta=(x+w*t)%pi2;

if (cta<(pi/2)) y=2*cta/pi*A;
else y=2*A*(1-(cta-pi)/pi)-A*2;
if (cta>pi) y=2*A*(1-(cta-pi)/pi)-2*A;
if (cta>(3*pi/2)) y=-2*A*(1-(cta-pi)/pi);

return y;
}
 « Last Edit: October 08, 2010, 03:07:34 am by DaZyS » Logged
Fu-Kwun Hwang
Hero Member

Offline

Posts: 3080

 « Embed this message Reply #15 on: October 08, 2010, 03:22:35 pm » posted from:Taipei,T'ai-pei,Taiwan

Great! you make it by yourself.

It is also possible to do it with just one pair of (if and else) code.
Just shift the origin of your coordinate system.

I will leave it as an exercise for you!
 Logged
 Pages: [1]   Go Up
"There is nothing either good or bad, but thinking makes it so." ..."Shakespeare (154-1616, English dramatist and poet) "