数値解析B (2014 Fall)
参考資料
演習1
浮動小数点数をビットパターンの文字列に変換するプログラム (FloatRepresentation1.java
)
import java.io.*;
public class FloatRepresentation1 {
public static void main(String[] args) throws Exception{
System.out.print("浮動小数点数を入力してください > ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
float value = Float.parseFloat(br.readLine());
int bits = Float.floatToRawIntBits(value);
String result = Integer.toBinaryString(bits);
for(int i=0; i<32-result.length(); i++) System.out.print("0");
System.out.println(result);
}
}
ビットパターンの文字列を浮動小数点数に変換するプログラム (FloatRepresentation2.java
)
import java.io.*;
public class FloatRepresentation2 {
public static void main(String[] args) throws Exception{
System.out.print("ビットパターンを入力してください > ");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String value = br.readLine();
int longBits = Integer.parseInt(value, 2);
float myFloat = Float.intBitsToFloat(longBits);
System.out.println(myFloat);
}
}
演習2
2次方程式を解くプログラム (QuadraticEquation.java
)
import java.io.*;
import java.text.DecimalFormat;
public class QuadraticEquation {
public static void main(String[] args) throws IOException{
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String inputString;
DecimalFormat df = new DecimalFormat("0.000000000000E0");
System.out.print("a=");
inputString = br.readLine();
float a = Float.parseFloat(inputString);
System.out.print("b=");
inputString = br.readLine();
float b = Float.parseFloat(inputString);
System.out.print("c=");
inputString = br.readLine();
float c = Float.parseFloat(inputString);
System.out.println("result:");
float d = b*b-4*a*c;
System.out.println("d=" + df.format(d));
float dsqrt = (float)Math.sqrt(d);
System.out.println("dsqrt=" + df.format(dsqrt));
float s1pre = -b+dsqrt;
System.out.println("s1pre=" + df.format(s1pre));
float s1 = s1pre / (2*a);
System.out.println("s1=" + df.format(s1));
float s2 = (-b-dsqrt) / (2*a);
System.out.println("s2=" + df.format(s2));
float s1alt = c / a / s1;
System.out.println("s1alt=" + df.format(s1alt));
}
}
演習3
2分法のプログラム (Binary.java
)
public class Binary{
public static double f(double x){
double y = x * x - 2.0;
return y;
}
public static void main(String[] args) {
double a = 1.0;
double b = 2.0;
double c = 0.0;
double eps = 1.0e-10;
while((b-a)>eps){
c = (a+b)/2.0;
double fa = f(a);
double fb = f(b);
double fc = f(c);
if(fa*fc>0.0) a=c;
else b=c;
}
System.out.println("x=" + c);
}
}
ニュートン法のプログラム (Newton.java
)
public class Newton {
public static double f(double x){
double y = x * x - 2.0;
return y;
}
public static double df(double x){
double y = 2.0 * x;
return y;
}
public static void main(String[] args) {
double x = 2.0;
double xnext;
double eps = 1.0e-10;
while(true){
xnext = x - f(x) / df(x);
if(Math.abs(xnext-x) < eps) break;
x = xnext;
}
System.out.println("x=" + x);
}
}
演習4
ガウスの消去法のプログラム1 (LinearEquation1.java
)
public class LinearEquation1 {
public static void main(String[] args){
double[][] a = {{1, 1, 1}, {1, 2, 2}, {2, 2, 1}};
double[] b = {6.0, 10.0, 9.0};
// double eps = 1.0e-10;
// double r = 0.1;
// double[][] a = {{eps, 1}, {1, 1}};
// double[] b = {1-r, 1};
int n = a.length;
// 前進消去
for(int k=0; k<n-1; k++){
for(int i=k+1; i<n; i++){
double tmp = a[i][k] / a[k][k];
b[i] = b[i] - b[k] * tmp;
for(int j=k; j<n; j++){
a[i][j] = a[i][j] - a[k][j] * tmp;
}
}
}
// 後退代入
for(int i=n-1; i>=0; i--){
for(int j=i+1; j<n; j++){
b[i] = b[i] - b[j] * a[i][j];
}
b[i] = b[i] / a[i][i];
}
// 結果の出力
for(int i=0; i<n; i++){
System.out.println("x[" + i + "]=" + b[i]);
}
}
}
ガウスの消去法のプログラム2 (LinearEquation2.java
)
public class LinearEquation2 {
public static void main(String[] args){
double[][] a = {{1, 1, 1}, {1, 2, 2}, {2, 2, 1}};
double[] b = {6.0, 10.0, 9.0};
// double eps = 1.0e-10;
// double r = 0.1;
// double[][] a = {{eps, 1}, {1, 1}};
// double[] b = {1-r, 1};
int n = a.length;
// 前進消去
for(int k=0; k<n-1; k++){
// ピボットを探す
double max = Math.abs(a[k][k]);
int l = k;
for(int j=k+1; j<n; j++){
double f = Math.abs(a[j][k]);
if(f > max){
max = f;
l = j;
}
}
// k行とl行の入れ替え
for(int j=0; j<n; j++){
double tmp = a[k][j];
a[k][j] = a[l][j];
a[l][j] = tmp;
}
double tmp = b[k];
b[k] = b[l];
b[l] = tmp;
for(int i=k+1; i<n; i++){
tmp = a[i][k] / a[k][k];
b[i] = b[i] - b[k] * tmp;
for(int j=k; j<n; j++){
a[i][j] = a[i][j] - a[k][j] * tmp;
}
}
}
// 後退代入
for(int i=n-1; i>=0; i--){
for(int j=i+1; j<n; j++){
b[i] = b[i] - b[j] * a[i][j];
}
b[i] = b[i] / a[i][i];
}
// 結果の出力
for(int i=0; i<n; i++){
System.out.println("x[" + i + "]=" + b[i]);
}
}
}
演習5
矩形則のプログラム ( RectangularRule.java
)
public class RectangularRule {
static double f(double x) {
return x * x;
}
public static void main(String[] args) {
double a = 0.0;
double b = 1.0;
int N = 10;
double h = (b - a) / N;
double sum = 0.0;
for (int i = 0; i < N; i++) {
double x = a + i * h;
sum = sum + f(x) * h;
}
System.out.println("sum=" + sum);
}
}
台形則のプログラム ( TrapezoidalRule.java
)
public class TrapezoidalRule {
static double f(double x) {
return x * x;
}
public static void main(String[] args) {
double a = 0.0;
double b = 1.0;
int N = 10;
double h = (b - a) / N;
double sum = 0.0;
for (int i = 0; i < N; i++) {
double x = a + i * h;
double y = a + (i+1) * h;
sum = sum + (f(x) + f(y)) * h / 2.0;
}
System.out.println("sum=" + sum);
}
}
シンプソン則のプログラム ( SimpsonsRule.java
)
public class SimpsonsRule {
static double f(double x) {
return x * x;
}
public static void main(String[] args) {
double a = 0.0;
double b = 1.0;
int N = 10;
double h = (b - a) / N / 2.0;
double sum = 0.0;
for (int i = 0; i < N; i++) {
double x = a + 2 * i * h;
double y = a + (2 * i + 1.0) * h;
double z = a + (2 * (i+1)) * h;
sum = sum + (f(x) + 4.0 * f(y) + f(z)) * h / 3.0;
}
System.out.println("sum=" + sum);
}
}