How to Incorporate a Numerical Solution into a Physics Java Applet?

In summary, incorporating a numerical solution into a physics Java applet requires understanding the mathematical principles behind the problem, implementing the appropriate algorithms and equations, and testing and debugging the code to ensure accuracy and efficiency. It is important to consider the limitations of numerical methods and to continuously improve and optimize the code for better performance. By following these steps, a numerical solution can be seamlessly integrated into a physics Java applet for accurate and efficient simulations.
  • #1
Robben
166
2

Homework Statement


[/B]
I would like to make a physics applet.

I have a numerical solution of the Lotka-Volterra model located here:
https://www.physicsforums.com/threads/lotka-volterra-model-eulers-method.803382/#post-5044961


Homework Equations



None

The Attempt at a Solution


[/B]
I want to be able to make graphs of my numerical solution and I am wondering how do I incorporate my numerical solution of the Lotka-Volterra model into an applet?

I tried searching GUI applets for Java but it is so broad the I would like to narrow it down for only physics application like making graphs or making a wave that corresponds to the wave equation.

Any links or videos of how to make a physics applet would be of great help.

My numerical solution:

Java:
public class Lotka {
    public static void main(String[] args) {
        double[] prey = new double[1000];
        double[] predator = new double[1000];
        double[] dt = new double[101];
        double[] b = new double[101];
        prey[0] = 10;
        predator[0] = 5;   
        dt[0] = 0;
        b[0] = 0.01;   
   
        for (int j = 1; j < 101; j ++) {
            dt[j] = 2 + dt[j-1];
            b[j] = b[0]*((double)j/2);  
        }    
   
        for(int i = 1; i < 100; i++) {
            prey[i] = prey[i-1] + (0.2*prey[i-1]*(1-prey[i-1]/500)
               -(b[i]*prey[i-1]*predator[i-1])/(1+b[i]*prey[i-1]))*(1/dt[i]);
            predator[i] = predator[i-1] + (0.1*predator[i-1]*(1-predator[i-1]
                /(0.2*prey[i-1])))*(1/dt[i]);
            //System.out.println("" + prey[i] + " " + predator[i]);  
        }      
    }      
}
 
Technology news on Phys.org
  • #2
Checkout Open Source Physics at www.compadre.org/osp

They have a whole collection of java software for making physics simulations including charting and ODE solvers.
 
  • #3
jedishrfu said:
Checkout Open Source Physics at www.compadre.org/osp

They have a whole collection of java software for making physics simulations including charting and ODE solvers.

Thanks for the link! I will be studying it now.
 
  • #4
jedishrfu said:
Checkout Open Source Physics at www.compadre.org/osp

They have a whole collection of java software for making physics simulations including charting and ODE solvers.

I noticed, in order to draw a line in my java applet it must be integers, i.e. for Graphics g then g.drawLine() must have int values and not double. Since my Lotka model numerical solution has double values from 10.0 to 9.87 (for the prey) and 4.99 to 4.82 (for the predator) how do I go about doing this? How do I get my drawline to take in double values instead of int?
 
  • #5
you have to develop scaling for what you want to draw

So say you decide your image is 1000x1000 pixels and your measurements range from 0cm to 15cm then
to draw a 3cm line you must compute linelength = 3 * 1000 / 15 = 200 pixels...
 
  • #6
[/code][/QUOTE]
jedishrfu said:
you have to develop scaling for what you want to draw

So say you decide your image is 1000x1000 pixels and your measurements range from 0cm to 15cm then
to draw a 3cm line you must compute linelength = 3 * 1000 / 15 = 200 pixels...

Hm, my measurements are

##p_0 = 10, \ q _0 = 5##
##p_1 = 9.946, \ q_1 = 4.925##
##p_2 = 9.89538, \ q_2 = 4.85231##
##p_3 = 9.84803, \ q_3 = 4.78187##
##\ldots## up to ##4.99## for predator (q) and ##10.0## for prey (p). Now my image pixel size is arbitrary and I just extended Applet, i.e. I have the following code in my applet:

Java:
import java.applet.*;
import java.awt.*;
import java.awt.event.*;

   public class LotkaApplet extends Applet
   implements MouseListener, ActionListener, Runnable{
...
}
 
  • #7
Use getWidth() and getHeight() java applet methods to get the canvas size .
 
  • #8
jedishrfu said:
Use getWidth() and getHeight() java applet methods to get the canvas size .
I think I would have to extend 2D graphics for this.
 

What is a physics Java applet?

A physics Java applet is a small program written in the Java programming language that is embedded into a web page. It is designed to simulate or demonstrate a physics concept or experiment.

What are the benefits of using a physics Java applet?

Using a physics Java applet allows for interactive and visual learning. It can help students better understand and visualize complex physics concepts. It can also make learning more engaging and fun.

Do I need to know Java to use a physics Java applet?

No, you do not need to have any prior knowledge of Java to use a physics Java applet. It is designed to be user-friendly and can be run by anyone with a web browser.

Can I customize a physics Java applet?

Yes, most physics Java applets have customizable parameters that allow users to change variables and see the effects on the simulation. Some applets also have options to change the appearance or add additional features.

Are there any safety concerns when using a physics Java applet?

No, since physics Java applets are run within a web browser, there are no safety concerns. However, it is always important to use caution and follow safety guidelines when conducting any type of physics experiment or simulation.

Similar threads

  • Programming and Computer Science
Replies
3
Views
1K
  • Programming and Computer Science
Replies
7
Views
2K
  • Programming and Computer Science
Replies
8
Views
1K
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
Replies
1
Views
943
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
4
Views
607
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
1
Views
2K
  • Programming and Computer Science
Replies
4
Views
1K
Back
Top