NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

Easy Java Simulations (2001- ) => Questions related to EJS => Topic started by: DaZyS on October 01, 2010, 06:57:04 pm



Title: How to make a triangle wave?
Post by: DaZyS on October 01, 2010, 06:57:04 pm
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.


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 01, 2010, 10:54:35 pm
A triangle wave can be generated from sum of many sinwave.

Please check out Fourier Synthesis (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=17.0)


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 02, 2010, 01:56:12 am
I'm noob in EJS. There is no other way to make a triangle wave?
Thanks.


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 02, 2010, 02:17:15 am
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.



Title: Re: How to make a triangle wave?
Post by: DaZyS on October 02, 2010, 03:28:06 am
I'm very lost, thanks anyway.


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 02, 2010, 09:28:51 am
I hope the following is what you are looking for.


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 02, 2010, 05:47:22 pm
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.


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 02, 2010, 08:36:25 pm
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;
}


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 05, 2010, 05:45:57 pm
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?
Thanks for your time.


Title: Re: How to make a triangle wave?
Post by: lookang 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.


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 05, 2010, 10:33:41 pm
Thanks for the simulation! I have to study the triangular wave, because I don't understand the parameters. Thanks for you time!


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 05, 2010, 10:48:31 pm
I downloaded your Actual Memristor.ejs file and compiled it with EJS.
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


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 06, 2010, 10:19:42 pm
Hello sir, I have a problem with her simulation.
I need to know how to modify the wave.
I put a picture.
(http://img408.imageshack.us/img408/8497/comparativai.jpg)

The first image is the wave that I want do.
The second is her simulation.


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 06, 2010, 11:56:11 pm
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.


Title: Re: How to make a triangle wave?
Post by: DaZyS on October 08, 2010, 03:05:02 am
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;
}
;D


Title: Re: How to make a triangle wave?
Post by: Fu-Kwun Hwang on October 08, 2010, 03:22:35 pm
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!  :D