Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.21 KB | None | 0 0
  1. package com.company;
  2. import java.io.*;
  3.  
  4. public class Main {
  5.     public static void main(String[] args) throws IOException {
  6.         euler();
  7.     }
  8.  
  9.     private static void euler() throws IOException {
  10.         double coelhos;
  11.         double raposas;
  12.         double a11 = 5;
  13.         double a12 = 0.05;
  14.         double a21 = 0.0004;
  15.         double a22 = 0.2;
  16.         double[] steps = {1, 0.5, 0.25, 0.1};
  17.         String[] filePaths = {"..\\eulerStep1.csv", "..\\eulerStep0.5.csv", "..\\eulerStep0.25.csv", "..\\eulerStep0.1.csv"};
  18.  
  19.         double step;
  20.         String filePath;
  21.         String valuesC;
  22.         String valuesR;
  23.         for (int s=0; s<4; s++){
  24.             step = steps[s];
  25.             filePath = filePaths[s];
  26.             valuesC = "";
  27.             valuesR = "";
  28.             coelhos = 520;
  29.             raposas = 85;
  30.  
  31.             System.out.println("------>STEP: " + step + "<------");
  32.             for(int i=0; i<20/step; i++){ //qual destes primeiro? Coelhos ou raposas?
  33.                 coelhos = coelhos + step * (a11*coelhos - a12*coelhos*raposas);
  34.                 raposas = raposas + step * (a21*coelhos*raposas - a22*raposas);
  35.  
  36.                 if (coelhos<=0){
  37.                     coelhos = 0;
  38.                     raposas = (raposas + step * a21*coelhos*raposas - a22*raposas);
  39.  
  40.                 }
  41.                 if (raposas<=0){
  42.                     raposas = 0;
  43.                     coelhos = (coelhos + step * a11*coelhos - a12*coelhos*raposas);
  44.                 }
  45.                 //Serem os dois zero
  46.  
  47.                 //Vai deixar "," no final
  48.                 valuesC += (int)coelhos + ",";
  49.                 valuesR += (int)raposas + ",";
  50.                 System.out.println("Coelhos: " + (int)coelhos + " Raposas: " + (int)raposas);
  51.             }
  52.             writeCSV(valuesC, valuesR, filePath);
  53.         }
  54.     }
  55.  
  56.     //values: String with "," -> CSV file
  57.     public static void writeCSV(String valuesC, String valuesR, String filePath) throws IOException {
  58.         FileWriter fileWriter = new FileWriter(filePath);
  59.         PrintWriter printWriter = new PrintWriter(fileWriter);
  60.         printWriter.println(valuesC);
  61.         printWriter.println(valuesR);
  62.         printWriter.close();
  63.     }
  64. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement