Начальный вариант проекта (ЛР-1 - ЛР-4)
This commit is contained in:
commit
58365241b3
73
build.xml
Normal file
73
build.xml
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<!-- You may freely edit this file. See commented blocks below for -->
|
||||||
|
<!-- some examples of how to customize the build. -->
|
||||||
|
<!-- (If you delete it and reopen the project it will be recreated.) -->
|
||||||
|
<!-- By default, only the Clean and Build commands use this build script. -->
|
||||||
|
<!-- Commands such as Run, Debug, and Test only use this build script if -->
|
||||||
|
<!-- the Compile on Save feature is turned off for the project. -->
|
||||||
|
<!-- You can turn off the Compile on Save (or Deploy on Save) setting -->
|
||||||
|
<!-- in the project's Project Properties dialog box.-->
|
||||||
|
<project name="NumericalMethods" default="default" basedir=".">
|
||||||
|
<description>Builds, tests, and runs the project NumericalMethods.</description>
|
||||||
|
<import file="nbproject/build-impl.xml"/>
|
||||||
|
<!--
|
||||||
|
|
||||||
|
There exist several targets which are by default empty and which can be
|
||||||
|
used for execution of your tasks. These targets are usually executed
|
||||||
|
before and after some main targets. They are:
|
||||||
|
|
||||||
|
-pre-init: called before initialization of project properties
|
||||||
|
-post-init: called after initialization of project properties
|
||||||
|
-pre-compile: called before javac compilation
|
||||||
|
-post-compile: called after javac compilation
|
||||||
|
-pre-compile-single: called before javac compilation of single file
|
||||||
|
-post-compile-single: called after javac compilation of single file
|
||||||
|
-pre-compile-test: called before javac compilation of JUnit tests
|
||||||
|
-post-compile-test: called after javac compilation of JUnit tests
|
||||||
|
-pre-compile-test-single: called before javac compilation of single JUnit test
|
||||||
|
-post-compile-test-single: called after javac compilation of single JUunit test
|
||||||
|
-pre-jar: called before JAR building
|
||||||
|
-post-jar: called after JAR building
|
||||||
|
-post-clean: called after cleaning build products
|
||||||
|
|
||||||
|
(Targets beginning with '-' are not intended to be called on their own.)
|
||||||
|
|
||||||
|
Example of inserting an obfuscator after compilation could look like this:
|
||||||
|
|
||||||
|
<target name="-post-compile">
|
||||||
|
<obfuscate>
|
||||||
|
<fileset dir="${build.classes.dir}"/>
|
||||||
|
</obfuscate>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
For list of available properties check the imported
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
|
||||||
|
Another way to customize the build is by overriding existing main targets.
|
||||||
|
The targets of interest are:
|
||||||
|
|
||||||
|
-init-macrodef-javac: defines macro for javac compilation
|
||||||
|
-init-macrodef-junit: defines macro for junit execution
|
||||||
|
-init-macrodef-debug: defines macro for class debugging
|
||||||
|
-init-macrodef-java: defines macro for class execution
|
||||||
|
-do-jar: JAR building
|
||||||
|
run: execution of project
|
||||||
|
-javadoc-build: Javadoc generation
|
||||||
|
test-report: JUnit report generation
|
||||||
|
|
||||||
|
An example of overriding the target for project execution could look like this:
|
||||||
|
|
||||||
|
<target name="run" depends="NumericalMethods-impl.jar">
|
||||||
|
<exec dir="bin" executable="launcher.exe">
|
||||||
|
<arg file="${dist.jar}"/>
|
||||||
|
</exec>
|
||||||
|
</target>
|
||||||
|
|
||||||
|
Notice that the overridden target depends on the jar target and not only on
|
||||||
|
the compile target as the regular run target does. Again, for a list of available
|
||||||
|
properties which you can use, check the target you are overriding in the
|
||||||
|
nbproject/build-impl.xml file.
|
||||||
|
|
||||||
|
-->
|
||||||
|
</project>
|
3
manifest.mf
Normal file
3
manifest.mf
Normal file
@ -0,0 +1,3 @@
|
|||||||
|
Manifest-Version: 1.0
|
||||||
|
X-COMMENT: Main-Class will be added automatically by build
|
||||||
|
|
1402
nbproject/build-impl.xml
Normal file
1402
nbproject/build-impl.xml
Normal file
File diff suppressed because it is too large
Load Diff
8
nbproject/genfiles.properties
Normal file
8
nbproject/genfiles.properties
Normal file
@ -0,0 +1,8 @@
|
|||||||
|
build.xml.data.CRC32=ba792dcd
|
||||||
|
build.xml.script.CRC32=3add4cfd
|
||||||
|
build.xml.stylesheet.CRC32=8064a381@1.63.0.46
|
||||||
|
# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml.
|
||||||
|
# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you.
|
||||||
|
nbproject/build-impl.xml.data.CRC32=ba792dcd
|
||||||
|
nbproject/build-impl.xml.script.CRC32=abc81b74
|
||||||
|
nbproject/build-impl.xml.stylesheet.CRC32=cdba79fa@1.67.0.46
|
73
nbproject/project.properties
Normal file
73
nbproject/project.properties
Normal file
@ -0,0 +1,73 @@
|
|||||||
|
annotation.processing.enabled=true
|
||||||
|
annotation.processing.enabled.in.editor=false
|
||||||
|
annotation.processing.processors.list=
|
||||||
|
annotation.processing.run.all.processors=true
|
||||||
|
annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output
|
||||||
|
application.title=NumericalMethods
|
||||||
|
application.vendor=aNNiMON
|
||||||
|
build.classes.dir=${build.dir}/classes
|
||||||
|
build.classes.excludes=**/*.java,**/*.form
|
||||||
|
# This directory is removed when the project is cleaned:
|
||||||
|
build.dir=build
|
||||||
|
build.generated.dir=${build.dir}/generated
|
||||||
|
build.generated.sources.dir=${build.dir}/generated-sources
|
||||||
|
# Only compile against the classpath explicitly listed here:
|
||||||
|
build.sysclasspath=ignore
|
||||||
|
build.test.classes.dir=${build.dir}/test/classes
|
||||||
|
build.test.results.dir=${build.dir}/test/results
|
||||||
|
# Uncomment to specify the preferred debugger connection transport:
|
||||||
|
#debug.transport=dt_socket
|
||||||
|
debug.classpath=\
|
||||||
|
${run.classpath}
|
||||||
|
debug.test.classpath=\
|
||||||
|
${run.test.classpath}
|
||||||
|
# This directory is removed when the project is cleaned:
|
||||||
|
dist.dir=dist
|
||||||
|
dist.jar=${dist.dir}/NumericalMethods.jar
|
||||||
|
dist.javadoc.dir=${dist.dir}/javadoc
|
||||||
|
endorsed.classpath=
|
||||||
|
excludes=
|
||||||
|
includes=**
|
||||||
|
jar.compress=false
|
||||||
|
javac.classpath=
|
||||||
|
# Space-separated list of extra javac options
|
||||||
|
javac.compilerargs=
|
||||||
|
javac.deprecation=false
|
||||||
|
javac.processorpath=\
|
||||||
|
${javac.classpath}
|
||||||
|
javac.source=1.6
|
||||||
|
javac.target=1.6
|
||||||
|
javac.test.classpath=\
|
||||||
|
${javac.classpath}:\
|
||||||
|
${build.classes.dir}
|
||||||
|
javac.test.processorpath=\
|
||||||
|
${javac.test.classpath}
|
||||||
|
javadoc.additionalparam=
|
||||||
|
javadoc.author=false
|
||||||
|
javadoc.encoding=${source.encoding}
|
||||||
|
javadoc.noindex=false
|
||||||
|
javadoc.nonavbar=false
|
||||||
|
javadoc.notree=false
|
||||||
|
javadoc.private=false
|
||||||
|
javadoc.splitindex=true
|
||||||
|
javadoc.use=true
|
||||||
|
javadoc.version=false
|
||||||
|
javadoc.windowtitle=
|
||||||
|
main.class=com.nummethods.lr4.LR_4
|
||||||
|
manifest.file=manifest.mf
|
||||||
|
meta.inf.dir=${src.dir}/META-INF
|
||||||
|
mkdist.disabled=false
|
||||||
|
platform.active=default_platform
|
||||||
|
run.classpath=\
|
||||||
|
${javac.classpath}:\
|
||||||
|
${build.classes.dir}
|
||||||
|
# Space-separated list of JVM arguments used when running the project.
|
||||||
|
# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value.
|
||||||
|
# To set system properties for unit tests define test-sys-prop.name=value:
|
||||||
|
run.jvmargs=
|
||||||
|
run.test.classpath=\
|
||||||
|
${javac.test.classpath}:\
|
||||||
|
${build.test.classes.dir}
|
||||||
|
source.encoding=windows-1251
|
||||||
|
src.dir=src
|
||||||
|
test.src.dir=test
|
15
nbproject/project.xml
Normal file
15
nbproject/project.xml
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://www.netbeans.org/ns/project/1">
|
||||||
|
<type>org.netbeans.modules.java.j2seproject</type>
|
||||||
|
<configuration>
|
||||||
|
<data xmlns="http://www.netbeans.org/ns/j2se-project/3">
|
||||||
|
<name>NumericalMethods</name>
|
||||||
|
<source-roots>
|
||||||
|
<root id="src.dir"/>
|
||||||
|
</source-roots>
|
||||||
|
<test-roots>
|
||||||
|
<root id="test.src.dir"/>
|
||||||
|
</test-roots>
|
||||||
|
</data>
|
||||||
|
</configuration>
|
||||||
|
</project>
|
35
src/com/nummethods/lr1/Epsilon.java
Normal file
35
src/com/nummethods/lr1/Epsilon.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.nummethods.lr1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate epsilon.
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class Epsilon {
|
||||||
|
|
||||||
|
public static void getEpsilon() {
|
||||||
|
System.out.println("Float epsilon: " + getEpsilonFloat());
|
||||||
|
System.out.println("Float constant epsilon: " + Float.MIN_VALUE);
|
||||||
|
System.out.println("Double epsilon: " + getEpsilonDouble());
|
||||||
|
System.out.println("Double constant epsilon: " + Double.MIN_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float getEpsilonFloat() {
|
||||||
|
float num = 1f;
|
||||||
|
float prevNumber = num;
|
||||||
|
while (num != 0f) {//1 + num != 1f) {
|
||||||
|
prevNumber = num;
|
||||||
|
num /= 2f;
|
||||||
|
}
|
||||||
|
return prevNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double getEpsilonDouble() {
|
||||||
|
double num = 1.0;
|
||||||
|
double prevNumber = 1.0;
|
||||||
|
while (num != 0.0) {
|
||||||
|
prevNumber = num;
|
||||||
|
num /= 2.0;
|
||||||
|
}
|
||||||
|
return prevNumber;
|
||||||
|
}
|
||||||
|
}
|
15
src/com/nummethods/lr1/LR_1.java
Normal file
15
src/com/nummethods/lr1/LR_1.java
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package com.nummethods.lr1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class LR_1 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
System.out.println("=== 1 ===");
|
||||||
|
Epsilon.getEpsilon();
|
||||||
|
MaxNumber.getMaxNumber();
|
||||||
|
System.out.println("\n=== 2 ===");
|
||||||
|
Taylor.getTaylor();
|
||||||
|
}
|
||||||
|
}
|
35
src/com/nummethods/lr1/MaxNumber.java
Normal file
35
src/com/nummethods/lr1/MaxNumber.java
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package com.nummethods.lr1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate max number.
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class MaxNumber {
|
||||||
|
|
||||||
|
public static void getMaxNumber() {
|
||||||
|
System.out.println("Float max number: " + getMaxNumberFloat());
|
||||||
|
System.out.println("Float constant max: " + Float.MAX_VALUE);
|
||||||
|
System.out.println("Double max number: " + getMaxNumberDouble());
|
||||||
|
System.out.println("Double constant max: " + Double.MAX_VALUE);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static float getMaxNumberFloat() {
|
||||||
|
float num = 1f;
|
||||||
|
float prevNumber = 1f;
|
||||||
|
while (num != Float.POSITIVE_INFINITY) {
|
||||||
|
prevNumber = num;
|
||||||
|
num *= 2f;
|
||||||
|
}
|
||||||
|
return prevNumber;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double getMaxNumberDouble() {
|
||||||
|
double num = 1.0;
|
||||||
|
double prevNumber = 1.0;
|
||||||
|
while (num != Double.POSITIVE_INFINITY) {
|
||||||
|
prevNumber = num;
|
||||||
|
num *= 2.0;//1.001
|
||||||
|
}
|
||||||
|
return prevNumber;
|
||||||
|
}
|
||||||
|
}
|
105
src/com/nummethods/lr1/Taylor.java
Normal file
105
src/com/nummethods/lr1/Taylor.java
Normal file
@ -0,0 +1,105 @@
|
|||||||
|
package com.nummethods.lr1;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Calculate Taylor series
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class Taylor {
|
||||||
|
|
||||||
|
public static void getTaylor() {
|
||||||
|
final int x = 2;
|
||||||
|
System.out.println("Taylor (long): " + taylorLongFactorial(x));
|
||||||
|
System.out.println("Taylor (double): " + taylorDoubleFactorial(x));
|
||||||
|
System.out.println("Taylor (w/o factorial long): " + taylorWithoutFactorialLong(x));
|
||||||
|
System.out.println("Taylor (w/o factorial double): " + taylorWithoutFactorialDouble(x));
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double taylorLongFactorial(int x) {
|
||||||
|
long sum = 0;
|
||||||
|
long tempSum;
|
||||||
|
int n = 0;
|
||||||
|
do {
|
||||||
|
int temp = 2 * n + 1;
|
||||||
|
tempSum = (long) Math.pow(-1, n);
|
||||||
|
tempSum *= Math.pow(x, temp);
|
||||||
|
tempSum /= factorialLong(n) * temp;
|
||||||
|
|
||||||
|
sum += tempSum;
|
||||||
|
n++;
|
||||||
|
} while(tempSum != 0);
|
||||||
|
System.out.println("N=" + n);
|
||||||
|
double sum2 = sum * (2.0 / Math.sqrt(Math.PI));
|
||||||
|
|
||||||
|
return sum2;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double taylorDoubleFactorial(int x) {
|
||||||
|
double sum = 0.0;
|
||||||
|
double tempSum;
|
||||||
|
int n = 0;
|
||||||
|
do {
|
||||||
|
int temp = 2 * n + 1;
|
||||||
|
tempSum = Math.pow(-1, n);
|
||||||
|
tempSum *= Math.pow(x, temp);
|
||||||
|
tempSum /= factorialDouble(n) * temp;
|
||||||
|
|
||||||
|
sum += tempSum;
|
||||||
|
n++;
|
||||||
|
} while(tempSum != 0);
|
||||||
|
System.out.println("N=" + n);
|
||||||
|
sum *= (2.0 / Math.sqrt(Math.PI));
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double taylorWithoutFactorialLong(int x) {
|
||||||
|
long sum = 0;
|
||||||
|
long tempSum = x;
|
||||||
|
int n = 0;
|
||||||
|
do {
|
||||||
|
sum += tempSum;
|
||||||
|
n++;
|
||||||
|
|
||||||
|
tempSum *= -1 * (2 * n - 1) * (x * x);
|
||||||
|
tempSum /= n * (2 * n + 1);
|
||||||
|
} while(tempSum != 0);
|
||||||
|
System.out.println("N=" + n);
|
||||||
|
|
||||||
|
double sum2 = sum * (2.0 / Math.sqrt(Math.PI));
|
||||||
|
|
||||||
|
return sum2;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double taylorWithoutFactorialDouble(int x) {
|
||||||
|
double sum = 0.0;
|
||||||
|
double tempSum = x;
|
||||||
|
int n = 0;
|
||||||
|
do {
|
||||||
|
sum += tempSum;
|
||||||
|
n++;
|
||||||
|
|
||||||
|
tempSum *= -1 * (2 * n - 1) * (x * x);
|
||||||
|
tempSum /= n * (2 * n + 1);
|
||||||
|
} while(Math.abs(tempSum) != 0);
|
||||||
|
System.out.println("N=" + n);
|
||||||
|
sum *= (2.0 / Math.sqrt(Math.PI));
|
||||||
|
|
||||||
|
return sum;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static long factorialLong(int n) {
|
||||||
|
long factorial = 1;
|
||||||
|
for (int i = 2; i <= n; i++) {
|
||||||
|
factorial *= i;
|
||||||
|
}
|
||||||
|
return factorial;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static double factorialDouble(int n) {
|
||||||
|
double factorial = 1.0;
|
||||||
|
for (int i = 2; i <= n; i++) {
|
||||||
|
factorial *= i;
|
||||||
|
}
|
||||||
|
return factorial;
|
||||||
|
}
|
||||||
|
}
|
16
src/com/nummethods/lr2/LR_2.java
Normal file
16
src/com/nummethods/lr2/LR_2.java
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
package com.nummethods.lr2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class LR_2 {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
final String name = "v10";
|
||||||
|
SLAE slae = new SLAE("/com/nummethods/lr2/" + name + ".slae");
|
||||||
|
System.out.println(slae.toString());
|
||||||
|
Vector answer = slae.gauss();
|
||||||
|
System.out.println("\nAnswer: " + answer.toString());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
59
src/com/nummethods/lr2/Matrix.java
Normal file
59
src/com/nummethods/lr2/Matrix.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package com.nummethods.lr2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class Matrix {
|
||||||
|
|
||||||
|
private double[][] coeff;
|
||||||
|
|
||||||
|
public Matrix(int rank) {
|
||||||
|
coeff = new double[rank][rank];
|
||||||
|
}
|
||||||
|
|
||||||
|
public double[][] get() {
|
||||||
|
return coeff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getRank() {
|
||||||
|
return coeff.length;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double get(int y, int x) {
|
||||||
|
return coeff[y][x];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int y, int x, double value) {
|
||||||
|
coeff[y][x] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector getLine(int y) {
|
||||||
|
Vector v = new Vector(coeff[y]);
|
||||||
|
return v;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setLine(int y, Vector row) {
|
||||||
|
coeff[y] = row.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swap(int i1, int i2) {
|
||||||
|
double[] temp = coeff[i1];
|
||||||
|
coeff[i1] = coeff[i2];
|
||||||
|
coeff[i2] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
final int rank = coeff.length;
|
||||||
|
for (int i = 0; i < rank; i++) {
|
||||||
|
for (int j = 0; j < rank; j++) {
|
||||||
|
sb.append(coeff[i][j]).append("\t");
|
||||||
|
}
|
||||||
|
sb.append("\n");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
119
src/com/nummethods/lr2/SLAE.java
Normal file
119
src/com/nummethods/lr2/SLAE.java
Normal file
@ -0,0 +1,119 @@
|
|||||||
|
package com.nummethods.lr2;
|
||||||
|
|
||||||
|
import java.io.BufferedReader;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.io.InputStreamReader;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Read and calculate SLAE by gauss method.
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class SLAE {
|
||||||
|
|
||||||
|
private Matrix A;
|
||||||
|
private Vector d;
|
||||||
|
|
||||||
|
public SLAE(String resource) {
|
||||||
|
try {
|
||||||
|
read(getClass().getResourceAsStream(resource));
|
||||||
|
} catch (IOException ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector gauss() {
|
||||||
|
int rank = A.getRank();
|
||||||
|
for (int row = 0; row < rank; row++) {
|
||||||
|
int best = row;
|
||||||
|
for (int i = row + 1; i < rank; i++) {
|
||||||
|
if (Math.abs(A.get(best, row)) < Math.abs(A.get(i, row))) {
|
||||||
|
best = i;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
A.swap(row, best);
|
||||||
|
d.swap(row, best);
|
||||||
|
|
||||||
|
for (int i = row + 1; i < rank; i++) {
|
||||||
|
double value = A.get(row, i) / A.get(row, row);
|
||||||
|
A.set(row, i, value);
|
||||||
|
// a[row][i] /= a[row][row];
|
||||||
|
}
|
||||||
|
if (Math.abs(A.get(row, row)) <= 1e-9) System.out.println("Ñèñòåìà íåñîâìåñòíà");
|
||||||
|
d.set(row, d.get(row) / A.get(row, row));
|
||||||
|
for (int i = 0; i < rank; i++) {
|
||||||
|
double x = A.get(i, row);
|
||||||
|
if (i != row && x != 0) {
|
||||||
|
for (int j = row + 1; j < rank; j++) {
|
||||||
|
double value = A.get(i, j) - A.get(row, j) * x;
|
||||||
|
A.set(i, j, value);
|
||||||
|
}
|
||||||
|
d.set(i, d.get(i) - d.get(row) * x);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
for (int i = 0; i < A.getRank(); i++) {
|
||||||
|
double y = 0;
|
||||||
|
for (int j = 0; j < A.getRank(); j++) {
|
||||||
|
y += A.get(i, j) * d.get(j);
|
||||||
|
}
|
||||||
|
if (Math.abs(d.get(i) - y) < 1e-9) {
|
||||||
|
System.err.println("Îøèáêà");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return d;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void read(InputStream is) throws IOException {
|
||||||
|
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
|
||||||
|
final int rank = Integer.parseInt(getNonCommentString(reader));
|
||||||
|
// Read matrix.
|
||||||
|
Matrix matrix = new Matrix(rank);
|
||||||
|
for (int y = 0; y < rank; y++) {
|
||||||
|
Vector v = getVectorFromString(getNonCommentString(reader));
|
||||||
|
matrix.setLine(y, v);
|
||||||
|
}
|
||||||
|
//System.out.print(matrix.toString());
|
||||||
|
Vector vector = getVectorFromString( getNonCommentString(reader) );
|
||||||
|
//System.out.println("\n" + vector.toString());
|
||||||
|
|
||||||
|
reader.close();
|
||||||
|
|
||||||
|
A = matrix;
|
||||||
|
d = vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
private String getNonCommentString(BufferedReader reader) throws IOException {
|
||||||
|
while (true) {
|
||||||
|
String line = reader.readLine();
|
||||||
|
if (line == null) return "";
|
||||||
|
if (!line.startsWith(";")) return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private Vector getVectorFromString(String line) {
|
||||||
|
String[] items = line.split(",");
|
||||||
|
Vector vector = new Vector(items.length);
|
||||||
|
for (int i = 0; i < items.length; i++) {
|
||||||
|
double value = Double.parseDouble(items[i].trim() );
|
||||||
|
vector.set(i, value);
|
||||||
|
}
|
||||||
|
return vector;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
sb.append("Source SLAE");
|
||||||
|
for (int i = 0; i < A.getRank(); i++) {
|
||||||
|
sb.append('\n');
|
||||||
|
sb.append(A.getLine(i).toString()).append("=\t").append(d.get(i));
|
||||||
|
}
|
||||||
|
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
47
src/com/nummethods/lr2/Vector.java
Normal file
47
src/com/nummethods/lr2/Vector.java
Normal file
@ -0,0 +1,47 @@
|
|||||||
|
package com.nummethods.lr2;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class Vector {
|
||||||
|
|
||||||
|
private double[] coeff;
|
||||||
|
|
||||||
|
public Vector(int rank) {
|
||||||
|
coeff = new double[rank];
|
||||||
|
}
|
||||||
|
|
||||||
|
public Vector(double[] row) {
|
||||||
|
coeff = new double[row.length];
|
||||||
|
System.arraycopy(row, 0, coeff, 0, row.length);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double[] get() {
|
||||||
|
return coeff;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double get(int x) {
|
||||||
|
return coeff[x];
|
||||||
|
}
|
||||||
|
|
||||||
|
public void set(int x, double value) {
|
||||||
|
coeff[x] = value;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void swap(int i1, int i2) {
|
||||||
|
double temp = coeff[i1];
|
||||||
|
coeff[i1] = coeff[i2];
|
||||||
|
coeff[i2] = temp;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
final int rank = coeff.length;
|
||||||
|
for (int i = 0; i < rank; i++) {
|
||||||
|
sb.append(coeff[i]).append("\t");
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
}
|
11
src/com/nummethods/lr2/v10.slae
Normal file
11
src/com/nummethods/lr2/v10.slae
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
; -= <20>אנטאםע 10 =-
|
||||||
|
; נאםד לאענטצ<D798>
|
||||||
|
5
|
||||||
|
; לאענטצא A
|
||||||
|
5, 1.2, -2, 2.4, 1
|
||||||
|
1.8, 5, 1, 2.2, 4
|
||||||
|
3, 1.8, 1, 1.6, 1
|
||||||
|
4.5, 2, 4, 2.6, 2
|
||||||
|
7, 1, 2.4, 0, 9
|
||||||
|
; גוךעמנ d
|
||||||
|
9, 7, 2.5, 9, -8
|
11
src/com/nummethods/lr2/v2.slae
Normal file
11
src/com/nummethods/lr2/v2.slae
Normal file
@ -0,0 +1,11 @@
|
|||||||
|
; -= <20>אנטאםע 2 =-
|
||||||
|
; נאםד לאענטצ<D798>
|
||||||
|
5
|
||||||
|
; לאענטצא A
|
||||||
|
3, 1.2, -1, 2.2, 1
|
||||||
|
1.4, 3, 1, 2.6, 4
|
||||||
|
3, 1.4, 1, 1.6, 1
|
||||||
|
-2.5, 2, 4, 1.8, 1.5
|
||||||
|
-3, 1, 2.2, 1, 8
|
||||||
|
; גוךעמנ d
|
||||||
|
5, 5, 1.5, 5, -8
|
7
src/com/nummethods/lr2/v2_1.slae
Normal file
7
src/com/nummethods/lr2/v2_1.slae
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
; נאםד לאענטצ<D798>
|
||||||
|
2
|
||||||
|
; לאענטצא A
|
||||||
|
5, 1
|
||||||
|
-3, -3
|
||||||
|
; גוךעמנ d
|
||||||
|
13, -15
|
18
src/com/nummethods/lr3/AbstractInterpolation.java
Normal file
18
src/com/nummethods/lr3/AbstractInterpolation.java
Normal file
@ -0,0 +1,18 @@
|
|||||||
|
package com.nummethods.lr3;
|
||||||
|
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public abstract class AbstractInterpolation {
|
||||||
|
|
||||||
|
protected Point2D[] input;
|
||||||
|
|
||||||
|
public AbstractInterpolation(Point2D[] input) {
|
||||||
|
this.input = input;
|
||||||
|
}
|
||||||
|
|
||||||
|
public abstract double interpolate(double x);
|
||||||
|
}
|
96
src/com/nummethods/lr3/CubicSpline.java
Normal file
96
src/com/nummethods/lr3/CubicSpline.java
Normal file
@ -0,0 +1,96 @@
|
|||||||
|
package com.nummethods.lr3;
|
||||||
|
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
public class CubicSpline extends AbstractInterpolation {
|
||||||
|
|
||||||
|
private SplineTuple[] splines;
|
||||||
|
|
||||||
|
public CubicSpline(Point2D[] input) {
|
||||||
|
super(input);
|
||||||
|
buildSpline();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double interpolate(double x) {
|
||||||
|
if (splines == null) {
|
||||||
|
buildSpline();
|
||||||
|
}
|
||||||
|
|
||||||
|
final int n = splines.length;
|
||||||
|
SplineTuple s;
|
||||||
|
|
||||||
|
if (x <= splines[0].x) {
|
||||||
|
// Åñëè x ìåíüøå òî÷êè ñåòêè x[0] - ïîëüçóåìñÿ ïåðâûì ýë-òîâ ìàññèâà
|
||||||
|
s = splines[1];
|
||||||
|
} else if (x >= splines[n - 1].x) {
|
||||||
|
// Åñëè x áîëüøå òî÷êè ñåòêè x[n - 1] - ïîëüçóåìñÿ ïîñëåäíèì ýë-òîì ìàññèâà
|
||||||
|
s = splines[n - 1];
|
||||||
|
} else {
|
||||||
|
// Èíà÷å x ëåæèò ìåæäó ãðàíè÷íûìè òî÷êàìè ñåòêè - ïðîèçâîäèì áèíàðíûé ïîèñê íóæíîãî ýë-òà ìàññèâà
|
||||||
|
int i = 0;
|
||||||
|
int j = n - 1;
|
||||||
|
while (i + 1 < j) {
|
||||||
|
int k = i + (j - i) / 2;
|
||||||
|
if (x <= splines[k].x) {
|
||||||
|
j = k;
|
||||||
|
} else {
|
||||||
|
i = k;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
s = splines[j];
|
||||||
|
}
|
||||||
|
|
||||||
|
double dx = x - s.x;
|
||||||
|
// Âû÷èñëÿåì çíà÷åíèå ñïëàéíà â çàäàííîé òî÷êå.
|
||||||
|
return s.a + (s.b + (s.c / 2.0 + s.d * dx / 6.0) * dx) * dx;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void buildSpline() {
|
||||||
|
final int n = input.length;
|
||||||
|
// Èíèöèàëèçàöèÿ ìàññèâà ñïëàéíîâ
|
||||||
|
splines = new SplineTuple[n];
|
||||||
|
for (int i = 0; i < n; i++) {
|
||||||
|
splines[i] = new SplineTuple();
|
||||||
|
splines[i].x = input[i].getX();
|
||||||
|
splines[i].a = input[i].getY();
|
||||||
|
}
|
||||||
|
splines[0].c = splines[n - 1].c = 0.0;
|
||||||
|
|
||||||
|
// Ðåøåíèå ÑËÀÓ îòíîñèòåëüíî êîýôôèöèåíòîâ ñïëàéíîâ c[i] ìåòîäîì ïðîãîíêè äëÿ òðåõäèàãîíàëüíûõ ìàòðèö
|
||||||
|
// Âû÷èñëåíèå ïðîãîíî÷íûõ êîýôôèöèåíòîâ - ïðÿìîé õîä ìåòîäà ïðîãîíêè
|
||||||
|
double[] alpha = new double[n - 1];
|
||||||
|
double[] beta = new double[n - 1];
|
||||||
|
alpha[0] = beta[0] = 0.0;
|
||||||
|
for (int i = 1; i < n - 1; i++) {
|
||||||
|
double hi = input[i].getX() - input[i - 1].getX();
|
||||||
|
double hi1 = input[i + 1].getX() - input[i].getX();
|
||||||
|
double A = hi;
|
||||||
|
double C = 2.0 * (hi + hi1);
|
||||||
|
double B = hi1;
|
||||||
|
double F = 6.0 * ((input[i + 1].getY() - input[i].getY()) /
|
||||||
|
hi1 - (input[i].getY() - input[i - 1].getY()) / hi);
|
||||||
|
double z = (A * alpha[i - 1] + C);
|
||||||
|
alpha[i] = -B / z;
|
||||||
|
beta[i] = (F - A * beta[i - 1]) / z;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Íàõîæäåíèå ðåøåíèÿ - îáðàòíûé õîä ìåòîäà ïðîãîíêè
|
||||||
|
for (int i = n - 2; i > 0; i--) {
|
||||||
|
splines[i].c = alpha[i] * splines[i + 1].c + beta[i];
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ïî èçâåñòíûì êîýôôèöèåíòàì c[i] íàõîäèì çíà÷åíèÿ b[i] è d[i]
|
||||||
|
for (int i = n - 1; i > 0; i--) {
|
||||||
|
double hi = input[i].getX() - input[i - 1].getX();
|
||||||
|
splines[i].d = (splines[i].c - splines[i - 1].c) / hi;
|
||||||
|
splines[i].b = hi * (2.0 * splines[i].c + splines[i - 1].c) / 6.0 + (input[i].getY() - input[i - 1].getY()) / hi;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ñòðóêòóðà, îïèñûâàþùàÿ ñïëàéí íà êàæäîì ñåãìåíòå ñåòêè
|
||||||
|
private class SplineTuple {
|
||||||
|
double a, b, c, d, x;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
106
src/com/nummethods/lr3/GraphicPanel.java
Normal file
106
src/com/nummethods/lr3/GraphicPanel.java
Normal file
@ -0,0 +1,106 @@
|
|||||||
|
package com.nummethods.lr3;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class GraphicPanel extends javax.swing.JPanel {
|
||||||
|
|
||||||
|
private Point2D[] input;
|
||||||
|
private Lagrange lagrange;
|
||||||
|
private CubicSpline cubicSpline;
|
||||||
|
|
||||||
|
private Point clickedPoint;
|
||||||
|
|
||||||
|
public GraphicPanel(Point2D[] input) {
|
||||||
|
this.input = input;
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
addMouseListener(new java.awt.event.MouseAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mousePressed(java.awt.event.MouseEvent evt) {
|
||||||
|
formMousePressed(evt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(java.awt.event.MouseEvent evt) {
|
||||||
|
formMousePressed(evt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
clickedPoint = new Point(0, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setInterpolations(Lagrange lagrange, CubicSpline cubicSpline) {
|
||||||
|
this.lagrange = lagrange;
|
||||||
|
this.cubicSpline = cubicSpline;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
final int width = g.getClipBounds().width;
|
||||||
|
final int height = g.getClipBounds().height;
|
||||||
|
|
||||||
|
// Ðàñ÷åò êîýôèöèåíòîâ ðàñòÿãèâàíèÿ
|
||||||
|
double xMin = input[0].getX();
|
||||||
|
double xMax = input[0].getX();
|
||||||
|
double yMin = input[0].getY();
|
||||||
|
double yMax = input[0].getY();
|
||||||
|
for (int i = 1; i < input.length; i++) {
|
||||||
|
Point2D point = input[i];
|
||||||
|
if (xMin > point.getX()) xMin = point.getX();
|
||||||
|
else if (xMax < point.getX()) xMax = point.getX();
|
||||||
|
if (yMin > point.getY()) yMin = point.getY();
|
||||||
|
else if (yMax < point.getY()) yMax = point.getY();
|
||||||
|
}
|
||||||
|
double dx = width / (xMax - xMin);
|
||||||
|
double dy = height / (yMax - yMin);
|
||||||
|
|
||||||
|
// Èíòåðïîëèðîâàííûå ãðàôèêè
|
||||||
|
double xStep = (xMax - xMin) / width;
|
||||||
|
for (double x = xMin; x <= xMax; x += xStep) {
|
||||||
|
g.setColor(Color.GREEN);
|
||||||
|
plot(g, (x - xMin) * dx, height - (lagrange.interpolate(x) - yMin) * dy);
|
||||||
|
|
||||||
|
g.setColor(Color.BLUE);
|
||||||
|
plot(g, (x - xMin) * dx, height - (cubicSpline.interpolate(x) - yMin) * dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ðàññòàíîâêà íà÷àëüíûõ òî÷åê
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
for (int i = 0; i < input.length; i++) {
|
||||||
|
Point2D point = input[i];
|
||||||
|
int x = (int) ( (point.getX() - xMin) * dx);
|
||||||
|
int y = (int) (height - (point.getY() - yMin) * dy);
|
||||||
|
g.fillRect(x, y, 3, 3);
|
||||||
|
g.drawString(point.getX() + ", " + point.getY(), x + 2, y - 4);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Îòîáðàæåíèå ïîçèöèè âûáðàííîé òî÷êè
|
||||||
|
double x = clickedPoint.getX() / dx + xMin;
|
||||||
|
int graphX = (int) ((x - xMin) * dx);
|
||||||
|
g.drawLine(graphX, 0, graphX, height);
|
||||||
|
|
||||||
|
// Ïîäïèñè
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawString("x: " + x, 10, 20);
|
||||||
|
g.setColor(Color.GREEN);
|
||||||
|
g.drawString("Lagrange: " + lagrange.interpolate(x), 10, 40);
|
||||||
|
g.setColor(Color.BLUE);
|
||||||
|
g.drawString("Cubic Spline: " + cubicSpline.interpolate(x), 10, 60);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void formMousePressed(java.awt.event.MouseEvent evt) {
|
||||||
|
clickedPoint = evt.getPoint();
|
||||||
|
repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void plot(Graphics g, double x, double y) {
|
||||||
|
g.drawLine((int) x, (int) y, (int) x, (int) y);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
40
src/com/nummethods/lr3/LR_3.java
Normal file
40
src/com/nummethods/lr3/LR_3.java
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package com.nummethods.lr3;
|
||||||
|
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import static javax.swing.JFrame.EXIT_ON_CLOSE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class LR_3 extends JFrame {
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new LR_3().setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LR_3() {
|
||||||
|
super("LR_3");
|
||||||
|
setBounds(300, 120, 0, 0);
|
||||||
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
Point2D[] input = new Point2D[] {
|
||||||
|
new Point2D.Double(1, 2.0),
|
||||||
|
new Point2D.Double(2, 2.3),
|
||||||
|
new Point2D.Double(3, 2.9),
|
||||||
|
new Point2D.Double(4, 3.5),
|
||||||
|
new Point2D.Double(5, 4.2),
|
||||||
|
new Point2D.Double(6, 6.0),
|
||||||
|
new Point2D.Double(7, 8.1),
|
||||||
|
new Point2D.Double(8, 10.7)
|
||||||
|
};
|
||||||
|
GraphicPanel panel = new GraphicPanel(input);
|
||||||
|
panel.setPreferredSize(new Dimension(400, 300));
|
||||||
|
panel.setInterpolations(new Lagrange(input), new CubicSpline(input));
|
||||||
|
add(panel);
|
||||||
|
|
||||||
|
pack();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
39
src/com/nummethods/lr3/Lagrange.java
Normal file
39
src/com/nummethods/lr3/Lagrange.java
Normal file
@ -0,0 +1,39 @@
|
|||||||
|
package com.nummethods.lr3;
|
||||||
|
|
||||||
|
import java.awt.geom.Point2D;
|
||||||
|
|
||||||
|
public class Lagrange extends AbstractInterpolation {
|
||||||
|
|
||||||
|
public Lagrange(Point2D[] input) {
|
||||||
|
super(input);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double interpolate(double x) {
|
||||||
|
double y = 0;
|
||||||
|
for (int i = 0; i < input.length; i++) {
|
||||||
|
y += calculateMember(i, x);
|
||||||
|
}
|
||||||
|
return y;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double calculateMember(int idx, double x) {
|
||||||
|
// ×èñëèòåëü
|
||||||
|
double m1 = 1;
|
||||||
|
for (int i = 0; i < input.length; i++) {
|
||||||
|
if (i != idx) {
|
||||||
|
m1 *= (x - input[i].getX());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// Çíàìåíàòåëü
|
||||||
|
double m2 = 1;
|
||||||
|
for (int i = 0; i < input.length; i++) {
|
||||||
|
if (i != idx) {
|
||||||
|
m2 *= (input[idx].getX() - input[i].getX());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return (m1 / m2) * input[idx].getY();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
9
src/com/nummethods/lr4/Function.java
Normal file
9
src/com/nummethods/lr4/Function.java
Normal file
@ -0,0 +1,9 @@
|
|||||||
|
package com.nummethods.lr4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public interface Function {
|
||||||
|
|
||||||
|
double f(double x);
|
||||||
|
}
|
65
src/com/nummethods/lr4/GraphicPanel.java
Normal file
65
src/com/nummethods/lr4/GraphicPanel.java
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
package com.nummethods.lr4;
|
||||||
|
|
||||||
|
import java.awt.Color;
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import java.text.DecimalFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class GraphicPanel extends javax.swing.JPanel {
|
||||||
|
|
||||||
|
private SimpsonIntegrate simpson;
|
||||||
|
|
||||||
|
public GraphicPanel(SimpsonIntegrate simpson) {
|
||||||
|
this.simpson = simpson;
|
||||||
|
|
||||||
|
setBackground(Color.WHITE);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
super.paintComponent(g);
|
||||||
|
final int width = g.getClipBounds().width;
|
||||||
|
final int height = g.getClipBounds().height;
|
||||||
|
|
||||||
|
// Ðàñ÷åò êîýôèöèåíòîâ ðàñòÿãèâàíèÿ
|
||||||
|
Integral integral = simpson.getIntegral();
|
||||||
|
double xMin = Math.min(integral.getA(), integral.getB());
|
||||||
|
double xMax = Math.max(integral.getA(), integral.getB());
|
||||||
|
double yMin = Math.min(integral.f(xMin), integral.f(xMax)) - 1;
|
||||||
|
double yMax = Math.max(integral.f(xMin), integral.f(xMax)) + 1;
|
||||||
|
double dx = width / (xMax - xMin);
|
||||||
|
double dy = height / (yMax - yMin);
|
||||||
|
|
||||||
|
// Èíòåðïîëèðîâàííûå ãðàôèêè
|
||||||
|
double xStep = (xMax - xMin) / width;
|
||||||
|
for (double x = xMin; x <= xMax; x += xStep) {
|
||||||
|
g.setColor(Color.GREEN);
|
||||||
|
plot(g, (x - xMin) * dx, height - (integral.f(x) - yMin) * dy);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ðàçáèåíèå
|
||||||
|
g.setColor(Color.RED);
|
||||||
|
ArrayList<Double> parts = simpson.getParts();
|
||||||
|
for (int i = 0; i < parts.size(); i++) {
|
||||||
|
Double xx = parts.get(i);
|
||||||
|
double x = xx;// / dx + xMin;
|
||||||
|
int graphX = (int) ((x - xMin) * dx);
|
||||||
|
g.drawLine(graphX, 0, graphX, height);
|
||||||
|
String part = new DecimalFormat("#.##").format(xx);
|
||||||
|
g.drawString(part, graphX, (i % 2 == 0) ? 10 : height - 20);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Ïîäïèñè
|
||||||
|
g.setColor(Color.BLACK);
|
||||||
|
g.drawString("Integral: " + simpson.calculate(6), 10, 20);
|
||||||
|
g.drawString("Parts: " + parts.size(), 10, 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void plot(Graphics g, double x, double y) {
|
||||||
|
g.drawLine((int) x, (int) y, (int) x, (int) y);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
36
src/com/nummethods/lr4/Integral.java
Normal file
36
src/com/nummethods/lr4/Integral.java
Normal file
@ -0,0 +1,36 @@
|
|||||||
|
package com.nummethods.lr4;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class Integral implements Function {
|
||||||
|
|
||||||
|
private Function func;
|
||||||
|
private double a, b;
|
||||||
|
private double epsilon;
|
||||||
|
|
||||||
|
public Integral(Function func, double a, double b, double epsilon) {
|
||||||
|
this.func = func;
|
||||||
|
this.a = a;
|
||||||
|
this.b = b;
|
||||||
|
this.epsilon = epsilon;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double f(double x) {
|
||||||
|
return func.f(x);
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getA() {
|
||||||
|
return a;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getB() {
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
public double getEpsilon() {
|
||||||
|
return epsilon;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
59
src/com/nummethods/lr4/LR_4.java
Normal file
59
src/com/nummethods/lr4/LR_4.java
Normal file
@ -0,0 +1,59 @@
|
|||||||
|
package com.nummethods.lr4;
|
||||||
|
|
||||||
|
import java.awt.Dimension;
|
||||||
|
import javax.swing.JFrame;
|
||||||
|
import static javax.swing.JFrame.EXIT_ON_CLOSE;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* v10
|
||||||
|
* cos(x)*exp(sin(x))
|
||||||
|
* 0 PI
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class LR_4 extends JFrame {
|
||||||
|
|
||||||
|
private static final Integral var10 = new Integral(new Function() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double f(double x) {
|
||||||
|
return Math.cos(x) * Math.exp(Math.sin(x));
|
||||||
|
}
|
||||||
|
}, 0, Math.PI, 0.005); // 3.4365
|
||||||
|
|
||||||
|
private static final Integral var3 = new Integral(new Function() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double f(double x) {
|
||||||
|
return Math.log(x*x*x);
|
||||||
|
}
|
||||||
|
}, 2, 10, 0.0002); // 40.9187
|
||||||
|
|
||||||
|
private static final Integral test = new Integral(new Function() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public double f(double x) {
|
||||||
|
return (x * x) * Math.sin(x);
|
||||||
|
}
|
||||||
|
}, 0, 1, 10e-6);// 0.22324
|
||||||
|
|
||||||
|
|
||||||
|
public static void main(String[] args) {
|
||||||
|
new LR_4().setVisible(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
public LR_4() {
|
||||||
|
super("LR_4");
|
||||||
|
setBounds(300, 120, 0, 0);
|
||||||
|
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
SimpsonIntegrate simpson = new SimpsonIntegrate(var10);
|
||||||
|
System.out.println( simpson.calculate(6) );
|
||||||
|
|
||||||
|
GraphicPanel panel = new GraphicPanel(simpson);
|
||||||
|
panel.setPreferredSize(new Dimension(400, 300));
|
||||||
|
add(panel);
|
||||||
|
|
||||||
|
pack();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
75
src/com/nummethods/lr4/SimpsonIntegrate.java
Normal file
75
src/com/nummethods/lr4/SimpsonIntegrate.java
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
package com.nummethods.lr4;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
/**
|
||||||
|
*
|
||||||
|
* @author aNNiMON
|
||||||
|
*/
|
||||||
|
public class SimpsonIntegrate {
|
||||||
|
|
||||||
|
private Integral integral;
|
||||||
|
private ArrayList<Double> part;
|
||||||
|
|
||||||
|
public SimpsonIntegrate(Integral integral) {
|
||||||
|
this.integral = integral;
|
||||||
|
|
||||||
|
part = new ArrayList<Double>();
|
||||||
|
}
|
||||||
|
|
||||||
|
public double calculate(int step) {
|
||||||
|
part.clear();
|
||||||
|
part.add(integral.getA());
|
||||||
|
|
||||||
|
double result = 0;
|
||||||
|
double h = (integral.getB() - integral.getA()) / step;
|
||||||
|
for (int i = 0; i < step; i++) {
|
||||||
|
double from = integral.getA() + h * i;
|
||||||
|
double partWithBigH = part(from, from + h, h);
|
||||||
|
double hDecreased = h;
|
||||||
|
|
||||||
|
boolean needToDecreaseH;
|
||||||
|
double partWithSmallH;
|
||||||
|
do {
|
||||||
|
partWithSmallH = part(from, from + h, hDecreased / 2);
|
||||||
|
needToDecreaseH = !checkRunge(partWithBigH, partWithSmallH) && (hDecreased > 0.001);
|
||||||
|
if (needToDecreaseH) {
|
||||||
|
hDecreased /= 2;
|
||||||
|
}
|
||||||
|
} while (needToDecreaseH);
|
||||||
|
|
||||||
|
result += Math.abs(partWithSmallH);
|
||||||
|
for(double k = hDecreased; k <= h; k += hDecreased) {
|
||||||
|
part.add(from + k);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integral getIntegral() {
|
||||||
|
return integral;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ArrayList<Double> getParts() {
|
||||||
|
return part;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double part(double a, double b, double h) {
|
||||||
|
double result = 0;
|
||||||
|
for (double i = a; i < b; i += h) {
|
||||||
|
result += integral(i, i + h);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
private double integral(double x1, double x2) {
|
||||||
|
final double h = x2 - x1;
|
||||||
|
return h/6 * (integral.f(x1) + 4 * integral.f(x1 + h/2) + integral.f(x2));
|
||||||
|
}
|
||||||
|
|
||||||
|
private boolean checkRunge(double i1, double i2) {
|
||||||
|
return (Math.abs(i1 - i2)) < 15 * integral.getEpsilon();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user