Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.io.*;
- public class Main {
- public static void main(String[] args) throws IOException {
- euler();
- }
- private static void euler() throws IOException {
- double coelhos;
- double raposas;
- double a11 = 5;
- double a12 = 0.05;
- double a21 = 0.0004;
- double a22 = 0.2;
- double[] steps = {1, 0.5, 0.25, 0.1};
- String[] filePaths = {"..\\eulerStep1.csv", "..\\eulerStep0.5.csv", "..\\eulerStep0.25.csv", "..\\eulerStep0.1.csv"};
- double step;
- String filePath;
- String valuesC;
- String valuesR;
- for (int s=0; s<4; s++){
- step = steps[s];
- filePath = filePaths[s];
- valuesC = "";
- valuesR = "";
- coelhos = 520;
- raposas = 85;
- System.out.println("------>STEP: " + step + "<------");
- for(int i=0; i<20/step; i++){ //qual destes primeiro? Coelhos ou raposas?
- coelhos = coelhos + step * (a11*coelhos - a12*coelhos*raposas);
- raposas = raposas + step * (a21*coelhos*raposas - a22*raposas);
- if (coelhos<=0){
- coelhos = 0;
- raposas = (raposas + step * a21*coelhos*raposas - a22*raposas);
- }
- if (raposas<=0){
- raposas = 0;
- coelhos = (coelhos + step * a11*coelhos - a12*coelhos*raposas);
- }
- //Serem os dois zero
- //Vai deixar "," no final
- valuesC += (int)coelhos + ",";
- valuesR += (int)raposas + ",";
- System.out.println("Coelhos: " + (int)coelhos + " Raposas: " + (int)raposas);
- }
- writeCSV(valuesC, valuesR, filePath);
- }
- }
- //values: String with "," -> CSV file
- public static void writeCSV(String valuesC, String valuesR, String filePath) throws IOException {
- FileWriter fileWriter = new FileWriter(filePath);
- PrintWriter printWriter = new PrintWriter(fileWriter);
- printWriter.println(valuesC);
- printWriter.println(valuesR);
- printWriter.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement