I have a question regarding java's initializing, and I do not know why java thinks that the double in my program is not initialized, even though I have initialized it. Can someone please explain to me how to resolve this problem? In the code below, java says that double "inC" is not initialized.
package tempconverter;
import java.awt.Color;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.BorderLayout;
public class TempConverter extends JFrame implements ActionListener, ItemListener
{
static String[] choices = { "From Celsius", "From Fahrenheit", "From Kelvin", "From Rankine" };
static JFrame f = new JFrame("Temperature Converter");
static JTextField enter = new JTextField(5);
static JButton confirm = new JButton("Convert");
static JComboBox choose = new JComboBox(choices);
static JFrame tell = new JFrame();
static JLabel one = new JLabel();
static JLabel two = new JLabel();
static JLabel three = new JLabel();
public static void main(String[] args)
{
Font main = new Font("Comic Sans", Font.PLAIN, 12);
confirm.addActionListener(new TempConverter());
confirm.setFont(main);
choose.setFont(main);
enter.setFont(main);
choose.addItemListener(new TempConverter());
f.setBackground(Color.WHITE);
f.setLayout(new FlowLayout());
f.setSize(340, 60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(enter);
f.add(confirm);
f.add(choose);
tell.setLayout(new BorderLayout());
tell.add(one, BorderLayout.NORTH);
tell.add(two, BorderLayout.WEST);
tell.add(three, BorderLayout.SOUTH);
tell.setSize(160, 65);
tell.setLocationRelativeTo(null);
f.setVisible(true);
}
#Override
public void actionPerformed(ActionEvent e)
{
double toConvert = Double.parseDouble(enter.getText());
double inF, inK, inC, tempF = 0, tempK, inR, tempR;
double tempC;
if (choose.getSelectedItem().equals("From Celsius"))
{
tempF = toConvert * 1.8 + 32;
tempK = toConvert + 273.15;
tempR = (toConvert + 273.15) * 1.8;
inF = Math.round(tempF * 100.00) / 100.00;
inK = Math.round(tempK * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°C = " + inF + "°F");
two.setText(toConvert + "°C = " + inK + "°K");
three.setText(toConvert + "°C = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Fahrenheit"))
{
if (toConvert == 32)
{
inC = 0;
inK = 273.15;
inR = toConvert + 459.67;
one.setText(toConvert + "°F = " + inC + "°C");
two.setText(toConvert + "°F = " + inK + "°K");
three.setText(toConvert + "°F = " + inR + "°R");
}
else
tempC = (toConvert - 32) / 1.8;
tempK = (toConvert - 32) / 1.8 + 273.15;
tempR = toConvert + 459.67;
inC = Math.round(tempC * 100.00) / 100.00;
inK = Math.round(tempK * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°F = " + inC + "°C");
two.setText(toConvert + "°F = " + inK + "°K");
three.setText(toConvert + "°F = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Kelvin"))
{
tempC = toConvert - 273.15;
tempF = tempC * 1.8 + 32;
tempR = toConvert * 1.8;
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempR * 100.00) / 100.00;
one.setText(toConvert + "°K = " + inC + "°C");
two.setText(toConvert + "°K = " + inF + "°F");
three.setText(toConvert + "°K = " + inR + "°R");
tell.setVisible(true);
}
if (choose.getSelectedItem().equals("From Rankine"))
{
if (toConvert == 0)
{
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText("0°R = -273.15°C");
two.setText("0°R = " + inF + "°F");
three.setText("0°R = " + inR + "°K");
tell.setVisible(true);
}
else
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
}
}
#Override
public void itemStateChanged(ItemEvent e)
{
repaint();
}
}
Java points out that I have not initialized "inC" in the else statement of the "From Fahrenheit" and the "From Rankine" method. However, when I initialize it in the variable declaration, it doesn't change when I try to change it in the if-else statement and the methods. How do I fix this?
I have copied yout example and i get the message at tempC. The reason is a missing } at the else path.
Change :
else
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
TO:
else {
tempC = (toConvert - 491.67) * (5 / 9);
tempF = toConvert - 459.67;
tempK = toConvert * (5 / 9);
inC = Math.round(tempC * 100.00) / 100.00;
inF = Math.round(tempF * 100.00) / 100.00;
inR = Math.round(tempK * 100.00) / 100.00;
one.setText(toConvert + "°R = " + inC + "°C");
two.setText(toConvert + "°R = " + inF + "°F");
three.setText(toConvert + "°R = " + inR + "°K");
tell.setVisible(true);
}
As pointed out above the easiest thing would be to simply assign a value to it when you declare it.
The reason that Java thinks that it has not been initialized is that it might actually not have. You only ever assign it a value in one of the 4 outer most if-statements. If none of these four conditions are true, the variable will never be initialized.
And as Jens has also pointed out, you are missing a { after the "else".
The actual cause of the error is dull: you're missing a brace.
But, let's look at tempK = toConvert * (5 / 9);. This will not do what you want since (5 / 9) will evaluate to 0 as the parentheses mean this is evaluated on its own and in integer arithmetic. To solve this, the easiest thing to do is remove the parentheses:
tempK = toConvert * 5 / 9;
so everything is computed in floating point. (Note that * and / have identical precedence).
You make this error a couple of times in your program.
Related
I made a java implementation of the new perceptualy uniform color space JzAzBz. OSA publication is : https://www.osapublishing.org/oe/fulltext.cfm?uri=oe-25-13-15131&id=368272.
My java code is :
private double b = 1.15;
private double g = 0.66;
private double c1 = 3424 / Math.pow(2, 12);
private double c2 = 2413 / Math.pow(2, 7);
private double c3 = 2392 / Math.pow(2, 7);
private double n = 2610 / Math.pow(2, 14);
private double p = 1.7 * 2523 / Math.pow(2, 5);
private double d = -0.56;
private double d0 = 1.6295499532821566 * Math.pow(10, -11);
public void XYZToJab(double[] xyz, double[] jab) {
double[] XYZp = new double[3];
XYZp[0] = b * xyz[0] - ((b - 1) * xyz[2]);
XYZp[1] = g * xyz[1] - ((g - 1) * xyz[0]);
XYZp[2] = xyz[2];
double[] LMS = new double[3];
LMS[0] = 0.41478972 * XYZp[0] + 0.579999 * XYZp[1] + 0.0146480 * XYZp[2];
LMS[1] = -0.2015100 * XYZp[0] + 1.120649 * XYZp[1] + 0.0531008 * XYZp[2];
LMS[2] = -0.0166008 * XYZp[0] + 0.264800 * XYZp[1] + 0.6684799 * XYZp[2];
double[] LMSp = new double[3];
for (int i = 0; i < 3; i++) {
LMSp[i] = Math.pow((c1 + c2 * Math.pow((LMS[i] / 10000.0), n)) / (1 + c3 * Math.pow((LMS[i] / 10000.0), n)), p);
}
double[] Iab = new double[3];
Iab[0] = 0.5 * LMSp[0] + 0.5 * LMSp[1];
Iab[1] = 3.524000 * LMSp[0] - 4.066708 * LMSp[1] + 0.542708 * LMSp[2];
Iab[2] = 0.199076 * LMSp[0] + 1.096799 * LMSp[1] - 1.295875 * LMSp[2];
jab[0] = (((1 + d) * Iab[0]) / (1 + d * Iab[0])) - d0;
jab[1] = Iab[1];
jab[2] = Iab[2];
}
public void JabToXYZ(double[] jab, double[] xyz) {
double[] Iab = new double[3];
Iab[0] = (jab[0] + d0) / (1 + d - d * (jab[0] + d0));
Iab[1] = jab[1];
Iab[2] = jab[2];
double[] LMSp = new double[3];
LMSp[0] = 1.0 * Iab[0] + 0.13860504 * Iab[1] + 0.05804732 * Iab[2];
LMSp[1] = 1.0 * Iab[0] - 0.13860504 * Iab[1] - 0.05804732 * Iab[2];
LMSp[2] = 1.0 * Iab[0] - 0.09601924 * Iab[1] - 0.81189190 * Iab[2];
double[] LMS = new double[3];
for (int i = 0; i < 3; i++) {
LMS[i] = 10000 * Math.pow((c1 - Math.pow(LMSp[i], 1 / p)) / ((c3 * Math.pow(LMSp[i], 1 / p)) - c2), 1 / n);
}
double[] XYZp = new double[3];
XYZp[0] = 1.92422644 * LMS[0] - 1.00479231 * LMS[1] + 0.03765140 * LMS[2];
XYZp[1] = 0.35031676 * LMS[0] + 0.72648119 * LMS[1] - 0.06538442 * LMS[2];
XYZp[2] = -0.09098281 * LMS[0] - 0.31272829 * LMS[1] + 1.52276656 * LMS[2];
xyz[0] = (XYZp[0] + (b - 1) * XYZp[2]) / b;
xyz[1] = (XYZp[1] + (g - 1) * XYZp[0]) / g;
xyz[2] = XYZp[2];
}
When I test it running XYZToJab and then JabToXYZ I get a good precision for X and Z (delta order is E-9) but for Y I get a bad precision (delta order is 1-5%).
Is there anyone who can help me ?
The implementation is almost correct: The error lies in JabToXYZ where the prior to last line should be changed from
(XYZp[1] + (g - 1) * XYZp[0]) / g;
to
(XYZp[1] + (g - 1) * xyz[0]) / g;
However the fact that you are using rounded invert matrices to 6 decimal places in the JabToXYZ function will prevent you to get a clean inversion. You should try to compute the inverse at full double precision:
>>> import numpy as np
>>> np.set_printoptions(formatter={'float': '{:0.15f}'.format})
>>> import colour.models.jzazbz
>>> colour.models.jzazbz.JZAZBZ_IZAZBZ_TO_LMS_P_MATRIX
array([[1.000000000000000, 0.138605043271539, 0.058047316156119],
[1.000000000000000, -0.138605043271539, -0.058047316156119],
[1.000000000000000, -0.096019242026319, -0.811891896056039]])
>>> colour.models.jzazbz.JZAZBZ_LMS_TO_XYZ_MATRIX
array([[1.924226435787607, -1.004792312595365, 0.037651404030618],
[0.350316762094999, 0.726481193931655, -0.065384422948085],
[-0.090982810982848, -0.312728290523074, 1.522766561305260]])
I am writing a program which solves either quadratic or cubic equations. The thing is that I don't know if I am placing the Math.toRadians correctly.
The code is the following:
public double[] getRaices(double a,double b, double c, double d) throws ComplexException {
if (a==0){
double discriminante=Math.pow(c,2)+((-4)*b*d);
if(discriminante>=0){
this.Raices[0]=(c*(-1)+Math.sqrt(discriminante))/(2*b);
this.Raices[1]=(c*(-1)-Math.sqrt(discriminante))/(2*b);
}else{
throw new ComplexException("No hay solucion Real");
}
} else{
double f=((3*c/a)-(Math.pow(b,2)/Math.pow(a,2)))/3;
double g=((2*Math.pow(b,3)/Math.pow(a,3))-(9*b*c/Math.pow(a,2))+(27*d/a))/27;
double h=(Math.pow(g,2)/4)+(Math.pow(f,3)/27);
if(f+g+h==0){
Raices [0]=Math.cbrt(d/a)*(-1);
Raices [1]=Math.cbrt(d/a)*(-1);
Raices [2]=Math.cbrt(d/a)*(-1);
}else{
if(h<=0){
double i=Math.sqrt((Math.pow(g,2)/4)-h);
double j=Math.cbrt(i);
double k=Math.acos(Math.toRadians(-1*(g/2*i)));
System.out.println(" "+k+" ");
double l=j*(0-1);
double m=Math.toRadians(Math.cos(Math.toRadians(k/3)));
System.out.println(" "+m+" ");
double n=Math.sqrt(3)*Math.sin(Math.toRadians(k/3));
System.out.println(" "+n+" ");
double p=(b/(3*a)*(0-1));
Raices [0]=2*j*Math.cos(Math.toRadians(k/3))-(b/(3*a));
Raices [1]=(l*(m+n))+p;
Raices [2]=(l*(m-n))+p;
}else{
double r=((0-1)*(g/2))+Math.sqrt(h);
double s=Math.cbrt(r);
double t=((0-1)*(g/2))-Math.sqrt(h);
double u=Math.cbrt(t);
throw new ComplexException("2 de las raices son imaginarias pero una raiz es real: "+Math.floor(Raices [0]=(s+u)-(b/(3*a))));
}
}
}
return Raices;
}
But the problem is in the if (h<=0).
I tested your code against the web page and found several errors.
First is g /2i, you wrote g/2*i instead of g/2/i or (g/(2*i). And several Math.toRadians not necessary (webpage said calculations is in radians, so no need to convert).
I added println to help following the formula :
package test;
public class Cubic {
private double[] Raices = new double[3];
public static void main(String[] args) throws ComplexException {
double[] raices = new Cubic().getRaices(2, -4, -22, 24);
System.out.println(raices[0] + "," + raices[1] + "," + raices[2]);
}
public double[] getRaices(double a, double b, double c, double d) throws ComplexException {
if (a == 0) {
double discriminante = Math.pow(c, 2) + ((-4) * b * d);
if (discriminante >= 0) {
this.Raices[0] = (c * (-1) + Math.sqrt(discriminante)) / (2 * b);
this.Raices[1] = (c * (-1) - Math.sqrt(discriminante)) / (2 * b);
} else {
throw new ComplexException("No hay solucion Real");
}
} else {
double f = ((3 * c / a) - (Math.pow(b, 2) / Math.pow(a, 2))) / 3;
System.out.println("f=" + f);
double g = ((2 * Math.pow(b, 3) / Math.pow(a, 3)) - (9 * b * c / Math.pow(a, 2)) + (27 * d / a)) / 27;
System.out.println("g=" + g);
double h = (Math.pow(g, 2) / 4) + (Math.pow(f, 3) / 27);
System.out.println("h=" + h);
if (f + g + h == 0) {
Raices[0] = Math.cbrt(d / a) * (-1);
Raices[1] = Math.cbrt(d / a) * (-1);
Raices[2] = Math.cbrt(d / a) * (-1);
} else {
if (h <= 0) {
double i = Math.sqrt((Math.pow(g, 2) / 4) - h);
double j = Math.cbrt(i);
double k = Math.acos(-1 * (g / 2 / i));
System.out.println("k=" + k + " ");
double l = j * (0 - 1);
System.out.println("l=" + l + " ");
double m = Math.cos(k / 3);
System.out.println("m= " + m + " ");
double n = Math.sqrt(3) * Math.sin(k / 3);
System.out.println("n= " + n + " ");
double p = (b / (3 * a) * (0 - 1));
System.out.println("p= " + p + " ");
Raices[0] = 2 * j * Math.cos(k / 3) - (b / (3 * a));
Raices[1] = (l * (m + n)) + p;
Raices[2] = (l * (m - n)) + p;
} else {
double r = ((0 - 1) * (g / 2)) + Math.sqrt(h);
double s = Math.cbrt(r);
double t = ((0 - 1) * (g / 2)) - Math.sqrt(h);
double u = Math.cbrt(t);
throw new ComplexException(
"2 de las raices son imaginarias pero una raiz es real: " + Math.floor(Raices[0] = (s + u) - (b / (3 * a))));
}
}
}
return Raices;
}
}
I've tried to port these 2 functions from Lua to Java (bspline2D and bspline3D), but they doen't work. The output is completely different, and I can't figure out why.
I've already tried messing with the array numbers, the subdivision value, the for loops, and everything that came up my head.
The test provided should give numbers that are totally different one another, but they're not.
These are the Java results:
bspline2D
0.0, 0.0, 0.0
-1.0, 0.0, 0.0
-1.0, 0.0, 0.0
-1.0, 0.0, 0.0
-1.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 0.0, 0.0
bspline3D
-2.0, 0.0, 0.0
-1.0, 0.0, 0.0
-1.0, 0.0, 0.0
0.0, 0.0, 0.0
0.0, 0.0, 0.0
...which are clearly wrong.
These are the outputs from the Lua version, the correct ones.
bspline2D
0, 0
-0.75, 0
-0.5, 0
-0.25, 0
0, 0
0.24739583, 0.0026041667
0.47916666, 0.020833334
0.6796875, 0.0703125
0.8333333, 0.16666667
0.9270833, 0.31770834
0.9583333, 0.5
0.9270833, 0.6822917
0.8333333, 0.8333333
0.6796875, 0.9296875
0.47916666, 0.9791667
0.24739583, 0.9973958
0, 1
bspline3D
0, 0, 0
0.24739583, 0.0026041667, 0
0.47916666, 0.020833334, 0
0.6796875, 0.0703125, 0
0.8333333, 0.16666667, 0
0.9270833, 0.31770834, 0
0.9583333, 0.5, 0
0.9270833, 0.6822917, 0
0.8333333, 0.8333333, 0
0.6796875, 0.9296875, 0
0.47916666, 0.9791667, 0
0.24739583, 0.9973958, 0
0, 1, 0
-0.25, 1, 0
-0.5, 1, 0
-0.75, 1, 0
-1, 1, 0
-1.25, 1, 0
-1.5, 1, 0
-1.75, 1, 0
-2, 1, 0
I've been scratching my head for days on this, and I have just no ideas, so I thought maybe someone here at StackOverflow would.
Here's the Java version that needs to be fixed:
public static List<Vector3> bspline2D(Vector3[] pointz, int subdivision) {
ArrayList spline = new ArrayList();
int pointsNumber = pointz.length;
Vector3[] points = new Vector3[pointsNumber + 4];
System.arraycopy(pointz, 0, points, 2, pointsNumber);
points[1] = points[2].add(points[2].minus(points[3]));
points[0] = points[1].add(points[1].minus(points[2]));
points[pointsNumber + 2] = points[pointsNumber - 3].add(points[pointsNumber - 3].minus(points[pointsNumber - 4]));
points[pointsNumber + 3] = points[pointsNumber].add(points[pointsNumber].minus(points[pointsNumber - 3]));
spline.add(pointz[0]);
for(int i = 1; i < pointsNumber - 1; ++i) {
double[] a = new double[4];
double[] b = new double[4];
a[0] = (-points[i - 1].x + 3.0D * points[i].x - 3.0D * points[i + 1].x + points[i + 2].x) / 6.0D;
a[1] = (3.0D * points[i - 1].x - 6.0D * points[i].x + 3.0D * points[i + 1].x) / 6.0D;
a[2] = (-3.0D * points[i - 1].x + 3.0D * points[i + 1].x) / 6.0D;
a[3] = (points[i - 1].x + 4.0D * points[i].x + points[i + 1].x) / 6.0D;
b[0] = (-points[i - 1].y + 3.0D * points[i].y - 3.0D * points[i + 1].y + points[i + 2].y) / 6.0D;
b[1] = (3.0D * points[i - 1].y - 6.0D * points[i].y + 3.0D * points[i + 1].y) / 6.0D;
b[2] = (-3.0D * points[i - 1].y + 3.0D * points[i + 1].y) / 6.0D;
b[3] = (points[i - 1].y + 4.0D * points[i].y + points[i + 1].y) / 6.0D;
for(int j = 0; j < subdivision; ++j) {
double t = j / subdivision;
spline.add(new Vector3((a[2] + t * (a[1] + t * a[0])) * t + a[3], (b[2] + t * (b[1] + t * b[0])) * t + b[3]));
}
}
return spline;
}
public static List<Vector3> bspline3D(Vector3[] pointz, int subdivision) {
ArrayList spline = new ArrayList();
int pointsNumber = pointz.length;
Vector3[] points = new Vector3[pointsNumber + 4];
System.arraycopy(pointz, 0, points, 2, pointsNumber);
points[1] = points[2].add(points[2].minus(points[3]));
points[0] = points[1].add(points[1].minus(points[2]));
points[pointsNumber + 2] = pointz[pointsNumber - 1].add(pointz[pointsNumber - 1].minus(pointz[pointsNumber - 2]));
points[pointsNumber + 3] = points[pointsNumber].add(points[pointsNumber].minus(pointz[pointsNumber - 1]));
spline.add(points[0]);
for(int i = 1; i < pointsNumber - 1; ++i) {
double[] a = new double[4];
double[] b = new double[4];
double[] c = new double[4];
a[0] = (-points[i - 1].x + 3.0D * points[i].x - 3.0D * points[i + 1].x + points[i + 2].x) / 6.0D;
a[1] = (3.0D * points[i - 1].x - 6.0D * points[i].x + 3.0D * points[i + 1].x) / 6.0D;
a[2] = (-3.0D * points[i - 1].x + 3.0D * points[i + 1].x) / 6.0D;
a[3] = (points[i - 1].x + 4.0D * points[i].x + points[i + 1].x) / 6.0D;
b[0] = (-points[i - 1].y + 3.0D * points[i].y - 3.0D * points[i + 1].y + points[i + 2].y) / 6.0D;
b[1] = (3.0D * points[i - 1].y - 6.0D * points[i].y + 3.0D * points[i + 1].y) / 6.0D;
b[2] = (-3.0D * points[i - 1].y + 3.0D * points[i + 1].y) / 6.0D;
b[3] = (points[i - 1].y + 4.0D * points[i].y + points[i + 1].y) / 6.0D;
c[0] = (-points[i - 1].z + 3.0D * points[i].z - 3.0D * points[i + 1].z + points[i + 2].z) / 6.0D;
c[1] = (3.0D * points[i - 1].z - 6.0D * points[i].z + 3.0D * points[i + 1].z) / 6.0D;
c[2] = (-3.0D * points[i - 1].z + 3.0D * points[i + 1].z) / 6.0D;
c[3] = (points[i - 1].z + 4.0D * points[i].z + points[i + 1].z) / 6.0D;
for(int j = 1; j < subdivision; ++j) {
double t = j / subdivision;
Vector3 sp = new Vector3();
sp.x = (a[2] + t * (a[1] + t * a[0])) * t + a[3];
sp.y = (b[2] + t * (b[1] + t * b[0])) * t + b[3];
sp.z = (c[2] + t * (c[1] + t * c[0])) * t + c[3];
spline.add(sp);
}
}
return spline;
}
public static void main(String[] args) {
Vector3[] v = new Vector3[] {new Vector3(0, 0, 0), new Vector3(1, 0, 0), new Vector3(1, 1, 0), new Vector3(0, 1, 0)};
System.out.println("\nbspline2D");
for(Vector3 v3 : bspline2D(v, 4)) {
System.out.println(v3.x + ", " + v3.y + ", " + v3.z);
}
System.out.println("\nbspline3D");
for(Vector3 v3 : bspline3D(v, 4)) {
System.out.println(v3.x + ", " + v3.y + ", " + v3.z);
}
}
And here's the Lua version:
function M.bspline2D(points, subdivision)
local spline = {}
local wbvector2 = dofile('wbvector3.lua')
local pointsNumber = #points
-- extend the points array
points[0] = wbvector2.add(points[1], wbvector2.minus(points[1], points[2]))
points[-1] = wbvector2.add(points[0], wbvector2.minus(points[0], points[1]))
points[pointsNumber + 1] = wbvector2.add(points[pointsNumber], wbvector2.minus(points[pointsNumber], points[pointsNumber - 1]))
points[pointsNumber + 2] = wbvector2.add(points[pointsNumber + 1], wbvector2.minus(points[pointsNumber + 1], points[pointsNumber]))
-- Interpolation
local index = 1
spline[index] = points[1] -- first point
for i = 0, pointsNumber - 1 do
local a = {} -- compute the third order coefficients for x
local b = {} -- compute the third order coefficients for y
a[1] = (-points[i-1].x + 3 * points[i].x - 3 * points[i+1].x + points[i+2].x) / 6.0;
a[2] = (3 * points[i-1].x - 6 * points[i].x + 3 * points[i+1].x) / 6.0;
a[3] = (-3 * points[i-1].x + 3 * points[i+1].x) / 6.0;
a[4] = (points[i-1].x + 4 * points[i].x + points[i+1].x) / 6.0;
b[1] = (-points[i-1].y + 3 * points[i].y - 3 * points[i+1].y + points[i+2].y) / 6.0;
b[2] = (3 * points[i-1].y - 6 * points[i].y + 3 * points[i+1].y) / 6.0;
b[3] = (-3 * points[i-1].y + 3 * points[i+1].y) / 6.0;
b[4] = (points[i-1].y + 4 * points[i].y + points[i+1].y) / 6.0;
for j = 1, subdivision do
index = index + 1
spline[index] = {}
local t = j / subdivision
spline[index].x = (a[3] + t * (a[2] + t * a[1])) * t + a[4]
spline[index].y = (b[3] + t * (b[2] + t * b[1])) * t + b[4]
end
end
return spline
end
function M.bspline3D(points, subdivision)
local spline = {}
local wbcore = require('wbcore')
local wbvector3 = require('wbvector3')
local pointsNumber = wbcore.tablelength(points)
-- extend the points array
points[0] = wbvector3.add(points[1], wbvector3.minus(points[1], points[2]))
points[-1] = wbvector3.add(points[0], wbvector3.minus(points[0], points[1]))
points[pointsNumber + 1] = wbvector3.add(points[pointsNumber], wbvector3.minus(points[pointsNumber], points[pointsNumber - 1]))
points[pointsNumber + 2] = wbvector3.add(points[pointsNumber + 1], wbvector3.minus(points[pointsNumber + 1], points[pointsNumber]))
-- Interpolation
local index = 1
spline[index] = points[1] -- first point
for i = 1, pointsNumber - 1 do
local a = {} -- compute the third order coefficients for x
local b = {} -- compute the third order coefficients for y
local c = {} -- compute the third order coefficients for z
a[1] = (-points[i-1].x + 3 * points[i].x - 3 * points[i+1].x + points[i+2].x) / 6.0;
a[2] = (3 * points[i-1].x - 6 * points[i].x + 3 * points[i+1].x) / 6.0;
a[3] = (-3 * points[i-1].x + 3 * points[i+1].x) / 6.0;
a[4] = (points[i-1].x + 4 * points[i].x + points[i+1].x) / 6.0;
b[1] = (-points[i-1].y + 3 * points[i].y - 3 * points[i+1].y + points[i+2].y) / 6.0;
b[2] = (3 * points[i-1].y - 6 * points[i].y + 3 * points[i+1].y) / 6.0;
b[3] = (-3 * points[i-1].y + 3 * points[i+1].y) / 6.0;
b[4] = (points[i-1].y + 4 * points[i].y + points[i+1].y) / 6.0;
c[1] = (-points[i-1].z + 3 * points[i].z - 3 * points[i+1].z + points[i+2].z) / 6.0;
c[2] = (3 * points[i-1].z - 6 * points[i].z + 3 * points[i+1].z) / 6.0;
c[3] = (-3 * points[i-1].z + 3 * points[i+1].z) / 6.0;
c[4] = (points[i-1].z + 4 * points[i].z + points[i+1].z) / 6.0;
for j = 1, subdivision do
index = index + 1
spline[index] = {}
local t = j / subdivision
spline[index].x = (a[3] + t * (a[2] + t * a[1])) * t + a[4]
spline[index].y = (b[3] + t * (b[2] + t * b[1])) * t + b[4]
spline[index].z = (c[3] + t * (c[2] + t * c[1])) * t + c[4]
end
end
return spline
end
local v = {{x = 0, y = 0, z = 0}, {x = 1, y = 0, z = 0}, {x = 1, y = 1, z = 0}, {x = 0, y = 1, z = 0}}
print()
print("bspline2D");
for _, v3 in ipairs(M.bspline2D(v, 4)) do
print(v3.x .. ", " .. v3.y .. ", " .. v3.z)
end
print()
print("bspline3D");
for _, v3 in ipairs(M.bspline3D(v, 4)) do
print(v3.x .. ", " .. v3.y .. ", " .. v3.z)
end
I'm having an issue with my function call. I'm trying to find the solution to the two-point boundary value problem x' = f(t,x) = x + 0.09 x 2 + cos(10 t) with boundary condition x(0) + x(1) - 3.0 = 0 using the secant and third-order Runge-Kutta methods and for whatever reason, my equation method gives me four errors on a single line, those being that it's not a statement, is missing two semicolons, and is missing an end parenthesis.
public class BoundaryValueProblem
{
public static double f(double t, double x)
{
return x + 0.09x^2 + Math.cos(10t);
}
public static void findZero
{
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
BoundaryValueProblem FZ = new BoundaryValueProblem();
f1 = FZ.f(1.0, 1.0);
f2 = FZ.f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while(abs(x5 + x6 - 3.0) < 1e-5)
{
x4 = x6 - f2 * ((x6 - x5)/(f2 - f1));
fx = FZ.f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
public void rkm(double x0, double t0, double h)
{
double x1, x2, x3;
int i=0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0/3.0;
double c1 = 5.0/3.0;
double c2 = -4.0/3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
for(i = 0; i < 40; i++)
{
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0 );
}
System.out.println();
}
public static void main(String[] args)
{
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}
I've had it work on simpler equations but this one is not working for me. I have the rest of the code mostly correct (I believe), but I don't think it's the cause of the error since I commented it out and the errors persisted, thus I'm not focused on that. Any advice or help would be greatly appreciated.
Below is fixed code. Please remember that java unlike Octave or Matlab requires multiplication operator (*) and doesn't support pow operator (^) - you would need to use Math.pow() instead.
public class BoundaryValueProblem {
public static double f(double t, double x) {
return x + 0.09 * x * x + Math.cos(10 * t);
}
public static void findZero() {
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
f1 = f(1.0, 1.0);
f2 = f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while (Math.abs(x5 + x6 - 3.0) < 1e-5) {
x4 = x6 - f2 * ((x6 - x5) / (f2 - f1));
fx = f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
public void rkm(double x0, double t0, double h) {
double x1, x2, x3;
int i = 0;
double a1 = 0.5;
double a2 = 0.25;
double c0 = 2.0 / 3.0;
double c1 = 5.0 / 3.0;
double c2 = -4.0 / 3.0;
double b21 = -.25;
double b10 = .5;
double b20 = .5;
double stepsize = .025;
System.out.println("Runge-Kutta Method:");
System.out.println("i: \t\t h: \t\t t0: \t\t x0:");
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
for (i = 0; i < 40; i++) {
x1 = x0 + h * b10 * f(t0, x0);
x2 = x0 + h * (b20 * f(t0, x0) + b21 * f(t0 + a1 * h, x1));
x3 = x0 + h * (c0 * f(t0, x0) + c1 * f(t0 + a1 * h, x1) + c2 * f(t0 + a2 * h, x2));
t0 = t0 + stepsize;
x0 = x3;
System.out.println(i + "\t\t " + h + "\t\t " + t0 + "\t\t " + x0);
}
System.out.println();
}
/**
* #param args
*/
public static void main(String[] args) {
BoundaryValueProblem FZ1 = new BoundaryValueProblem();
FZ1.findZero();
BoundaryValueProblem RKM1 = new BoundaryValueProblem();
RKM1.rkm(1.0, 0.0, 0.0);
}
}
public static void findZero// correct this. Where are parenthesis () for this method
{
double x4;
double x5 = .7;
double x6 = 1.0;
int n = 1;
double fx;
double f1;
double f2;
double Error;
BoundaryValueProblem FZ = new BoundaryValueProblem();
f1 = FZ.f(1.0, 1.0);
f2 = FZ.f(1.0, 1.0);
System.out.println("Secant Method");
System.out.println("n: \t\t x1: \t\t x2: \t\t Error:");
while(abs(x5 + x6 - 3.0) < 1e-5)
{
x4 = x6 - f2 * ((x6 - x5)/(f2 - f1));
fx = FZ.f(1.0, 1.0);
x5 = x6;
x6 = x4;
f1 = f2;
f2 = fx;
Error = x5 + x6 - 3.0;
System.out.println(n + "\t\t" + x5 + "\t\t" + x6 + "\t\t" + Error);
n++;
}
System.out.println();
}
There is a method without parentheses: public static void findZero
It should be public static void findZero()
Function findZero should have parenthesis at the end like this:
public static void findZero()
return x + 0.09x^2 + Math.cos(10t);
In Java you have to put math operator between operands so instead of 0.09x you have to write 0.09*x and instead of 10t you have to write 10*t
^ sign is XOR operator not power operator so you have to use Math.pow function (I'm guessing that's what you wanted to do)
In the end this line should look like this:
return x + Math.pow(0.09*x, 2) + Math.cos(10*t);
I'm also getting error with abs function. I don't know if you wrote that function yourself but if you didn't you should use Math.abs function
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
When I change the list option in the code below, java picks up my input and gives me an answer that is really weird. This is the code:
package tempconverter;
import javax.swing.*;
import java.awt.event.*;
import java.awt.FlowLayout;
import static java.lang.System.out;
public class TempConverter extends JFrame implements ActionListener {
final static String[] inList = {"From Celsius", "From Fahrenheit", "From Kelvin"};
static JFrame f = new JFrame();
static JTextField enter = new JTextField(3);
static JButton confirm = new JButton("Convert");
static JList choose = new JList(inList);
public static void main(String[] args) {
confirm.addActionListener(new TempConverter());
f.setLayout(new FlowLayout());
f.setSize(300, 60);
f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
f.add(enter);
f.add(confirm);
f.add(choose);
f.setVisible(true);
}
#Override
public void actionPerformed (ActionEvent e) {
Object choice = choose.getSelectedValue();
double toConvert = Double.parseDouble(enter.getText());
double inF, inK, inC;
if (choice.equals("From Celsius")) {
inF = toConvert * 1.8 + 32;
inK = toConvert + 273.15;
out.println("In degrees Fahrenheit, " + toConvert + " degrees Celsius would be " + inF + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Celsius would be " + inK + " degrees.");
}
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = toConvert - 32 / 1.8 - 273.15;
out.println("In degrees Celsius, " + toConvert + " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Fahrenheit would be " + inK + " degrees.");
}
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = inC + 1.8 + 32;
out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
}
}
}
How do I make java realise that I changed the list selection?
An example I tested is that I set the selection to "From Fahrenheit" and typed 32, but it gave me 14.222222222222221 degrees Celsius and -258.92777777777775 degrees Kelvin.
It should be +273.15 instead of -273.15, and typo with *. Change mentioned in comment
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = toConvert - 32 / 1.8 + 273.15;
// change here ............^......
out.println("In degrees Celsius, " + toConvert
+ " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert
+ " degrees Fahrenheit would be " + inK + " degrees.");
}
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = inC * 1.8 + 32;
// .......^.... change here
out.println("In degrees Celsius, " + toConvert
+ " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert
+ " degrees Kelvin would be " + inF + " degrees.");
}
First you have wrong calculation here.
inF = inC + 1.8 + 32;
here is working code.
if (choice.equals("From Kelvin")) {
inC = toConvert - 273.15;
inF = (inC * 1.8) + 32;
out.println("In degrees Celsius, " + toConvert + " degrees Kelvin would be " + inC + " degrees.");
out.println("In degrees Fahrenheit, " + toConvert + " degrees Kelvin would be " + inF + " degrees.");
}
if (choice.equals("From Fahrenheit")) {
inC = toConvert - 32 / 1.8;
inK = (toConvert - 32 / 1.8) + 273.15;
out.println("In degrees Celsius, " + toConvert + " degrees Fahrenheit would be " + inC + " degrees.");
out.println("In degrees Kelvin, " + toConvert + " degrees Fahrenheit would be " + inK + " degrees.");
}