Sie sind auf Seite 1von 4

Diagram klas dla problemu TRANSMITANCJA

Diagram sekwencji dla problemu TRANSMITANCJA

Kod klas dla problemu TRANSMITANCJA


package transferfunction01; public class TestTF { public Complex createComplex(double re, double im) { return Complex.valueOf(re, im); } public TransferFun createTF(double[] num, double[] den) { return new TransferFun(num, den); } public Complex evalTF(TransferFun tf, Complex x) { return tf.evalTF(x); } public static void main(String[] args) { double num[] = {1.0, 2.0, 3.0}; double den[] = {1.0, 2.0, 3.0, 4.0}; TestTF ttf = new TestTF(); Complex x = ttf.createComplex(2.0, 1.0); System.out.println("x = "+x.toString()); TransferFun tf = ttf.createTF(num, den); System.out.println("tf = "+tf.toString()); Complex val = ttf.evalTF(tf, x); System.out.println("tf(x) = "+val.toString()); } } ========================================================= package transferfunction01; public class TransferFun { private Poly num; private Poly den; public TransferFun(double[] num, double[] den) { this.num = new Poly(num); this.den = new Poly(den); } @Override public String toString() { return "("+num.toString()+"/"+den.toString()+")"; } public Complex evalTF(Complex x) { Complex valNum = num.evalPoly(x); Complex valDen = den.evalPoly(x); return valNum.divide(valDen); } } ========================================================== package transferfunction01; import java.text.DecimalFormat; public class Poly { private double a[] = new double[10]; private int n; public Poly(double[] coeff) { this.n = coeff.length; for (int i = 0; i < n; i++) { a[i] = coeff[i]; } }

public Complex evalPoly(Complex x) { Complex res = Complex.valueOf(a[0], 0.0); for (int i = 1; i < n; i++) { Complex ax = Complex.valueOf(a[i], 0.0); res = ax.add(x.multiply(res)); } return res; } @Override public String toString() { DecimalFormat df = new DecimalFormat("###.0000"); StringBuilder sb = new StringBuilder("("); for (int i = 0; i < n; i++) { if(i != n-1) { sb.append(df.format(a[i])+" "); } else { sb.append(df.format(a[i])); } } sb.append(")"); return sb.toString(); } } ========================================================== package transferfunction01; import java.text.DecimalFormat; public class Complex { private final double re; private final double im; private Complex(double re, double im) { this.re = re; this.im = im; } public static Complex valueOf(double re, double im) { return new Complex(re, im); } public static Complex valueOfPolar(double r , double theta) { return new Complex(r * Math.cos(theta), r*Math.sin(theta)); } public double realPart() { return re; } public double imagPart() { return im; } public Complex return new } public Complex return new } public Complex return new add(Complex c) { Complex(re + c.re, im + c.im); subtract(Complex c) { Complex(re - c.re, im - c.im); multiply(Complex c) { Complex(re * c.re - im * c.im, re * c.im + im * c.re);

} public Complex divide(Complex c) { double tmp = c.re *c.re + c.im * c.im; return new Complex((re * c.re + im * c.im)/tmp, (im * c.re - re * c.im)/tmp); } @Override public boolean equals(Object o) { if (o == this) return true; if (!(o instanceof Complex)) return false; Complex c = (Complex) o; return Double.compare(re, c.re) == 0 && Double.compare(im, c.im) == 0; }

@Override public int hashCode() { int result = 17 + hashDouble(re); result = 31 * result + hashDouble(im); return result; } private static int hashDouble(double val) { long longBits = Double.doubleToLongBits(val); return (int) (longBits ^ (longBits >>> 32)); } @Override public String toString() { DecimalFormat df = new DecimalFormat("##0.0000"); String str = ""; if (im >= 0.0) { str = "(" + df.format(re) + " + " + df.format(im) + "i)"; } else { str = "(" + df.format(re) + " - " + df.format(Math.abs(im)) + "i)"; } return str; } }

Das könnte Ihnen auch gefallen