Skip to content

Instantly share code, notes, and snippets.

@pif
Created December 8, 2010 15:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pif/733464 to your computer and use it in GitHub Desktop.
Save pif/733464 to your computer and use it in GitHub Desktop.
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Collections;
import processing.core.PApplet;
import processing.core.PVector;
/**
*
* @author Ostap
*/
public class Chys4 extends PApplet {
public double[] y = new double[]{1.3421, 2.2107, 2.7583 ,5.5803 ,6.8789};
public double[] x = new double[]{1.1, 1.3, 1.4, 1.8, 1.9};
public double[] p = new double[]{1,1,1,1,1};//0.3, 0.3, 0.3, 0.05, 0.05};
/*public double[] y = new double[]{0.5891, 0.5972, 0.6052, 0.6131, 0.6210, 0.6288};
public double[] x = new double[]{0.63, 0.64, 0.65, 0.66, 0.67, 0.68};
public double[] p = new double[]{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.4};
*/
public int m = 4;
public int n = x.length;
private double phi(int idx, int xidx) {
return Math.pow(x[xidx], idx);
}
private double phiKphiJ(int k, int j) {
double sum = 0;
for (int i= 0; i < n; i++) {
sum+=p[i]*phi(k,i)*phi(j,i);
}
return sum;
}
private double fphiJ(int j) {
double sum = 0;
for (int i= 0; i < n; i++) {
sum+=p[i]*y[i]*phi(j,i);
}
return sum;
}
private double[][] a = new double[m][m+1];
private double[] b = new double[m];
private void setCoefficients() {
for (int j = 0; j < m; j ++) {
for (int k = 0; k < m; k ++) {
a[k][j] = phiKphiJ(k, j);
}
b[j] = fphiJ(j);
a[j][m] = b[j];
}
}
private double res[];
public double[] getCoefficients() {
if(res == null) {
EquationsSet es = new EquationsSet(a);
res = es.Solve();
}
return res;
}
/**
* @param args the command line arguments
*/
private PVector getScreenPoint(float x, float y) {
return new PVector(map(x,1,2,0,width), height-map(y,0,8,0,height));
}
private void putPoint(float x, float y) {
PVector point = getScreenPoint(x, y);
point(point.x,point.y);
}
public void setup() {
size(400,400);
background(255);
// chyselni logic
Chys4 c4 = new Chys4();
c4.setCoefficients();
double[] res = c4.getCoefficients();
System.out.println("Found coefficients:");
for (int i= 0; i < res.length; i++) {
System.out.println("a["+i+"]="+res[i]);
}
System.out.println("\nPolinom values:");
double sq=0;
double prev = 0;
for (int i=0;i<c4.n;i++) {
double sum=0;
for (int j=0;j<c4.m;j++) {
sum+=c4.phi(j,i)*res[j];
}
//draw lines
if(i>0) {
strokeWeight(2);
//given line
stroke(0,255,0);
PVector s = getScreenPoint((float)c4.x[i-1], (float)c4.y[i-1]);
PVector f = getScreenPoint((float)c4.x[i], (float)c4.y[i]);
line(s.x, s.y, f.x, f.y);
//calculated line
stroke(0,0,255);
PVector s1 = getScreenPoint((float)c4.x[i-1], (float)prev);
PVector f1 = getScreenPoint((float)c4.x[i], (float)sum);
line(s1.x, s1.y, f1.x, f1.y);
}
strokeWeight(4);
stroke(0);
putPoint((float)c4.x[i], 0f);//ordinate
stroke(0);
putPoint((float)c4.x[i], (float)c4.y[i]);//point from data
stroke(255,0,0);
putPoint((float)c4.x[i], (float)sum);//point from data
System.out.println("x["+i+"]="+c4.x[i]+"\ty["+i+"]="+c4.y[i]+"\tF(x["+i+"])="+sum);
prev = sum;
sq+=c4.p[i]*Math.pow(c4.y[i]-sum,2);
}
System.out.println("\nLeast square difference:");
System.out.println((new BigDecimal(Double.toString(sq))).toPlainString());
}
@Override
public void draw() {
}
public static void main(String[] args) {
PApplet.main(new String[] { "--present", "--bgcolor=#000000",
"--present-stop-color=#000000", "Chys4" });
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment